Jump to content
Search Community

How to chain timelines with masterTl.add(tl1).add(tl2)...

skyslide test
Moderator Tag

Recommended Posts

How can i chain timelines which waits for the last one to finish?

Do i have to calculate manually the length of the last timeline added with tl.add(tl1) (tl1.duration())?

I would like to play the added timelines as "chained clips", same behavior as tl.to() or tl.from(), but for timelines, not individual animations.

 

const tl1 = getMy10sAnimation1() //gsap.timeline() ...to().to().to()...
const tl2 = getMy2sAnimation1()  //gsap.timeline()
const tl3 = getMy7sAnimation1()  //gsap.timeline()

const masterTl = gsap.timeline({repeat: -1})
    .add(tl1)
    .add(tl2)
    .add(tl3)

// expected result:
// -----------
//            --
//              ---------
// repeat

// but i get:
// -----------
// --
// -------

 

Link to comment
Share on other sites

Hi and welcome to the GreenSock forums,

 

Without seeing all your code, my guess is that the functions like getMy10sAnimation1() are not returning the animations as shown in the demo below. Your functions are probably just creating the animations as soon as being called. You could log masterTl.duration() for further investigation.

 

 

 

See the Pen jOYxVpm??editors=1010 by snorkltv (@snorkltv) on CodePen

 

If you need more help please provide a minimal demo 

 

 

  • Like 4
Link to comment
Share on other sites

  • 1 year later...
On 4/8/2022 at 12:17 PM, Carl said:

Hi and welcome to the GreenSock forums,

 

Without seeing all your code, my guess is that the functions like getMy10sAnimation1() are not returning the animations as shown in the demo below. Your functions are probably just creating the animations as soon as being called. You could log masterTl.duration() for further investigation.

 

 

 

 

 

 

If you need more help please provide a minimal demo 

 

 

 

Hi I'm using GSAP with BarbaJS 2. I have this code

function blue() {
  let tl = gsap.timeline()
  .to(".blue", {scale:2})
  return tl
}

function pink() {
  let tl = gsap.timeline()
  .to(".pink", {rotation:360})
  return tl
}

const master = gsap.timeline()
master
  .call(blue, [])
  .call(pink, [])

I'm trying to use call function in place of add but the 2 timelines start at the same time, not chaining.

What could be the problem here? I read to use call when we need to pass arguments, is it true? Or we simply could use.add(blue(args)) ?

Link to comment
Share on other sites

Hi @Black Ducas!

 

Without a minimal demo, it's very difficult to troubleshoot; the issue could be caused by CSS, markup, a third party library, a 3rd party script, etc. Would you please provide a very simple CodePen or Stackblitz that illustrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best. See if you can recreate the issue with as few dependancies as possible. Start minimal and then incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen

that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

 

Using a framework/library like React, Vue, Next, etc.? 

CodePen isn't always ideal for these tools, so here are some Stackblitz starter templates that you can fork and import the gsap-trial NPM package for using any of the bonus plugins: 

 

Please share the StackBlitz link directly to the file in question (where you've put the GSAP code) so we don't need to hunt through all the files. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

 

Finally @Ihatetomatoes made an excellent set of videos on using GSAP with BarbaJS:

 

Happy Tweening!

Link to comment
Share on other sites

Hi,

 

On top of needing a minimal demo there is a clear difference between add and call methods:

https://gsap.com/docs/v3/GSAP/Timeline/call()

 

https://gsap.com/docs/v3/GSAP/Timeline/add()

 

In this case call is just executing the functions you pass to it, the call method doesn't really care about what's returned from the function, that's why you're seeing both animations happening at the same time, since they're executed one after the other almost immediately.

 

The add method on the other hand, as the name indicates, adds to the timeline what is returned from the callback (in case of being a function) or directly adding a label, a tween/timeline or a callback as well. In the case of being a GSAP Tween/Timeline it will add that instance's duration to the timeline so calling add twice should work in the way you expect:

master
  .add(blue, [])
  .add(pink, [])

Hopefully this helps.

Happy Tweening!

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