Jump to content
Search Community

.from() in ScrollTrigger only triggers when scrolling up

Léo Shcl test
Moderator Tag

Go to solution Solved by Rodrigo,

Recommended Posts

Hello everybody,

 

I'm new to Greensock and I face an unexpected behavior. I try to recreate my own SVG drawing animation by applying the same code as this example: 

 

I'm using Typescript React framework.

 

The animation for the ball following the past and the pulse animations work very well but the line isn't drawn when I scroll down but is drawn when I scroll up. So i figured out that the ".from()" of the timeline is triggering only when I scroll up. I tried to change it to ".from(".theLine", {drawSVG: 0}, 0)" to ".fromTo(".theLine", {drawSVG: 0}, {drawSVG: 1}, 0)" but this time the line doesn't appear at all.

 

When I scroll up here is what the path's style looks like: image.png.517402753721eb0be38473d791b645e6.png

When I scroll down: image.png.383dedaf16bf0e6ee21b4ed0640c5cc2.png

 

Can you help me please ?

 

Thank you ! :)

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

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

  • Solution

Hi @Léo Shcl and welcome to the GreenSock forums!

 

Thanks for being a Club GreenSock member and for supporting GreenSock! 💚

 

You are using React but you're not using GSAP Context. GSAP Context is your best friend when it comes to create animations with GSAP in React. This is especially important when using from() instances.

 

Perhaps the problem is that React 18 runs in "strict" mode locally by default which causes your useEffect() to get called TWICE! Very annoying. It has caused a lot of headaches for a lot of people outside the GSAP community too.

 

.from() tweens use the CURRENT value as the destination and it renders immediately the value you set in the tween, so when it's called the first time it'd work great but if you call it twice, it ends up animating from the from value (no animation). It's not a GSAP bug - it's a logic thing.

 

For example, let's say el.x is 0 and you do this: 

useEffect(() => {
  // what happens if this gets called twice?
  gsap.from(el, {x: 100})
}, []);

 

The first time makes el.x jump immediately to 100 and start animating backwards toward the current value which is 0 (so 100 --> 0). But the second time, it would jump to 100 (same) and animate back to the current value which is now 100 (100 --> 100)!  See the issue?

 

In GSAP 3.11, we introduced a new gsap.context() feature that solves all of this for you. All you need to do is wrap your code in a context call, and then return a cleanup function that reverts things: 

// 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!
}, []);

 

One of the React team members chimed in here if you'd like more background.

 

We strongly recommend reading the React article at

 

Here is a fork of your example:

https://stackblitz.com/edit/gsap-react-basic-f48716-xsiulv?file=src%2FApp.js

 

Hopefully this helps.

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