The GreenSock Animation Platform, commonly called GSAP, is a widely popular JavaScript library for creating smooth and dynamic animations on the web. One notable aspect of GSAP is its Timeline functionality. This powerful component enables precise and efficient coordination of intricate animations. In this article, we will explore the fundamental concept of the GSAP timeline and how it simplifies the process of sequencing and managing animations in web development.
A GSAP Timeline is a convenient container for grouping animations, allowing users to regulate them collectively. It simplifies the management of animation sequences by eliminating the need for separate delays at each step. Without Timelines, orchestrating a series of animations would become more complex and require individual delay for each animation. For instanc
// WITHOUT Timelines (only using tweens with 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
If the first animation needs to be longer, subsequent delays will need adjustment. Furthermore, if pausing(), restart(), or reverse() the entire sequence is desired, it could lead to complications. However, with GSAP’s Timelines, it's much more straightforward.
//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});
Now, with timelines, controlling the entire sequence is made easy. By adjusting the timing, everything is automatically readjusted without the need for modifying individual delays. Additionally, with just a single line of code, you can pause(), resume(), seek(), and
reverse() the whole sequence.
The default behavior of the GSAP Timeline involves adding animations at the end of the timeline, resulting in a sequential arrangement of animations. However, users have the flexibility to precisely control the placement of these animations by utilizing the position parameter. This parameter, located after the vars parameter, offers various options for precise positioning.
Absolute Time: Absolute time (in seconds) refers to a specific numerical value used to determine the exact moment an animation should be inserted within a timeline. To illustrate, if you want to insert an animation precisely 5 seconds after the beginning of the timeline, you can employ code such as:
tl.to(".class", { x: 100 }, 5);
Labels: Labels can be used to indicate the specific location for adding an animation. If the label doesn't exist, it will automatically be placed at the end of the timeline. For instance, in `tl.to(".class", { x: 100 }, "someLabel");`, the animation will be inserted at the designated spot marked by the "someLabel" label.
"<" and ">" are symbols used as pointers in animations. They indicate the start and end of the previous animation, respectively. When you use "<", it means inserting an animation at the beginning of the previous one. On the other hand, using ">" places it at the end of the previous animation.
tl.to(".class", { x: 100 }, "<");tl.to(".class", { x: 100 }, ">");
When using "+=" and "-=" in your code, you can easily establish relative values. For instance, if you use "+=1", it signifies one second after the timeline ends, creating a gap. On the other hand, "-=1" indicates one second before the end, causing an overlap. Similarly, by using "myLabel+=2", you indicate two seconds after the label "myLabel". Additionally, "<+=3" denotes three seconds after the start of the previous animation.
In GSAP version 3.7.0, a new feature called percentage-based values was introduced. These values allow you to determine positions in relation to the animation's total duration using percentages. For instance, if you enter "+=50%", it means moving beyond the end of the GSAP timeline by 50% of the animation's total duration.
These parameters and callbacks offer advanced control and customization options for your GSAP Timeline animation. They enable you to effortlessly create dynamic and interactive web animations, enhancing the functionality of your animations. By adding these properties and callbacks to the vars object, you can enhance the overall experience of your web animations.
gsap.timeline({ onComplete: myFunction, // Calls a function when the animation completes repeat: 2, // Specifies the number of times the animation should repeat after its first iteration repeatDelay: 1, // Determines the time in seconds between each repeat of the animation yoyo: true, // Causes the animation to alternate direction on each repeat, creating a back-and-forth effect});
Property | Description |
autoRemoveChildren | When autoRemoveChildren is assigned to true, this property automatically kills or removes child tweens/timelines as soon as they are complete. While it can enhance speed and memory management, it's generally not recommended as it hinders the ability to go backward in time (e.g., using reverse() or setting the progress lower). By default, the root timelines have autoRemoveChildren set to true for better memory management. |
callbackScope | The property 'callbackScope' determines the scope for various callbacks such as onStart, onUpdate, and onComplete. It allows precise control over the context within which these callbacks are executed by specifying what 'this' refers to. This ensures a clear understanding of the execution context and enables accurate management of callback behaviors. |
delay | It represents the duration in seconds that the GSAP animation should wait before starting. |
onComplete | It's a function which gets executed when the animation is finished. |
onCompleteParams | Passes parameter arrays to the function “onComplete” for customization. For instance: gsap.timeline({onComplete: myFunction, onCompleteParams: ["param1", "param2"]});. |
onInterrupt | This property specifies as a function which gets called when the animation is interrupted. It will not trigger if the animation completes normally. |
onInterruptParams | The onInterruptParams property allows you to provide the parameters arrays for the onInterrupt function. This is useful when passing specific values to the onInterrupt function. For example: gsap.to(".class", {x:100, onInterrupt:myFunction, onInterruptParams:["param1", "param2"]});. |
onRepeat | It's a function, gets called every time the animation repeats. |
onRepeatParams | This property allows for the acceptance of parameters arrays that can be passed to the onRepeat function. An illustrative example is gsap.timeline({onRepeat: myFunction, onRepeatParams: ["param1", "param2"]}); |
onReverseComplete | The onReverseComplete property defines a function triggers when an animation returns to its initial state from the reverse direction. This occurs, for example, when the reverse() method is called and the animation moves backwards towards the beginning. Once the animation's time reaches 0, the onReverseComplete function is activated. The same can happen if the GSAP animation is included in a timeline instance that is reversed and plays backward to or beyond its starting point. |
onReverseCompleteParams | The property "onReverseCompleteParams" accepts parameters arrays that are passed to the function "onReverseComplete". For instance:gsap.timeline({onReverseComplete: myFunction, onReverseCompleteParams: ["param1", "param2"]}); |
onStart | This property signifies the function invoked when the animation initiates, triggered by a change in the animation's time from 0 to another value. If the tween is restarted multiple times, this event can occur repeatedly. |
onStartParams | The property "onStartParams" allows you to provide parameters arrays that will be passed to the onStart function. gsap.timeline({onStart: myFunction, onStartParams: ["param1", "param2"]});. |
onUpdate | This property specifies a function that gets called with each animation update. The update occurs on each frame when the animation is functional/active. |
onUpdateParams | The property allows you to pass parameters arrays to the “onUpdate” function. This is useful for providing additional information or data that need to be processed within the function. For instance, gsap.timeline({onUpdate: myFunction, onUpdateParams: ["param1", "param2"]});. |
paused | This property is a boolean that, if set to true, causes the animation to pause immediately upon its creation. |
repeat | This property indicates how many times the animation should repeat after its initial iteration. For example, if the repeat value is set to 1, the animation will play a total of two times (the original play plus one repetition). To repeat indefinitely, you can use -1. It is important to note that the repeat value must always be an integer. |
repeatDelay | The repeatDelay property represents the duration, in seconds, between each repetition of the animation. For instance, if the repeat is set to 2 and repeatDelay is 1, the animation will play initially and then wait for 1 second before repeating. It will play again, followed by another 1-second delay before the final repetition. |
repeatRefresh | When the repeatRefresh property is assigned to true, it triggers the invalidation of all child tweens within a repeating timeline. This causes their starting and ending values to be re-recorded internally on each full iteration, except for yoyos. This feature proves particularly beneficial when dealing with dynamic values like relative, random, or function-based ones. For example, if you use x: "random(-100, 100)", it will generate a new random x value with each repeat. It's important to note that properties such as duration, delay, and stagger do not refresh alongside this process. |
smoothChildTiming | The smoothChildTiming property allows for automatic adjustment of child animations' startTime to ensure seamless playback when timing-related properties are modified on the processing. When enabled (set to true), it repositions the child animations accordingly. Let's consider a scenario: imagine the timeline's playhead is currently at 75% completion of a child tween, which moves an element to 100 from 0, and then the reverse() method is triggered. By default (when smoothChildTiming is assigned to false, except for the globalTimeline), the tween would reverse in place while maintaining its startTime consistency. Consequently, instead of being at the expected 25% point of completion, the timeline's playhead would remain at 75%. |
yoyo | When set to true, every other repetition cycle will run in the opposite direction, resulting in a back-and-forth effect for the tween animation (moving forward and then backward). It's worth noting that this property does not impact the 'reversed' property. For example, if the repeat count is 2 and 'yoyo' is set to false, the sequence would appear as: start - 1 - 2 - 3 - 1 - 2 - 3 - 1 - 2 - 3 - end. However, when 'yoyo' is assigned to true, the sequence would be: start - 1 - 2 -3-3-2-1-1-2-3-end. |
Defaults | Anything that is defined in the defaults object of a timeline will be inherited by its child animations upon creation. This means that if you often find yourself repeatedly setting the same properties such as ease or duration, utilizing defaults can greatly improve the conciseness and efficiency of your code. |
Nesting | Nesting timelines allows for the nesting of timelines within other timelines to any desired level of depth. This feature enhances code modularity and improves maintainability, leading to more organized and manageable animations. |
In animations, whether they are Tweens or Timelines, a parent Timeline contains each animation. These animations have their playheads that indicate their position in time (referred to as "totalTime," or "time", including repeats and repeatDelays). When the parent Timeline shifts to a new place, it synchronizes the playheads of its children unless a child is paused.
If a timeline is rendered at a particular point , it instructs its children to render based on their playhead position. If a child is also a timeline with its children, it follows the same process for its children, resulting in a cascading effect that ensures synchronized playheads.
When an animation is unpaused (via “resume()” or “play()”), it resumes from where it left off, synchronized with its parent's timeline. This synchronization ensures smooth playback. However, if the parent timeline's “smoothChildTiming” is set to false, the child animation remains stationary at its original starting point.
When you reverse an animation or change its timeScale, the “startTime” of the animation automatically adjusts to maintain synchronization with the parent timeline's playhead. This adjustment happens seamlessly and ensures smooth playback for a flawless experience.
In this particular scenario, imagine you have a 10 sec tween on the timeline of root. Suppose you're already 2 sec to the tween and decide to change its position using the “seek(5)”. Here's where it gets interesting: despite making this adjustment, the root timeline playhead remains unaffected. To ensure that the tween jumps to 5 sec and plays correctly, we need to modify its “startTime” parameter by -3. By doing so, we align the playheads of both the tween and root timeline perfectly.
The GSAP Timeline is a powerful feature that simplifies the coordination of complex animations in web development. By grouping animations and offering precise control over their sequencing, it streamlines the process of managing animations. With its easy-to-use sequence control, effortless timing adjustments, and precise positioning within the timeline, GSAP becomes an indispensable asset for crafting captivating and dynamic web animations.
Wеlcomе to Scribblеrsdеn: Whеrе Words Comе to Lifе! Wе arе a vibrant community of writеrs, making languagе fun and forging connеctions. Explorе our world of crеativity today!
Recommended For You.....
Subscribe Us
Always Get Notified
Alert
Leave a Reply