Lesson 18 / GSAP
GSAP: Web Animation
Use GSAP for performant tweens, timelines, easing, plugins, and more sophisticated web animation.
Teacher Notes / In-Class Use
Demo Live
- Walk through the interactive demo before students start changing their own project files.
- Connect the demo back to the first goal: Explain why GSAP is useful for complex web animation
Try In Class
- Move an element across the screen with GSAP.
- Have students make one visible change, save, refresh, and explain what changed.
Submit Or Check
- Ask students to show the work in the browser, not only in the editor.
- Have students commit their progress with a clear message when the checkpoint is stable.
Watch For
- Students copying code without checking file paths, spelling, or capitalization.
- Visual changes that work locally but break when the project is published.
Learning Goals
- Explain why GSAP is useful for complex web animation
- Create tweens and sequence them with timelines
- Use easing, callbacks, repeat, yoyo, and staggered animations
- Optimize animations with transform-based movement
Interactive Demo
How to use this demo.
Use the demo as a small lab. Change one thing, observe the result, then connect it back to your own project.
What To Try
- Change duration, stagger, and easing before replaying the timeline.
- Compare how CSS-like motion decisions feel when controlled as a sequence.
What Changes
- Multiple elements animate as part of one coordinated timeline.
- The code sample updates with the timeline settings.
What To Notice
- GSAP is useful when motion needs sequencing and control beyond a single CSS transition.
- Staggering can make grouped elements feel organized instead of noisy.
Apply It
- Identify one place where a sequence would communicate better than everything moving at once.
Interactive Demo
GSAP Timeline Lab
Adjust the timeline settings and replay the sequence. The preview uses plain JavaScript, while the code shows the GSAP timeline you would write.
duration: 0.6s / stagger: 0.2s / ease: power2.out
const timeline = gsap.timeline();
timeline.from('.item', {
duration: 0.6,
opacity: 0,
y: 32,
stagger: 0.2,
ease: 'power2.out',
}); What to notice
- A timeline coordinates multiple animations as one sequence.
- Stagger creates a delay between similar elements.
- Ease changes the feel without changing the layout.
Try this
- Set stagger to 0, then replay the timeline.
- Compare power2.out with elastic.out.
- Copy the GSAP snippet into a page that loads GSAP.
This demo uses extra JavaScript for teaching. The code sample shows the pattern to practice. View full demo source (opens in a new tab).
Introduction
As UX developers, we know that animation isn't just about flashy effects. It's about bringing our websites to life, creating engaging user experiences, and adding that extra touch of polish that captivates our audience.
When it comes to web animation, GSAP is a powerful tool of choice.
Think of GSAP as your animation playground, a place where you can unleash your creativity and bring complex animation ideas to reality.
With its intuitive syntax, powerful features, and strong performance, GSAP helps create smooth, sophisticated, and performant animations.
Why GSAP Rocks
- Unmatched Performance: GSAP is known for smooth and fluid animations, even on complex projects.
- Cross-Browser Compatibility: GSAP works across major browsers, reducing browser inconsistency headaches.
- Intuitive Syntax: GSAP's syntax is clean, concise, and easy to learn.
- Powerful Features: GSAP offers timelines, easing functions, plugins, and more.
- Active Community: GSAP has a supportive community with resources, tutorials, and inspiration.
Getting Started with GSAP
Include GSAP in your project by downloading it from the GSAP website or using a CDN.
Use CSS selectors to target the elements you want to animate.
Create a tween. A tween is the core animation unit in GSAP. It defines the properties you want to animate and the duration of the animation.
This animates the element with the class "my-element" to move 100 pixels to the right and rotate 360 degrees over 1 second.
<script src="https://cdn.jsdelivr.net/npm/gsap@3/dist/gsap.min.js"></script>
<div class="my-element">Animate me</div>
<script>
gsap.to('.my-element', {
duration: 1,
x: 100,
rotation: 360,
});
</script> Core concepts
- Tweens: The basic animation unit in GSAP.
- Timelines: A powerful tool for sequencing and controlling multiple tweens.
- Easing Functions: Control the speed and acceleration of animations.
- Plugins: Extend GSAP's functionality with features like scroll triggering and morphing.
Timelines: Orchestrating Animations
Timelines allow you to sequence and control multiple tweens, creating complex animation sequences.
const timeline = gsap.timeline();
timeline
.to('.box', { duration: 0.5, x: 100 })
.to('.box', { duration: 0.5, rotation: 180 })
.to('.box', { duration: 0.5, scale: 1.2 }); Easing Functions: Adding Personality
Easing functions control the speed and acceleration of animations, adding personality and realism.
Use the GSAP Ease Visualizer for precise ease functions.
gsap.to('.my-element', {
duration: 1,
x: 100,
ease: 'elastic.out(1, 0.3)',
}); Plugins: Expanding GSAP's Capabilities
- ScrollTrigger: Animate elements based on scroll position.
- MorphSVG: Morph SVG shapes into other shapes.
- SplitText: Break text into individual div elements.
- DrawSVG: Reveal SVG strokes progressively.
- Visit the GSAP Plugins Page for more.
Real-World Examples
- Interactive Hero Sections
- Smooth Page Transitions
- Interactive Data Visualizations
- Micro-Interactions
Code Challenge
Create a simple animation that moves an element across the screen, changes its color, and rotates it using GSAP.
Use a timeline and different easing functions.
Advanced Tweening
Sometimes, you want an animation to start from a state that's different from the element's current state. That's where gsap.from() and gsap.fromTo() come in.
From Tweens
- gsap.from(): Animates an element from a specified state to its current state.
- gsap.fromTo(): Animates an element from one specified state to another specified state.
gsap.from('.card', {
duration: 0.6,
opacity: 0,
y: 30,
});
gsap.fromTo(
'.button',
{ scale: 0.8, opacity: 0 },
{ duration: 0.4, scale: 1, opacity: 1 }
); Staggered Tweens
Staggered tweens allow you to animate multiple elements with a delay between each animation, creating visually appealing sequenced effects.
gsap.to('.gallery-item', {
duration: 0.4,
opacity: 1,
y: 0,
stagger: 0.1,
}); Callback Tweens
Callbacks allow you to trigger functions at different stages of an animation, such as when it starts, updates, or completes.
gsap.to('.notification', {
duration: 0.3,
y: 0,
opacity: 1,
onStart: () => console.log('Animation started'),
onComplete: () => console.log('Animation complete'),
}); Repeat and Yoyo
The repeat and yoyo properties allow you to create looping and back-and-forth animations.
- repeat: Specifies the number of times the animation should repeat.
- yoyo: Makes the animation play in reverse on alternate repeats.
gsap.to('.pulse', {
duration: 0.8,
scale: 1.15,
repeat: -1,
yoyo: true,
ease: 'sine.inOut',
}); Overwrite Manager
The overwrite manager handles conflicting tweens, ensuring that only the most recent tween affects an element's properties.
gsap.to('.box', {
duration: 1,
x: 200,
overwrite: 'auto',
}); Performance Optimization
When animating elements, prioritize using transform properties such as translateX, translateY, scale, and rotate over top, left, bottom, and right.
Transform properties are handled more efficiently by browsers and often result in smoother animations.
Animating top, left, and similar layout properties can trigger layout recalculations, which can be expensive and lead to janky animations.
// Good: using transform
gsap.to('.element', { duration: 1, x: 100 });
// Avoid: animating layout position
gsap.to('.element', { duration: 1, left: 100 }); Force3D: Encouraging GPU Rendering
In some cases, the browser may not automatically use hardware acceleration for transform properties. You can encourage GPU rendering by setting force3D: true in your tween.
Use force3D sparingly. It can sometimes have the opposite effect and hurt performance.
gsap.to('.element', {
duration: 1,
x: 100,
force3D: true,
}); Will-Change: Informing the Browser of Upcoming Changes
The will-change CSS property informs the browser that an element's properties are about to change.
This allows the browser to optimize the element for animation, improving performance.
Use will-change carefully. It can consume memory and degrade performance if overused. Remove will-change after the animation is complete.
.element {
will-change: transform;
} Checkpoint
Before moving on, make sure these feel true.
- I can explain the main concept in my own words.
- I can apply this lesson to my current project.
- I can verify the result in the browser.
- I can commit the change with a clear message.
Project Connection
This lesson supports current class projects.
Practice
- Move an element across the screen with GSAP.
- Change its color and rotate it during the same sequence.
- Use a timeline with at least three tweens.
- Try different easing functions and explain the difference.
Resources
- GSAP CDN
- GSAP Ease Visualizer
- GSAP Plugins Page