Jump to content
Search Community

How to Invert Trace Animation From Right to Left

ReinhardTral test
Moderator Tag

Recommended Posts

I have an animation that goes from left to right. The arrows point towards the invert, as shown in the image. I want to reverse the animation so that it goes in the opposite direction, from left to right. Is there an easy way to do this?"

 

Mi code :

 

"use client"

import React, { useEffect, useRef } from 'react';
import { gsap, MotionPathPlugin } from 'gsap/all';
gsap.registerPlugin(MotionPathPlugin);

const CornerTopRight = () => {
    useEffect(() => {
        // Número de animaciones
        const numAnimations = 4;

        // Duración y retraso base
        const duration = 5;
        const baseDelay = 0.3;
        const repeatDelay = 0.9;


        // Crear un bucle para generar las animaciones
        for (let i = 1; i <= numAnimations; i++) {
            const delay = baseDelay * (i - 1);

            gsap.to(`.arrowCornerTopR${i}`, {
                duration,
                yoyo: true,
                repeat: -1,
                ease: "linear",
                motionPath: {
                    autoRotate: 90,
                    drawSVG: 0,
                    path: ".pathCornerTopR",
                    align: ".pathCornerTopR",
                    alignOrigin: [0.8, 0.8]
                },
                delay,
                repeatDelay,
            }).play();
        }

        // Limpiar todas las animaciones al desmontar el componente
        return () => {
            for (let i = 1; i <= numAnimations; i++) {
                gsap.killTweensOf(`.arrowCornerTopR${i}`);
            }
        };
    }, []);


    return (
        <svg className="w-full" viewBox="0 0 95.084 22.22" xmlns="http://www.w3.org/2000/svg">
            <g fill="#000000" transform="matrix(0.9718469977378846, 0, 0, 0.8569409847259521, 0.8365160947518957, 1.7288000841259965)">
                <path d="M95.084 6.222h-.813v1.777h.813zm0 3.555h-.813v1.778h.813zm0 3.555h-.813v1.778h.813zm0 3.555h-.813v1.778h.813zm0 3.556h-.813v1.777h.813zm0-16.443c0-.511-.09-1.01-.256-1.476l-.756.33c.13.36.199.746.199 1.146v.444h.813zM93.751.913a3.509 3.509 0 0 0-1.66-.849l-.146.876c.476.098.92.324 1.29.658zM90.402 0h-1.626v.889h1.626zM87.15 0h-1.626v.889h1.626zm-3.253 0h-1.627v.889h1.627zm-3.253 0h-1.627v.889h1.627zM77.39 0h-1.627v.889h1.627Zm-3.254 0H72.51v.889h1.626zm-5.93.022h-1.627V.91h1.627zm2.905 0h-1.627V.91h1.627zM23.823.056h-1.626v.888h1.626zm-3.253 0h-1.627v.888h1.627zm-3.253 0H15.69v.888h1.627zm-3.253 0h-1.627v.888h1.627zm-3.253 0H9.184v.888h1.627zm-3.254 0H5.931v.888h1.626zm-5.93.021H0v.89h1.627zm2.904 0H2.905v.889H4.53z" />
                <path d="M68.208.016h-1.627v.889h1.627zm-3.253 0h-1.627v.889h1.627zm-3.253 0h-1.627v.889h1.627zm-3.254 0h-1.626v.889h1.626zm-3.253 0H53.57v.889h1.626zm-3.253 0h-1.626v.889h1.626zm-5.93.022h-1.627v.888h1.626zm2.904 0H47.29v.888h1.626z" />
                <path d="M46.011.032h-1.626V.92h1.626zm-3.253 0h-1.627V.92h1.627zm-3.253 0h-1.627V.92h1.627zm-3.253 0h-1.627V.92h1.627zm-3.253 0h-1.627V.92h1.627zm-3.254 0H28.12V.92h1.626zm-5.93.021h-1.627v.89h1.627zm2.904 0h-1.626v.889h1.626z" />
                <path className='pathCornerTopR' d="M1.425 1.729 L35.857 1.729 L93.244 1.729 M93.244 1.729 L93.244 8.869 L93.244 20.77" />
            </g>
            {[1, 2, 3, 4].map((index) => (
                    <path key={index} fill-opacity="1" className={`arrowCornerTopR${index}`} fillOpacity="1" d="M 3.374 0.171 C 3.374 0.171 1.787 4.069 1.787 4.069 C 1.787 4.069 4.961 4.069 4.961 4.069 C 4.961 4.069 3.374 0.171 3.374 0.171 Z" transform="matrix(0, 1, -1, 0, 8.1867e-8, 1.00523e-7)" />
                ))}
        </svg>
    );
};

export default CornerTopRight;

 

 

 

Captura desde 2024-02-06 08-24-27.png

CapturaEditada.png

Link to comment
Share on other sites

Hi there! I see you're using React -

Proper cleanup is very important with frameworks, but especially with React. React 18 runs in strict mode locally by default which causes your useEffect() and useLayoutEffect() to get called TWICE.

 

Since GSAP 3.12, we have the useGSAP() hook (the NPM package is here) that simplifies creating and cleaning up animations in React (including Next, Remix, etc). It's a drop-in replacement for useEffect()/useLayoutEffect(). All the GSAP-related objects (animations, ScrollTriggers, etc.) created while the function executes get collected and then reverted when the hook gets torn down.

 

Here is how it works:

const container = useRef(); // the root level element of your component (for scoping selector text which is optional)

useGSAP(() => {
  // gsap code here...
}, { dependencies: [endX], scope: container }); // config object offers maximum flexibility

Or if you prefer, you can use the same method signature as useEffect():

useGSAP(() => {
  // gsap code here...
}, [endX]); // simple dependency Array setup like useEffect()

This pattern follows React's best practices.

 

We strongly recommend reading the React guide we've put together at https://gsap.com/resources/React/

 

If you still need help, here's a React starter template that you can fork to create a minimal demo illustrating whatever issue you're running into. Post a link to your fork back here and we'd be happy to take a peek and answer any GSAP-related questions you have. Just use simple colored <div> elements in your demo; no need to recreate your whole project with client artwork, etc. The simpler the better. 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...