Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
SimonWidjaja

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

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!

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Maybe you could have an onUpdate callback that checks the progress and state of the timeline? Might be expensive performance-wise, though.

  • Like 1

Share this post


Link to post
Share on other sites

So there is no other more robust (lower level) solution? 

 

sorry. not at this time.

Share this post


Link to post
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... 

Share this post


Link to post
Share on other sites

Related to @Carl suggestion.

 

 

 

You could also make use of an event emitter.

 

 

  • Like 4

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.

×