Jump to content
GreenSock

Returns : Timeline

A new timeline instance containing the root tweens and timelines


Details

Seamlessly transfers all tweens, timelines, and (optionally) delayed calls from the root timeline into a new timeline so that you can perform advanced tasks on a seemingly global basis without affecting tweens/timelines that you create after the export.

For example, imagine a game that uses the GSAP for all of its animations and at some point during the game, you want to slow everything down to a stop (tweening the timeScale) while at the same time animating a new popup window into place:

  1. var tl = gsap.exportRoot();
  2. gsap.to(tl, {duration: 0.5, timeScale: 0});
  3. //this tween isn't affected because it's created after the export.
  4. gsap.fromTo(myWindow, {scaleX: 0, scaleY: 0}, {duration: 1, scaleX: 1, scaleY: 1});

You could then re-animate things when you’re ready by tweening the timeScale back to 1. Or you could use exportRoot() to collect all the animations and pause() them and then animate the popup screen (or whatever). Then resume() that instance or even reverse().

You can exportRoot() as many times as you want; all it does is wrap all the loose tweens, timelines, and delayedCalls into a timeline which itself gets placed onto the root, so if you exportRoot() again, that timeline would get wrapped into another one, etc. Things can be nested as deeply as you want.

However, if you plan to call exportRoot() a lot (hundreds or thousands of times), it’d be wise to de-nest things from the previous export first to avoid a situation where the browser thinks there’s a recursion error (maximum callstack). You could do so like this (assumes we previously declared a variable named “allAnimations” to store the exported root):

  1. //De-nest things from the "allAnimations" exported root if one exists...
  2. if (allAnimations) {
  3. var animations = allAnimations.getChildren(),
  4. startTime = allAnimations.startTime(),
  5. root = allAnimations.timeline,
  6. l = animations.length,
  7. i;
  8. for (i = 0; i < l; i++) {
  9. root.add(animations[i], animations[i].startTime() - animations[i].delay() + startTime);
  10. }
  11. allAnimations.kill();
  12. }
  13. //now do the export
  14. allAnimations = gsap.exportRoot();

Note: completed tweens and timelines are removed from the root (for automatic garbage collection), so if you exportRoot() after a particular tween completes, it won’t be included in the export. The only way around that is to set autoRemoveChildren property of the Animation._rootTimeline to false, but that is NOT recommended because you’d need to manually kill() your tweens and timelines manually to make them eligible for garbage collection.

Copyright 2017, GreenSock. All rights reserved. This work is subject to theterms of useor for Club GreenSock members, the software agreement that was issued with the membership.
×