Jump to content
Search Community

Recommended way to propagate state change on timeline? (.eventCallback() is not sufficient)

SimonWidjaja test
Moderator Tag

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

Recommended Posts

Hi GSAP enthusiasts,

I am searching for the best way to propagate state changes on timeline level (e.g. "timeline was paused", "timeline started playing again").

There are event callbacks: "onComplete", "onUpdate", "onStart", "onReverseComplete" or "onRepeat". But none of them is working for me...

I am looking for something like "onStateChange" that I can listen for, to properly propagate state changes.

 

e.g.

tl..eventCallback('onStateChange', function(evt) {

  if (tl.paused()) { /*...*/ }
  else { /*...*/ }

})

 

Is there any recommended way to achieve this?

Any hint appreciated.

Happy animating!

Link to comment
Share on other sites

There isn't really anything in the API currently for that.

I suspect you could create your own AnimationObject class and assign it an animation property which would then reference your timeline.

Your AnimationObject would have its own play(), pause(), resume(), reverse() methods.

The AnimationObject would have custom logic that tracks the state of its animation and be responsible for firing events when the state changes.

So instead of calling tl.pause() directly you would go through your AnimationObject like

 

var myAnimationObject = new AnimationObject(tl);

// then later if you want to pause tl you would do

myAnimationObject.pause();

 

When you call myAnimation.pause() the pause() method will check to see if the AnimationObject's animation (your tl) is paused, and if not it will know that the state has changed and fire the appropriate event(s)

 

This is just a very simplistic overview. It will take a bit of work to code it for real and wire it all up, but its entirely possible. 

 

 

  • Like 4
Link to comment
Share on other sites

Hey Carl, thanks for the quick reply.

I have already created my own Controller object to proxy the immediate timeline commands (pause, play, seek etc) as you mention. That totally works for my own "controlled" calls. But it's not working for other calls like timeline triggers etc. on dynamic timelines (e.g. with user generated custom actions that call timeline functions directly). So there is no other more robust (lower level) solution? The only solution I see right now would be proxying (overriding) the relevant state changing functions of the TimelineMax object. But that would be pretty dirty... Your thoughts?

Thanks again for your amazing support!

  • Like 1
Link to comment
Share on other sites

ok thanks! then please consider an additional event callback "onStateChange" as a feature request ;)

As interim solution I will proxy/hijack all state changing methods of the timeline object and implement the state change propagation myself. I will let you know if it works... 

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