Jump to content
Search Community

Is there a way to call an animation complete on your own?

Damian Hutter test
Moderator Tag

Recommended Posts

Hi there,

 

Currently I'm playing around with framer-motion and GSAP. I really love GSAP, however, framer-motion has some need tricks like spring animations, page transitions and works just so well with react. So in a perfect world I would be able to combine the power of both to control great spring animations with GSAPs powerful timeline and morphing capabilities.

But, in order to do so I would need to tell framer motion when to start an animation. Easy - just using framer-motions start() function within GSAPs onStart() callback. However, the issue is, that GSAP calls an animation 'complete' way before it's really done. Which makes total sense as GSAP is not controlling the animation but messes up the timeline.

Therefore my question is - can I call an animation 'complete' on my own? As framer has the option to call a function onEnd().

Thanks in advance! Here's a little codesandbox outlining the overall idea...

https://codesandbox.io/s/suspicious-swanson-54075?file=/src/App.js

Link to comment
Share on other sites

Something like this, does it help? 🙂
 

  React.useEffect(() => {
    const duration = 2;
    tl.to(refApp.current, {
      duration,
      onStart: () => {
        console.log(controlCardWrapper);
        controlCardWrapper.start({
          rotate: 100,
          transition: { type: "spring", mass: duration }
        });
      },
      onComplete: () => console.log("I am ready!")
    });
  }, []);

 

Link to comment
Share on other sites

Hi @nicofonseca - Thanks a lot for your response!

 

Yea, I thought about something similar but it has one major flaw - A spring animation doesn't have a duration. The animation is based on physics and therefore calculated on the fly. That being said, it would be an option to time the animation and provide the duration to GSAP. But it feels very 'hacky'.

That's why I thought there might be a possibility to tell GSAP on your own when the animation should be completed. Basically an await function or something like that.

Anyway - good thinking 🎉

  • Like 1
Link to comment
Share on other sites

6 hours ago, Damian Hutter said:

That's why I thought there might be a possibility to tell GSAP on your own when the animation should be completed. Basically an await function or something like that.

 

I don't think I totally understanding what you mean. Can you explain this more? GSAP animations can be awaited, and you can do stuff like adding pauses or forcing animations to complete, but it's not clear from your code what you want.

 

  • Like 1
Link to comment
Share on other sites

8 hours ago, nicofonseca said:

If you don't set a duration value GSAP will set it as '1' by default

It's actually 0.5 seconds by default. :)

 

8 hours ago, Damian Hutter said:

can I call an animation 'complete' on my own?

Like @OSUblake I'm kinda lost here - let's say, for example, that you've got a gsap.to() animation with a duration of 2 seconds. Are you saying that you've also got a framer-motion animation going simultaneously that uses a physics-based spring ease so you don't actually have any idea when it will end...but let's just say that it'll end after 3.4 seconds....you want to force the gsap.to() animation to make its 2-second animation actually take 3.4 seconds instead? Or are you saying that if the framer-motion one happens to end FIRST, you want a way to force the GSAP one to jump to its completion state and call any onComplete (if defined)? 

 

If you could elaborate a bit, I'm sure that'd help us offer some suggestions. 

 

One of the major down sides of using physics-based animations is that it's almost impossible to control timing well. Imagine trying to do complex choreography or even doing simple things like "start this animation 0.5 seconds before the end of that other one" - pretty much impossible with a system like that. 

  • Like 3
Link to comment
Share on other sites

Hi @GreenSock, @OSUblake, @nicofonseca

First of all - thanks a lot for all the ideas and insides so far. That really helped a lot. I got back to my code and tried to come up with a cleaner version which might showcase some aspects of my explorations. Within the codesandbox (https://codesandbox.io/s/suspicious-swanson-54075?file=/src/App.js) you can now see 3 boxes falling from the sky and bouncing back up on the ground. I've put a description of each box within the code and hopefully this underlines the idea behind everything.

Thanks to @nicofonseca I was actually able to get the timeline to work by pausing and resuming the timeline from the framer onCompleteAnimation function. However, I need to specify at least a default duration of 0.1 for the overall timeline as a 0 is not working. That's where my initial idea comes into play if there would a way to tell the GSAP timeline - hey please await this function before moving on. Without the need to manually stop and resume the timeline itself.

@GreenSock You're right in terms of the difficult implementation when it comes to timing. However, framer-motion and others have the option to define a restSpeed or restDelta to tell the next animation to start. Which feels pretty natural. In my experiment I was just looking for the possibility to combine these animations with GreenSocks MorphSVGplugin which is just unmatched by anyone else. Also the ScrollTrigger is so much better than the one which framer-motion offers.

Thanks again so far - I've learned a lot already! 🎉

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