GSAP (GreenSock Animation Platform)
Overview
GSAP is a robust, blazingly fast JavaScript animation library that lets you animate anything (CSS properties, SVG, canvas, React components). It's built for modern web performance and handles cross-browser inconsistencies out of the box.
Core components:
- gsap.to(), gsap.from(), gsap.fromTo(): Basic tweens.
- Timeline:
gsap.timeline() to sequence multiple tweens.
- Plugins: Add extra capabilities (e.g.,
ScrollTrigger for scroll animations, Flip for FLIP animations).
Installation
Reference: GSAP docs, React guide
| Use case |
Packages |
| Core |
gsap |
| React / Next.js (recommended) |
gsap, @gsap/react |
Add via your package manager (see npm). GSAP provides a dedicated React hook (useGSAP) for cleanup and context management.
Core Usage
Basic Tweens
import gsap from "gsap";
// Animate to a state
gsap.to(".box", { x: 100, duration: 1, ease: "power2.inOut" });
// Animate from a state
gsap.from(".box", { opacity: 0, y: 50, duration: 1, stagger: 0.1 });
Timelines
Timelines are perfect for choreographing sequences.
const tl = gsap.timeline({ repeat: -1, yoyo: true });
tl.to(".box1", { x: 100, duration: 1 })
.to(".box2", { y: 50, duration: 0.5 }, "-=0.5") // Start 0.5s early
.to(".box3", { rotation: 360 });
Eases
Customize the feel of your animations.
- Standard:
"none", "power1" to "power4", "back", "bounce", "circ", "elastic", "expo", "sine".
- Add
.in, .out, or .inOut (e.g., "power2.inOut").
React Usage (useGSAP)
When using React (or Next.js), always use the @gsap/react package and its useGSAP hook instead of standard useEffect. It automatically handles cleanup, preventing memory leaks and strict-mode double-firings.
import { useRef } from "react";
import gsap from "gsap";
import { useGSAP } from "@gsap/react";
gsap.registerPlugin(useGSAP);
export default function App() {
const container = useRef<HTMLDivElement>(null);
useGSAP(() => {
// gsap code here...
// All animations created here are automatically reverted on cleanup!
gsap.to(".box", { x: 360, duration: 2 });
}, { scope: container }); // Scope allows selecting elements only inside this container
return (
<div ref={container}>
<div className="box">Box</div>
</div>
);
}
Popular Plugins
Plugins extend GSAP's core functionality. They must be registered before use.
ScrollTrigger
Animate elements on scroll, pin sections, or link animations to the scrollbar.
import gsap from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
gsap.registerPlugin(ScrollTrigger);
gsap.to(".box", {
scrollTrigger: {
trigger: ".box",
start: "top center", // When top of element hits center of viewport
end: "bottom top",
scrub: true, // Tie animation strictly to scrollbar
pin: true,
},
x: 500,
});
Other Notable Plugins:
- Flip: Seamlessly animate elements changing state/layout.
- Observer: Normalize events (scroll, touch, pointer) for intent-based interactions.
- Draggable: Make elements draggable, spinnable, or tossable.
- SplitText: Split HTML text into characters, words, or lines for granular animation (Premium).
- DrawSVG: Intuitively animate SVG strokes (Premium).
- MorphSVG: Morph any SVG path into another (Premium).
Common Patterns & Best Practices
- Registration: Always register plugins up front (e.g.,
gsap.registerPlugin(ScrollTrigger, useGSAP)).
- React strict mode: Rely on
useGSAP() to avoid duplicate animations created by double invocation of useEffect in React 18 strict mode.
- Transforms: Use GSAP shortcuts (
x, y, rotation, scale, autoAlpha) instead of standard transform or opacity strings for better performance and accuracy.
- FOUC (Flash of Unstyled Content): For initial load animations, hide elements with CSS (
visibility: hidden, not display: none) and use gsap.from() or gsap.set(..., { autoAlpha: 0 }).
Additional Resources
1---2name: gsap3description: Install and use GSAP (GreenSock Animation Platform) for high-performance JavaScript animations. Covers core utilities (Tween, Timeline), React integration (@gsap/react/useGSAP), Eases, and popular plugins like ScrollTrigger, ScrollSmoother, and Flip. Use when creating complex animations, scroll-based effects, UI transitions, or SVG animations, or when the user mentions GSAP, GreenSock, or ScrollTrigger.4---5
6# GSAP (GreenSock Animation Platform)
7
8## Overview
9
10GSAP is a robust, blazingly fast JavaScript animation library that lets you animate anything (CSS properties, SVG, canvas, React components). It's built for modern web performance and handles cross-browser inconsistencies out of the box.
11
12**Core components**:
13- **gsap.to(), gsap.from(), gsap.fromTo()**: Basic tweens.
14- **Timeline**: `gsap.timeline()` to sequence multiple tweens.
15- **Plugins**: Add extra capabilities (e.g., `ScrollTrigger` for scroll animations, `Flip` for FLIP animations).
16
17---
18
19## Installation
20
21Reference: [GSAP docs](https://gsap.com/docs/v3/), [React guide](https://gsap.com/resources/React)
22
23| Use case | Packages |
24|----------|----------|
25| Core | `gsap` |
26| React / Next.js (recommended) | `gsap`, `@gsap/react` |
27
28Add via your package manager (see [npm](https://www.npmjs.com/package/gsap)). GSAP provides a dedicated React hook (`useGSAP`) for cleanup and context management.
29
30---
31
32## Core Usage
33
34### Basic Tweens
35```javascript
36import gsap from "gsap";
37
38// Animate to a state
39gsap.to(".box", { x: 100, duration: 1, ease: "power2.inOut" });
40
41// Animate from a state
42gsap.from(".box", { opacity: 0, y: 50, duration: 1, stagger: 0.1 });
43```
44
45### Timelines
46Timelines are perfect for choreographing sequences.
47```javascript
48const tl = gsap.timeline({ repeat: -1, yoyo: true });
49
50tl.to(".box1", { x: 100, duration: 1 })
51 .to(".box2", { y: 50, duration: 0.5 }, "-=0.5") // Start 0.5s early
52 .to(".box3", { rotation: 360 });
53```
54
55### Eases
56Customize the feel of your animations.
57- **Standard**: `"none"`, `"power1"` to `"power4"`, `"back"`, `"bounce"`, `"circ"`, `"elastic"`, `"expo"`, `"sine"`.
58- Add `.in`, `.out`, or `.inOut` (e.g., `"power2.inOut"`).
59
60---
61
62## React Usage (`useGSAP`)
63
64When using React (or Next.js), **always** use the `@gsap/react` package and its `useGSAP` hook instead of standard `useEffect`. It automatically handles cleanup, preventing memory leaks and strict-mode double-firings.
65
66```tsx
67import { useRef } from "react";
68import gsap from "gsap";
69import { useGSAP } from "@gsap/react";
70
71gsap.registerPlugin(useGSAP);
72
73export default function App() {
74 const container = useRef<HTMLDivElement>(null);
75
76 useGSAP(() => {
77 // gsap code here...
78 // All animations created here are automatically reverted on cleanup!
79 gsap.to(".box", { x: 360, duration: 2 });
80 }, { scope: container }); // Scope allows selecting elements only inside this container
81
82 return (
83 <div ref={container}>
84 <div className="box">Box</div>
85 </div>
86 );
87}
88```
89
90---
91
92## Popular Plugins
93
94Plugins extend GSAP's core functionality. They must be registered before use.
95
96### ScrollTrigger
97Animate elements on scroll, pin sections, or link animations to the scrollbar.
98```javascript
99import gsap from "gsap";
100import { ScrollTrigger } from "gsap/ScrollTrigger";
101
102gsap.registerPlugin(ScrollTrigger);
103
104gsap.to(".box", {
105 scrollTrigger: {
106 trigger: ".box",
107 start: "top center", // When top of element hits center of viewport
108 end: "bottom top",
109 scrub: true, // Tie animation strictly to scrollbar
110 pin: true,
111 },
112 x: 500,
113});
114```
115
116### Other Notable Plugins:
117- **Flip**: Seamlessly animate elements changing state/layout.
118- **Observer**: Normalize events (scroll, touch, pointer) for intent-based interactions.
119- **Draggable**: Make elements draggable, spinnable, or tossable.
120- **SplitText**: Split HTML text into characters, words, or lines for granular animation (Premium).
121- **DrawSVG**: Intuitively animate SVG strokes (Premium).
122- **MorphSVG**: Morph any SVG path into another (Premium).
123
124---
125
126## Common Patterns & Best Practices
127
128- **Registration**: Always register plugins up front (e.g., `gsap.registerPlugin(ScrollTrigger, useGSAP)`).
129- **React strict mode**: Rely on `useGSAP()` to avoid duplicate animations created by double invocation of `useEffect` in React 18 strict mode.
130- **Transforms**: Use GSAP shortcuts (`x`, `y`, `rotation`, `scale`, `autoAlpha`) instead of standard `transform` or `opacity` strings for better performance and accuracy.
131- **FOUC (Flash of Unstyled Content)**: For initial load animations, hide elements with CSS (`visibility: hidden`, not `display: none`) and use `gsap.from()` or `gsap.set(..., { autoAlpha: 0 })`.
132
133---
134
135## Additional Resources
136
137- [reference.md](reference.md) — Official GSAP docs, core API, plugins, React integration — indexable
138- Official Docs: https://gsap.com/docs/v3/
139- React Guide: https://gsap.com/resources/React
140- ScrollTrigger: https://gsap.com/docs/v3/Plugins/ScrollTrigger