Jump to content
Search Community

re-render timeline instance not working in a React app

Ethan Wood test
Moderator Tag

Recommended Posts

I try to briefly describe my problem:

 

I wrote a React application to generate a series of animations on a web page. Each element can be added with gsap animation effects. The animation can be rendered by passing the animation configuration parameters corresponding to this element to gsap. For example, to Implementing an animation that rotates an element will go through the following process:

const config = {
  duration: 3,
  rotate: 360,
  delay: 1,
}

const element = "#text-element-1";

const timeline = gsap.to(element, config);

 

Now, I will store the config corresponding to each element in redux, and then implement the function of modifying the config of the element through a form. This animation parameter is passed to gsap.

 

Now, when I modify the animation parameters of an element and submit it to redux, the program will read the new config and recreate a gsap timeline instance. I use this timeline to control the animation by calling play () and other methods.

 

Problem Description:
① When I use a default config to pass to gasp, the animation can be executed smoothly, the following is the correct animation execution (check the style attribute change of the dom element)

correct.thumb.gif.b1c4a997877111306d8e0ed7a262c18f.gif
 
② But whenever I modify the config of the element, a timeline instance will be regenerated. At this time, the timeline instance will be called again, and the animation will not respond. However, I checked the dom and found that the style attribute was indeed updated by gasp , but not the correct one.
abnormal.thumb.gif.45018526c28766905e926f5a673c6ae5.gif
 
You can clearly see the difference. After regenerating the timeline instance, gasp does change the style attribute of the element, but it should be done gradually, not all at once.
 
Because of the complexity and size of the entire application, I can't provide an example through codepen, sorry. But I have provided partial examples and code screenshots with similar logic, I hope you can help me see. This code reads the element's config and then generates a masterTimeline instance:
  const keyframes: keyframesData = useSelector((state: any) => state.anime.keyframes);
  const hasKfrChange = useSelector((state: any) => state.anime.hasKfrChange);

  const [timeline, setTimeline] = useState(gsap.timeline({ paused: true }));

  useEffect(() => {
    const fragment = gsap.timeline({ paused: true });

    if (keyframes) {
      Object.values(keyframes).forEach((value: any) => {
        fragment.add(animeExe(value), "<");
        setTimeline(fragment);
      });
    } else {
      throw new Error("data type of keyframesdata is only can be object");
    }
  }, [hasKfrChange]);

Parameter explanation:

  • keyframes is an object that stores the config of each element. I will traverse to get all the animation parameters, and then pass them to animeExe.
  • The role of animeExe is to accept the current incoming config, then generate a timeline instance, and return this value. Like this: timeline.to('#my-element', config); return timeline;
 

A series of elements are contained in a keyframes object, and then, gsap animations of all elements will be added to the timeline instance, and finally will be added to the main timeline masterTimeline to realize gsap animation generation of a series of elements.

 

So, my question is, why the different timeline instances re-create according to different configs are only valid when they are called for the first time, but are invalid after modifying the config?

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

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