| name | gsap-setup |
| description | Use when setting up GSAP in a project, installing GSAP, registering plugins, configuring defaults, or implementing GSAP best practices and workflows. |
GSAP Setup and Best Practices
Proper GSAP setup ensures smooth development, optimal performance, and maintainable code. Learn installation, plugin registration, configuration, and industry best practices.
Installation
NPM Installation
npm install gsap
npm install @gsap/react
CDN Installation
<script src="https://cdn.jsdelivr.net/npm/gsap@3.13.0/dist/gsap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gsap@3.13.0/dist/ScrollTrigger.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gsap@3.13.0/dist/Draggable.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gsap@3.13.0/dist/Flip.min.js"></script>
ES Modules (CDN)
<script type="module">
import gsap from 'https://cdn.jsdelivr.net/npm/gsap@3.13.0/index.js'
import { ScrollTrigger } from 'https://cdn.jsdelivr.net/npm/gsap@3.13.0/ScrollTrigger.js'
gsap.registerPlugin(ScrollTrigger)
</script>
Project Setup
Basic Setup
import gsap from 'gsap'
import { ScrollTrigger } from 'gsap/ScrollTrigger'
import { Draggable } from 'gsap/Draggable'
gsap.registerPlugin(ScrollTrigger, Draggable)
gsap.defaults({
ease: 'power2.out',
duration: 1
})
ScrollTrigger.config({
ignoreMobileResize: true,
autoRefreshEvents: 'visibilitychange,DOMContentLoaded,load'
})
export { gsap }
Advanced Setup
import gsap from 'gsap'
import { ScrollTrigger } from 'gsap/ScrollTrigger'
import { Observer } from 'gsap/Observer'
import { Draggable } from 'gsap/Draggable'
import { Flip } from 'gsap/Flip'
import { ScrollToPlugin } from 'gsap/ScrollToPlugin'
import { SplitText } from 'gsap/SplitText'
import { MorphSVGPlugin } from 'gsap/MorphSVGPlugin'
import { MotionPathPlugin } from 'gsap/MotionPathPlugin'
import { DrawSVGPlugin } from 'gsap/DrawSVGPlugin'
import { ScrambleTextPlugin } from 'gsap/ScrambleTextPlugin'
import { CustomEase } from 'gsap/CustomEase'
{ }
gsap.(
,
,
,
,
,
,
,
,
,
,
,
)
gsap.({
: ,
: ,
:
})
gsap..(, )
.(, )
gsap
Plugin Registration
Register Plugins
import gsap from 'gsap'
import { ScrollTrigger } from 'gsap/ScrollTrigger'
import { Draggable } from 'gsap/Draggable'
gsap.registerPlugin(ScrollTrigger, Draggable)
gsap.registerPlugin(ScrollTrigger)
gsap.registerPlugin(Draggable)
Register with React Hook
import gsap from 'gsap'
import { useGSAP } from '@gsap/react'
import { ScrollTrigger } from 'gsap/ScrollTrigger'
gsap.registerPlugin(useGSAP, ScrollTrigger)
Conditional Registration
import gsap from 'gsap'
const useScrollTrigger = true
const useDraggable = false
if (useScrollTrigger) {
import('gsap/ScrollTrigger').then(({ ScrollTrigger }) => {
gsap.registerPlugin(ScrollTrigger)
})
}
if (useDraggable) {
import('gsap/Draggable').then(({ Draggable }) => {
gsap.registerPlugin(Draggable)
})
}
Global Configuration
Defaults
gsap.defaults({
ease: 'power2.out',
duration: 1,
delay: 0,
overwrite: 'auto',
lazy: true
})
gsap.to('.box', {
x: 200,
duration: 2,
ease: 'back.out(1.7)'
})
Timeline Defaults
const tl = gsap.timeline({
defaults: {
ease: 'power2.out',
duration: 1
},
paused: true,
repeat: -1,
yoyo: true
})
tl.to('.box1', { x: 100 })
.to('.box2', { x: 200, ease: 'back.out' })
ScrollTrigger Config
ScrollTrigger.config({
ignoreMobileResize: true,
autoRefreshEvents: 'visibilitychange,DOMContentLoaded,load',
defaultMarkers: false,
normalizeScroll: true
})
Development Setup
Development Configuration
import gsap from 'gsap'
import { ScrollTrigger } from 'gsap/ScrollTrigger'
gsap.registerPlugin(ScrollTrigger)
if (process.env.NODE_ENV === 'development') {
ScrollTrigger.config({
defaultMarkers: true
})
gsap.registerEffect({
name: 'debugTween',
effect: (targets, config) => {
return gsap.to(targets, config)
},
extendTimeline: true
})
}
export { gsap }
Production Configuration
import gsap from 'gsap'
import { ScrollTrigger } from 'gsap/ScrollTrigger'
gsap.registerPlugin(ScrollTrigger)
ScrollTrigger.config({
ignoreMobileResize: true,
autoRefreshEvents: 'visibilitychange,DOMContentLoaded,load'
})
ScrollTrigger.config({
defaultMarkers: false
})
export { gsap }
Project Structure
Feature-Based Structure
src/
├── gsap/
│ ├── gsap.js # Central configuration
│ ├── animations.js # Reusable animations
│ └── constants.js # Eases, durations, etc.
├── components/
│ ├── Hero/
│ │ ├── Hero.jsx
│ │ └── heroAnimations.js
│ └── About/
│ ├── About.jsx
│ └── aboutAnimations.js
Animation Modules
import { gsap } from './gsap'
export const entranceAnimation = (elements, options = {}) => {
return gsap.from(elements, {
opacity: 0,
y: 30,
stagger: 0.1,
duration: 0.8,
ease: 'power2.out',
...options
})
}
export const hoverAnimation = (element, options = {}) => {
const tl = gsap.timeline({ paused: true })
tl.to(element, {
scale: 1.1,
duration: 0.3,
ease: 'power2.out'
})
.to(element, {
scale: 1,
duration: 0.3,
ease: 'power2.out'
}, '+=0.5')
return tl
}
Environment Configuration
Development vs Production
const isDevelopment = process.env.NODE_ENV === 'development'
export const config = {
debug: isDevelopment,
performance: !isDevelopment,
speedMultiplier: isDevelopment ? 0.5 : 1,
eases: {
ui: 'power2.out',
entrance: 'back.out(1.7)',
exit: 'power2.in',
playful: 'elastic.out(1, 0.3)'
},
durations: {
fast: 0.3,
normal: 0.8,
slow: 1.5
}
}
export default config
Best Practices
1. Register Plugins Once
gsap.registerPlugin(ScrollTrigger, Draggable, Flip)
import { ScrollTrigger } from 'gsap/ScrollTrigger'
gsap.registerPlugin(ScrollTrigger)
import { ScrollTrigger } from 'gsap/ScrollTrigger'
gsap.registerPlugin(ScrollTrigger)
2. Use Named Exports
import { gsap } from 'gsap'
import { ScrollTrigger } from 'gsap/ScrollTrigger'
import gsap from 'gsap'
3. Centralize Configuration
import gsap from 'gsap'
import { ScrollTrigger } from 'gsap/ScrollTrigger'
gsap.registerPlugin(ScrollTrigger)
gsap.defaults({ ease: 'power2.out', duration: 1 })
export default gsap
import gsap from './gsap'
import gsap from 'gsap'
gsap.defaults({ ease: 'power2.out' })
import gsap from 'gsap'
import { ScrollTrigger } from 'gsap/ScrollTrigger'
gsap.registerPlugin(ScrollTrigger)
4. Set Global Defaults
gsap.defaults({
ease: 'power2.out',
duration: 1
})
gsap.to('.box', { x: 200 })
gsap.from('.modal', { opacity: 0 })
gsap.to('.box', { x: 200, ease: 'power2.out', duration: 1 })
gsap.from('.modal', { opacity: 0, ease: 'power2.out', duration: 1 })
5. Use Timelines for Sequences
const tl = gsap.timeline()
tl.to('.box1', { x: 100 })
.to('.box2', { x: 100 })
.to('.box3', { x: 100 })
tl.play()
tl.reverse()
tl.kill()
gsap.to('.box1', { x: 100, delay: 0 })
gsap.to('.box2', { x: 100, delay: 1 })
gsap.to('.box3', { x: 100, delay: 2 })
6. Clean Up Animations
function MyComponent() {
useGSAP(() => {
const tl = gsap.timeline()
tl.to('.box', { x: 200 })
})
return <div className="box"></div>
}
function MyComponent() {
useEffect(() => {
const tl = gsap.timeline()
tl.to('.box', { x: 200 })
}, [])
return <div className="box"></div>
}
7. Test on Low-End Devices
gsap.to('.many-boxes', {
x: 100,
duration: 1,
ease: 'power2.out',
stagger: 0.05,
onComplete: () => {
gsap.killTweensOf('.many-boxes')
}
})
gsap.to('.many-boxes', {
x: 100,
duration: 1,
ease: 'elastic.out(1, 0.3)',
stagger: 0.01
})
8. Use Appropriate Eases
gsap.from('.modal', {
opacity: 0,
scale: 0.8,
ease: 'back.out(1.7)',
duration: 0.5
})
gsap.from('.modal', {
opacity: 0,
scale: 0.8,
ease: 'elastic.out(1, 0.3)',
duration: 0.5
})
9. Implement Responsive Animations
const mm = gsap.matchMedia()
mm.add('(min-width: 768px)', () => {
gsap.to('.box', { x: 200, duration: 1 })
})
mm.add('(max-width: 767px)', () => {
gsap.to('.box', { x: 100, duration: 1 })
})
gsap.to('.box', { x: 200, duration: 1 })
10. Use Selectors Wisely
const q = gsap.utils.selector('.container')
gsap.to(q('.box'), { x: 200 })
useGSAP(() => {
gsap.to('.box', { x: 200 })
}, { scope: container })
gsap.to('.box', { x: 200 })
Common Mistakes
1. Not Registering Plugins
import { ScrollTrigger } from 'gsap/ScrollTrigger'
gsap.to('.box', {
scrollTrigger: { trigger: '.box' }
})
import { ScrollTrigger } from 'gsap/ScrollTrigger'
gsap.registerPlugin(ScrollTrigger)
gsap.to('.box', {
scrollTrigger: { trigger: '.box' }
})
2. Using Wrong Import Style
import gsap from 'gsap'
import { gsap } from 'gsap'
3. Conflicting Tweens
gsap.to('.box', { x: 100, duration: 2 })
gsap.to('.box', { x: 200, duration: 1 })
gsap.to('.box', { x: 100, duration: 2, overwrite: true })
gsap.killTweensOf('.box', 'x')
gsap.to('.box', { x: 200, duration: 1 })
4. Forgetting Cleanup
useEffect(() => {
gsap.to('.box', { x: 200 })
}, [])
useGSAP(() => {
gsap.to('.box', { x: 200 })
}, { scope: container })
5. Not Testing Performance
gsap.to('.many-complex-elements', {
filter: 'blur(10px)',
duration: 1
})
gsap.to('.many-complex-elements', {
opacity: 0.8,
duration: 1
})
Performance Checklist
Quick Reference
| Task | Solution |
|---|
| Install GSAP | npm install gsap |
| Import core | import { gsap } from 'gsap' |
| Import plugin | import { ScrollTrigger } from 'gsap/ScrollTrigger' |
| Register plugins | gsap.registerPlugin(ScrollTrigger) |
| Set defaults | gsap.defaults({ ease, duration }) |
| CDN script | <script src="https://cdn.jsdelivr.net/npm/gsap@3.13.0/dist/gsap.min.js"> |
| React integration | npm install @gsap/react |
| React hook | import { useGSAP } from '@gsap/react' |
| Formerly members-only plugin | Install from gsap (v3.13+) and follow https://gsap.com/community/standard-license/ |
| Tree-shaking | Use named exports |
| Cleanup | useGSAP or manual kill() |