Tween Methods & Callbacks
Master GreenSock Animation Controls and Event Handling
Core GreenSock Concepts You'll Learn
Getter/Setter Methods
Master duration(), time(), and timeScale() methods for dynamic tween control. Learn to both retrieve and modify tween properties programmatically.
Event Callbacks
Implement onUpdate, onStart, onComplete, and other callbacks to execute custom functions at specific animation events.
Interactive Animation
Create responsive animations that react to user interactions like mouse events with proper speed and timing controls.
This exercise expands on the previous speed gauge example, adding numerical display and advanced interaction patterns. You'll implement dynamic value updates and variable playback speeds.
Animation Interaction Flow
Mouse Over
Speed gauge animates clockwise at normal speed while numbers increase from 0 to 750
Mouse Out
Animation reverses at 6x accelerated speed back to starting position
Click Interaction
Clicking restarts the entire animation from the beginning position instantly
All elements are now enclosed in a wrapper div that centers everything on the page. The div with an ID of numberDisplay has an initial value set to zero.
New JavaScript Variables
$numberDisplay
jQuery selector targeting the div where animated numbers appear in the speedometer interface.
displayValue
Holds the calculated value that gets displayed to users as the animation progresses.
finalValue
Set to 750, represents the maximum value the animation will reach at completion.
GSAP Getter vs Setter Methods
| Feature | Getter Usage | Setter Usage |
|---|---|---|
| duration() method | pointerTween.duration() | pointerTween.duration(10) |
| time() method | pointerTween.time() | pointerTween.time(0.5) |
| timeScale() method | pointerTween.timeScale() | pointerTween.timeScale(6) |
The time() method sets the playhead position along the timeline. Setting time(0.5) on a 1-second tween positions the animation halfway through its duration when the page loads.
Implementing onUpdate Callback
Add Callback to Tween
Include onUpdate:showValue in the tween properties object to trigger the function on every frame update
Create showValue Function
Define the custom function that will execute every time the tween updates, whether playing forward or reversing
Calculate Display Values
Use progress() method multiplied by finalValue to create meaningful numerical feedback for users
The progress() method returns a value between 0 and 1, making it perfect for calculating percentages and proportional values. Multiply by your target maximum for instant user feedback.
Complete TweenLite Callback Arsenal
onStart
Fires when the tween begins playing. Perfect for initialization logic or user interface updates at animation start.
onUpdate
Executes on every frame during animation. Essential for real-time value updates and progress indicators.
onComplete
Triggers when tween reaches its end. Ideal for chaining animations or cleanup operations.
onReverse
Fires when reverse() is called. Useful for handling backward animation states and UI adjustments.
onReverseComplete
Executes when reversed animation finishes. Perfect for resetting states after reverse completion.
Final Implementation Checklist
Verify 3-second duration with smooth number progression from 0 to 750
Animation should play 6x faster when reversing to starting position
Clicking should instantly reset pointer to start position and begin fresh animation
Combined play().timeScale(1) and reverse().timeScale(6) should execute smoothly
Implementing onUpdate Callback
Add Callback to Tween
Include onUpdate:showValue in the tween properties object to trigger the function on every frame update
Create showValue Function
Define the custom function that will execute every time the tween updates, whether playing forward or reversing
Calculate Display Values
Use progress() method multiplied by finalValue to create meaningful numerical feedback for users
The progress() method returns a value between 0 and 1, making it perfect for calculating percentages and proportional values. Multiply by your target maximum for instant user feedback.
Complete TweenLite Callback Arsenal
onStart
Fires when the tween begins playing. Perfect for initialization logic or user interface updates at animation start.
onUpdate
Executes on every frame during animation. Essential for real-time value updates and progress indicators.
onComplete
Triggers when tween reaches its end. Ideal for chaining animations or cleanup operations.
onReverse
Fires when reverse() is called. Useful for handling backward animation states and UI adjustments.
onReverseComplete
Executes when reversed animation finishes. Perfect for resetting states after reverse completion.
Final Implementation Checklist
Verify 3-second duration with smooth number progression from 0 to 750
Animation should play 6x faster when reversing to starting position
Clicking should instantly reset pointer to start position and begin fresh animation
Combined play().timeScale(1) and reverse().timeScale(6) should execute smoothly
Key Takeaways
