tmusharraf Posted November 30, 2022 Share Posted November 30, 2022 I'm working with GSAP and using the timeline. I'm updating the GSAP timeline pause or resume based on the useState. I created a env here please have a look at it. const [MovingBoxTimeLineAnim, setMovingBoxTimeLineAnim] = useState(true); let MovingBoxTimeLine = gsap.timeline({ ... }) useLayoutEffect(() => { let ctx = gsap.context(() => { gsap.set(".moving_box", { ... } MovingBoxTimeLine.to(".moving_box", {...}) MovingBoxTimeLine.pause() }, howToSectionAnim); return () => ctx.revert(); // cleanup }) // END OF: useLayoutEffect useEffect( () => { MovingBoxTimeLineAnim ? MovingBoxTimeLine.resume() : MovingBoxTimeLine.pause() }, [MovingBoxTimeLineAnim]) const StopAnimation = () => { console.log('stop') setMovingBoxTimeLineAnim(!MovingBoxTimeLineAnim) } const ResumeAnimation = () => { console.log('resume') setMovingBoxTimeLineAnim(!MovingBoxTimeLineAnim) } StopAnimation and ResumeAnimation called when clicked on a button Link to comment Share on other sites More sharing options...
GSAP Helper Posted November 30, 2022 Share Posted November 30, 2022 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 CodeSandbox that demonstrates the issue? Please don't include your whole project. Just some colored <div> elements and the GSAP code is best (avoid frameworks if possible). 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 If you're using something like React/Next/Nuxt/Gatsby or some other framework, you may find CodeSandbox easier to use. 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 More sharing options...
Rodrigo Posted November 30, 2022 Share Posted November 30, 2022 Hi, The only issue I can think of with the information you are providing is that your timeline instance is being created on every component render: const [MovingBoxTimeLineAnim, setMovingBoxTimeLineAnim] = useState(true); let MovingBoxTimeLine = gsap.timeline({ ... }) Every time you update your state MovingBoxTimeLine will be created again which is why the pause method might not be working as expected. The solution for GSAP instances that are used many times during the life cycle of a React App is to store them in a useRef object that will keep the GSAP timeline untouched through re-renders: const [MovingBoxTimeLineAnim, setMovingBoxTimeLineAnim] = useState(true); const MovingBoxTimeLine = useRef(); useLayoutEffect(() => { let ctx = gsap.context(() => { gsap.set(".moving_box", { ... } MovingBoxTimeLine.current = gsap.timeline({paused: true}) .to(".moving_box", {...}); }, howToSectionAnim); return () => ctx.revert(); // cleanup }); // END OF: useLayoutEffect useEffect( () => { MovingBoxTimeLineAnim ? MovingBoxTimeLine.current.resume() : MovingBoxTimeLine.current.pause() }, [MovingBoxTimeLineAnim]); Hopefully this helps you get started. If you have more questions please use this starter codesandbox example: https://codesandbox.io/s/gsap-react-setup-zvbntr?file=/src/App.js https://codesandbox.io/s/gsap-react-starter-ueftpb Happy Tweening! Link to comment Share on other sites More sharing options...
tmusharraf Posted December 1, 2022 Author Share Posted December 1, 2022 10 hours ago, Rodrigo said: Hi, The only issue I can think of with the information you are providing is that your timeline instance is being created on every component render: const [MovingBoxTimeLineAnim, setMovingBoxTimeLineAnim] = useState(true); let MovingBoxTimeLine = gsap.timeline({ ... }) Every time you update your state MovingBoxTimeLine will be created again which is why the pause method might not be working as expected. The solution for GSAP instances that are used many times during the life cycle of a React App is to store them in a useRef object that will keep the GSAP timeline untouched through re-renders: const [MovingBoxTimeLineAnim, setMovingBoxTimeLineAnim] = useState(true); const MovingBoxTimeLine = useRef(); useLayoutEffect(() => { let ctx = gsap.context(() => { gsap.set(".moving_box", { ... } MovingBoxTimeLine.current = gsap.timeline({paused: true}) .to(".moving_box", {...}); }, howToSectionAnim); return () => ctx.revert(); // cleanup }); // END OF: useLayoutEffect useEffect( () => { MovingBoxTimeLineAnim ? MovingBoxTimeLine.current.resume() : MovingBoxTimeLine.current.pause() }, [MovingBoxTimeLineAnim]); Hopefully this helps you get started. If you have more questions please use this starter codesandbox example: https://codesandbox.io/s/gsap-react-setup-zvbntr?file=/src/App.js https://codesandbox.io/s/gsap-react-starter-ueftpb Happy Tweening! Thanks for the reply, Actually I was using the useRef but still not able to achieve what I want to achieve. Below is my complete code. I hope you can see what's the real problem. Thanks export const HowToSection = () => { const [MovingBoxTimeLineAnim, setMovingBoxTimeLineAnim] = useState(true); let MovingBoxTimeLine = gsap.timeline({ defaults: { repeat: -1, ease: "none", } }) const howToSectionAnim = useRef(); useLayoutEffect(() => { let ctx = gsap.context(() => { gsap.set(".moving_box", { y: (i) => i == 2 ? "200%" : "60px", x: (i) => i == 0 ? "34px" : i == 1 ? "80vw" : i == 2 ? "50vw" : "" }); MovingBoxTimeLine.to(".moving_box", { x: "+=150vw", duration: 10, modifiers: { x: gsap.utils.unitize(x => parseFloat(x) % 150, 'vw') } } ) MovingBoxTimeLine.pause() }, howToSectionAnim); return () => ctx.revert(); // cleanup }, []); useEffect( () => { MovingBoxTimeLineAnim ? MovingBoxTimeLine.resume() : MovingBoxTimeLine.pause() }, [MovingBoxTimeLineAnim]) const StopAnimation = () => { console.log('stop') setMovingBoxTimeLineAnim(!MovingBoxTimeLineAnim) } const ResumeAnimation = () => { console.log('resume') setMovingBoxTimeLineAnim(!MovingBoxTimeLineAnim) } Link to comment Share on other sites More sharing options...
Solution Rodrigo Posted December 1, 2022 Solution Share Posted December 1, 2022 Hi, In your code you are not using a ref for your GSAP instance: let MovingBoxTimeLine = gsap.timeline({ defaults: { repeat: -1, ease: "none", } }) That right there is going to be initialized everytime your component re-renders. The timeline has to be in a ref when the components are re-rendered because of state changes, otherwise that instance is not the same being controlled in the useEffect hook. As mentioned before is really hard for us to troubleshoot without a minimal demo. Here is a starter template that you can fork and use to create your demo: https://stackblitz.com/edit/gsap-react-toggle-timeline?file=src%2FApp.js As you can see the timeline is stored in a ref and toggled using the state, you should follow the same approach in your case. Happy Tweening! 2 Link to comment Share on other sites More sharing options...
tmusharraf Posted December 6, 2022 Author Share Posted December 6, 2022 On 12/1/2022 at 7:50 PM, Rodrigo said: Hi, In your code you are not using a ref for your GSAP instance: let MovingBoxTimeLine = gsap.timeline({ defaults: { repeat: -1, ease: "none", } }) That right there is going to be initialized everytime your component re-renders. The timeline has to be in a ref when the components are re-rendered because of state changes, otherwise that instance is not the same being controlled in the useEffect hook. As mentioned before is really hard for us to troubleshoot without a minimal demo. Here is a starter template that you can fork and use to create your demo: https://stackblitz.com/edit/gsap-react-toggle-timeline?file=src%2FApp.js As you can see the timeline is stored in a ref and toggled using the state, you should follow the same approach in your case. Happy Tweening! Thank you, I was not using a ref for GSAP instance. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now