Jump to content
Search Community

Reversing the target timeline

ashura test
Moderator Tag

Recommended Posts

Hello, my problem here is that why nestedTL can't get reverse even if I already give a reverse in the "tl" is there something wrong trying to conflict each other? Can anyone tell me notice how is it done here? I'm using a react. I tried different things to reversing but nothing really working out. I'm trying to reverse a specific timeline while adding it in the main timeline "tl". 
 

            const nestedTL = gsap.timeline({ defaults: { duration: 2, ease: 'sin.inOut' }, paused: false });
            
            const tl = gsap.timeline({ 
                defaults: { duration: 2, ease: 'sin.inOut' }, 
                paused: false,
                onComplete:() => reverseNestedTL()
            });
            
            nestedTL.to('.container-opening', {
              '--opening-dash': '100%',
              ease: 'expo.inOut'
            });
            
            nestedTL.to('.auth-lyr .line', {
              '--line-width': '50px',
              ease: 'expo.inOut'
            }, 1);


            tl.add(nestedTL);
            
            // Animations in tl (not included in reverse)
            tl.to('.auth-lyr h4', {
              opacity: 1,
              filter: 'blur(0px)'
            }, 1);
            
            const toAllText = document.querySelectorAll('.original .d-word');
            
            toAllText.forEach((letter, index) => {
                const randomAnimation = animations[Math.floor(Math.random() * animations.length)];
                gsap.set(letter, {
                    transform: `${randomAnimation}`,
                    filter: 'blur(5px)',
                    opacity: 0
                });
                
                // Animations in tl (not included in reverse)
                tl.to(letter, {
                    transform: 'translateX(0%) translateY(0%)',
                    filter: 'blur(0px)',
                    duration: Math.floor(Math.random() * 3) + 1.5,
                    ease: 'expo.inOut',
                    opacity: 1,
                }, 2);
            });

            const newTL = gsap.timeline()

            // Add h1.overlaying-text to nestedTL for reversal
            newTL.to('h1.overlaying-text', {
                opacity: 0.865,
                duration: 5,
                ease: 'sin.inOut',
            });

            nestedTL.add(newTL)

            tl.add(newTL)
            
            function reverseNestedTL() {
                nestedTL.timeScale(2).reverse();
            }
            // // Assign the onComplete callback as a function reference without executing it immediately
            // tl.eventCallback("onComplete", () => {
            //     nestedTL.reverse();
            // });

            // Add h1.overlaying-text to nestedTL for reversal
            // tl.add(nestedTL.nextLabel(2));

 

Link to comment
Share on other sites

that problem I already solved it already, but the problem is that I want to use reversing without recalling the same logic that applies in the "tl"...anyways if anyone can give me a better idea than using this one.. I would be glad thank you.

like for example calling the '--opening-dash' again or something like that in their structure rather just reversing with specific targets of timeline that would be easy for me. 

 

            const tl = gsap.timeline({ defaults: { duration:2, ease:'sin.inOut' }, paused:false }) 
            tl.to('.container-opening',{
                '--opening-dash':'100%',
                ease:"expo.inOut"
            })
            tl.to('.auth-lyr .line',{
                "--line-width":'50px',
                ease:'expo.inOut'
            },1)
            tl.to('.auth-lyr h4',{
                opacity:1,
                filter:'blur(0px)'
            },1)

            const toAllText = document.querySelectorAll('.original .d-word');
            console.log(toAllText)

            toAllText.forEach((letter, index) => {
                const randomAnimation = animations[Math.floor(Math.random() * animations.length)];
                gsap.set(letter,{
                    transform:`${randomAnimation}`,
                    filter:'blur(5px)',
                    opacity:0
                })
                console.log(letter)
                tl.to(letter,{
                    transform:`translateX(0%) translateY(0%)`,
                    filter:'blur(0px)',
                    duration: Math.floor(Math.random() * 3) + 1.5, // Range between 3 to 5 seconds
                    ease:'expo.inOut',
                    opacity:1,
                },2);
            });

            tlRef.current = tl.to('h1.overlaying-text',{
                opacity:0.865,
                duration:5,
                ease:'sin.inOut',
            })

            tl.eventCallback("onComplete", () => {
                const reverseTL = gsap.timeline({ defaults: { duration:2, ease:'sin.inOut' }, paused:false, onComplete:() => {
                    setUp(true)
                } }) 

                reverseTL.to('.container-opening',{
                    '--opening-dash':'0%',
                    ease:"expo.inOut"
                })
                reverseTL.to('h1.overlaying-text',{
                    opacity:0,
                    duration:5,
                    ease:'sin.inOut'
                })

            })

 

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