Jump to content
Search Community

Gsap Timeline did not pause due to react hooks or may be re-render

Rizwan Wbst test
Moderator Tag

Recommended Posts

Here is my code for the SVG path animation I want to stop the animation when the the started and crashed both values are true, it did call the pause function but it never did stop. please help to understand where i'm wrong
const DynamicChart = ({ delay, started, crashed, launchTime }) => {
  // console.log('delay, started, crashed, launchTime---',delay(), started, crashed, launchTime);
  const dRef = useRef(null);
  const lRef = useRef(null);
  const uRef = useRef(null);
  const jRef = useRef(null);
  const bRef = useRef(null);
  const hRef = useRef(null);
  const [f, setF] = useState(null);
  const [N, setN] = useState(null);
  const seconds = (Date.now() - (launchTime || 0)) / 1e3;

  const I = (e, t, a, c, n) => {
    const d1 = t.current?.getAttribute("d");
    const d2 = a.current?.getAttribute("d");

    c.to(e.current, { duration: 40, paused: n, attr: { d: d1 } }, "+=1")
      .to(e.current, { duration: 40, paused: n, attr: { d: d2 } }, "+=1")
      .to(e.current, { duration: 40, paused: n, attr: { d: d2 } }, "+=1");
  };

  useEffect(() => {
    if (started && crashed) {
      setF(gsap.timeline({ defaults: { duration: 1 }, paused: true }));
      setN(gsap.timeline({ defaults: { duration: 1 }, paused: true }));
    } else if (started) {
      setF(gsap.timeline({ defaults: { duration: 1 } }));
      setN(gsap.timeline({ defaults: { duration: 1 } }));
    }
  }, [started, crashed]);

  useEffect(() => {
    if (N && f) {
      if (!crashed) {
        const r = seconds >= 7 ? seconds - 7 : 0;
        N.seek(r);
        f.seek(r);
        I(dRef, jRef, hRef, f, crashed);
        I(lRef, uRef, bRef, N, crashed);
      } else {
        f.pause();
        N.pause();
      }
    }
  }, [crashed, seconds, N, f, jRef, hRef, lRef, uRef, bRef, dRef]);

  const getSVG = (e, t, a, c) => {
    return (
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 790 516">
        <defs>
          <linearGradient
            id={c ? "lineGradient" : "pathGradient"}
            gradientTransform="rotate(80)"
          >
            <stop offset="100%" stopColor="#fd4a95" stopOpacity={c ? 1 : 0.4}>
              <animate
                attributeName="stopColor"
                values="#fd4a95"
                dur="3s"
                repeatCount="indefinite"
              ></animate>
            </stop>
            <stop offset="100%" stopColor="#dd7dab20" stopOpacity={c ? 1 : 0}>
              <animate
                attributeName="stop-color"
                values="#dd7dab20"
                dur="3s"
                repeatCount="indefinite"
              ></animate>
            </stop>
          </linearGradient>
        </defs>
        <path
          ref={e}
          className={c ? 'glc-l' : 'glc-g'}
          d="M790,516H0L0,410.1C274.03013,325.17245,509.13669,190.059,701.62815,0H790Z"
        ></path>
        <path
          ref={t}
          d="M790,516H0L0,410.1C307.15109,408.69073,604.58475,296.45161,701.62815,0H790Z"
        ></path>
        <path
          ref={a}
          d="M790,516H0L0,410.1C395,410.1,701.62815,335.7353,701.62815,0H790Z"
        ></path>
      </svg>
    );
  };

  return (
    <div className="game-line-chart-holder absolute-splash" style={delay(0)}>
      <ul className="absolute-splash">
        <li className="absolute-splash">{getSVG(dRef, jRef, hRef, false)}</li>
        <li className="absolute-splash">{getSVG(lRef, uRef, bRef, true)}</li>
      </ul>
    </div>
  );
};

 

Link to comment
Share on other sites

It's pretty tough to troubleshoot without a minimal demo - the issue could be caused by CSS, markup, a third party library, your browser, an external script that's totally unrelated to GSAP, etc. Would you please provide a very simple CodePen or Stackblitz that demonstrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best. See if you can recreate the issue with as few dependancies as possible. If not, incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, then at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

Here's a starter CodePen that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen

 

Using a framework/library like React, Vue, Next, etc.? 

CodePen isn't always ideal for these tools, so here are some Stackblitz starter templates that you can fork and import the gsap-trial NPM package for using any of the bonus plugins: 

 

Please share the StackBlitz link directly to the file in question (where you've put the GSAP code) so we don't need to hunt through all the files. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

Link to comment
Share on other sites

Hi there! I see you're using React -

Proper animation 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.

In GSAP 3.11, we introduced a new gsap.context() feature that helps make animation cleanup a breeze. All you need to do is wrap your code in a context call. All GSAP animations and ScrollTriggers created within the function get collected up in that context so that you can easily revert() ALL of them at once.

Here's the structure:

// typically it's best to useLayoutEffect() instead of useEffect() to have React render the initial state properly from the very start.
useLayoutEffect(() => {
  let ctx = gsap.context(() => {
    // all your GSAP animation code here
  });
  return () => ctx.revert(); // <- cleanup!
}, []);

This pattern follows React's best practices, and one of the React team members chimed in here if you'd like more background.

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

Happy tweening!

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...