Jump to content
Search Community

Timeline.addPause doesn't work if no tweens are added

Zadvorsky 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

Hello friends.

 

I ran into a strange issue: if you .addPause() to a timeline that has doesn't have any tweens inside it (only callbacks), the timeline isn't paused.

See the example pen. Note that when the pen runs, both 'before pause' and 'after pause' are logged.

If you uncomment either of the tweens, addPause works correctly.

 

I know this looks a little silly, but I ran into this issue in production.

I'm working on a state transition system where states have an 'enter' and an 'exit' method.

The states also have a preload function, which returns a promise.

 

In pseudo code, the transition logic looks like this:

 

const tl = new TimelineLite();

if (currentState) {
  tl.add(currentState.exit());
  tl.add(() => {
    scene.remove(currentState);
  });
}

if (nextState) {
  tl.addPause();
  tl.add(() => {
    scene.add(nextState);
  });
  tl.add(nextState.enter());
  
  nextState.preload().then(() => {
    tl.resume(); // or removePause
  });
}

 

The goal is to begin preload and exit simultaneously. Then when both are done, the next state is added to the scene and it's enter animation is played.

 

The 'enter' and 'exit' methods return a no-op function (if there is no animation for that state), or a tween (if there is an animation for that state).

 

The issue I have is that if there is no current state (or the current state has no exit animation), AND the next state doesn't have an enter animation, the 'scene.add(nextState);' callback is triggered before the preload is complete.

 

I can work around this in code, but I would expect the 'addPause' to work even without any tweens.

Or maybe this is too much of an edge case?

 

EDIT: Actually, it looks like both callbacks in the pen are also triggered if only the 1st tween is uncommented. But if the 2nd (or both) tweens are uncommented, the pause works as expected.

 

See the Pen 0c8217e76810f3fd2a72bd4375b67c5a by zadvorsky (@zadvorsky) on CodePen

Link to comment
Share on other sites

@zadvorski It gets tricky because GSAP mainly is for animation and works with time even if it is tiny fraction it will work. We ran into similar question once and I had to create my own question on this topic, where you can find more detailed explanation.

 

 

You can get around it by adding tiny fraction of time as following demo. Also, please avoid posting private demos as we can't fork them.

 

See the Pen zWYBpR?editors=0110 by Sahil89 (@Sahil89) on CodePen

 

  • Like 3
Link to comment
Share on other sites

Hi zadvorksi,

 

Thanks for the clear demo.

The demo and thread Sahil referenced are great and should shed some light on how things work.

However, if you need more info on the what, why, how, just let us know.

 

In the meantime I'm going to take this opportunity to promote your work https://codepen.io/zadvorsky/ to the others as I'm a bit of a fan.

 

Carl

 

 

  • Like 4
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...