GSAP - Timelines: Unterschied zwischen den Versionen

Aus Wikizone
Wechseln zu: Navigation, Suche
 
(5 dazwischenliegende Versionen von 2 Benutzern werden nicht angezeigt)
Zeile 1: Zeile 1:
 +
https://greensock.com/docs/v3/GSAP/gsap.timeline()
 
  https://greensock.com/forums/topic/12643-master-timeline-and-the-use-of-functions/
 
  https://greensock.com/forums/topic/12643-master-timeline-and-the-use-of-functions/
== Master Timeline ==
 
Gutes Vorgehen für flexiblen Einsatz:
 
  
Celli, I read you question a little differently. it seems you are already using a modular approach, it's just that you are doing
+
<syntaxhighlight lang="javascript">
 +
// WITHOUT Timelines (only using delays):
 +
gsap.to("#id", { x: 100, duration: 1 });
 +
gsap.to("#id", { y: 50, duration: 1, delay: 1 }); //wait 1 second
 +
gsap.to("#id", { opacity: 0, duration: 1, delay: 2 }); //wait 2 seconds
 +
 
  
<syntaxhighlight lang="javascript">
+
//WITH Timelines (cleaner, more versatile)
var first = new TImelineLite();
+
var tl = gsap.timeline({repeat: 2, repeatDelay: 1});
var second = new TimelineLite();
+
tl.to("#id", {x: 100, duration: 1});
var third = new TimelineLite();
+
tl.to("#id", {y: 50, duration: 1});
 +
tl.to("#id", {opacity: 0, duration: 1});
  
master.add(first);
+
// then we can control the whole thing easily...
master.add(second);
+
tl.pause();
master.add(third);
+
tl.resume();
 +
tl.seek(1.5);
 +
tl.reverse();
 
</syntaxhighlight>
 
</syntaxhighlight>
  
and want to know why its better to do
+
== Master Timeline ==
 
+
Gutes Vorgehen für flexiblen Einsatz:
 +
Ältere Beispiele (v2)
 
<syntaxhighlight lang="javascript">
 
<syntaxhighlight lang="javascript">
 
function getFirst() {
 
function getFirst() {

Aktuelle Version vom 19. April 2024, 14:03 Uhr

https://greensock.com/docs/v3/GSAP/gsap.timeline()
https://greensock.com/forums/topic/12643-master-timeline-and-the-use-of-functions/
// WITHOUT Timelines (only using delays):
gsap.to("#id", { x: 100, duration: 1 });
gsap.to("#id", { y: 50, duration: 1, delay: 1 }); //wait 1 second
gsap.to("#id", { opacity: 0, duration: 1, delay: 2 }); //wait 2 seconds


//WITH Timelines (cleaner, more versatile)
var tl = gsap.timeline({repeat: 2, repeatDelay: 1});
tl.to("#id", {x: 100, duration: 1});
tl.to("#id", {y: 50, duration: 1});
tl.to("#id", {opacity: 0, duration: 1});

// then we can control the whole thing easily...
tl.pause();
tl.resume();
tl.seek(1.5);
tl.reverse();

Master Timeline[Bearbeiten]

Gutes Vorgehen für flexiblen Einsatz: Ältere Beispiele (v2)

function getFirst() {
  var tl = new TimelineLite();
  tl.to(...)
  return tl;
}

function getSecond() {
  var tl = new TimelineLite();
  tl.to(...)
  return tl;
}

function getThird() {
  var tl = new TimelineLite();
  tl.to(...)
  return tl;
}

master.add(getFirst());
master.add(getSecond());
master.add(getThird());

The end result of both techniques is going to be identical and both offer a modular approach. However, wrapping your timelines in a function has two advantages which may not be applicable all the time, but its nice to have them:)

1: By using functions to create your timelines, you can create them "on-demand" only when needed. In the context of a large game or application the function-wrapped way means you don't have to create 500 timelines in advance, you can just call the proper function when you need a timeline.

2: You can create the same timeline multiple times and add each unique copy of the timeline to the same or any parent timeline. Although it isn't always necessary, it is very easy to use the function-wrapped technique to do

master.add(getFirst());
master.add(getFirst());
master.add(getFirst());
master.add(getSecond());
master.add(getSecond());

When it comes to doing elaborate, dynamic scene changes (based on user interaction) it makes things incredibly flexible.

Imagine your app has a variety of scenes, and each scene has its own timeline for animating in and out.

You could do something like:

navTimeline.add(scene1Out());
navTimeline.add(scene3In(), "-=1");

which would trigger the timeline that the scene3In() function generates 1 second before the scene1Out animation ends.

and then maybe later the user wants to go back to scene1

navTimeline.add(scene3Out());
navTimeline.add(scene1In(), "-=1");

The end result of this technique is that you can generate your transition timelines whenever you need them, as many times as you need them, and glue them together in any number of ways.


function carMove(){
var tl = new TimelineMax();
tl.to("#car", 2, {x:"+=200"})
/* more tweens go here */
return tl;
}

function wheelsRotate(){
var tl = new TimelineMax();
tl.to("#wheels", 2, {rotation:"+=360"})
/* more tweens go here */
return tl;
}

masterTL
.add( carMove())
.add( wheelsRotate());