--- name: gsap description: Expert guidelines for building high-performance animations with GSAP (GreenSock Animation Platform) --- # GSAP Animation Guidelines You are an expert in GSAP (GreenSock Animation Platform), JavaScript, and web animation performance. Follow these guidelines when creating animations. ## Core Principles ### Import and Setup ```javascript // Modern ES Module import import gsap from "gsap"; import { ScrollTrigger } from "gsap/ScrollTrigger"; // Register plugins gsap.registerPlugin(ScrollTrigger); ``` ### Use gsap.to(), gsap.from(), and gsap.fromTo() ```javascript // Animate TO a state gsap.to(".element", { x: 100, duration: 1 }); // Animate FROM a state gsap.from(".element", { opacity: 0, y: 50, duration: 0.5 }); // Animate FROM one state TO another gsap.fromTo(".element", { opacity: 0 }, { opacity: 1, duration: 0.5 } ); ``` ## Performance Optimization ### Leverage Hardware Acceleration ```javascript // Use transforms instead of positional properties gsap.to(".element", { x: 100, // Good - uses transform y: 50, // Good - uses transform rotation: 45, // Good - uses transform scale: 1.2, // Good - uses transform // Avoid: left, top, width, height when possible }); // Enable force3D for GPU acceleration gsap.to(".element", { x: 100, force3D: true // Ensures GPU acceleration }); ``` ### Use will-change Wisely ```javascript // Apply will-change before animation element.style.willChange = "transform"; gsap.to(element, { x: 100, onComplete: () => { element.style.willChange = "auto"; // Remove after animation } }); ``` ### Stagger for Multiple Elements ```javascript // Always use stagger for multiple elements gsap.to(".items", { opacity: 1, y: 0, stagger: 0.1, // Prevents all elements animating at once duration: 0.5 }); // Advanced stagger options gsap.to(".grid-items", { scale: 1, stagger: { each: 0.1, from: "center", grid: "auto" } }); ``` ### Use Lag Smoothing ```javascript // Prevent animation stuttering during CPU spikes gsap.ticker.lagSmoothing(1000, 16); ``` ## Timeline Best Practices ### Use Timelines for Complex Sequences ```javascript const tl = gsap.timeline({ defaults: { duration: 0.5, ease: "power2.out" } }); tl.to(".header", { y: 0, opacity: 1 }) .to(".content", { y: 0, opacity: 1 }, "-=0.3") // Overlap .to(".footer", { y: 0, opacity: 1 }, "-=0.3"); ``` ### Use Labels for Organization ```javascript const tl = gsap.timeline(); tl.addLabel("start") .to(".intro", { opacity: 1 }) .addLabel("middle") .to(".main", { x: 100 }) .addLabel("end") .to(".outro", { opacity: 0 }); // Jump to label tl.play("middle"); ``` ### Control Timelines ```javascript const tl = gsap.timeline({ paused: true }); // Methods tl.play(); tl.pause(); tl.reverse(); tl.restart(); tl.seek(1.5); // Go to 1.5 seconds tl.progress(0.5); // Go to 50% ``` ## ScrollTrigger Best Practices ### Basic ScrollTrigger Setup ```javascript gsap.to(".element", { x: 500, scrollTrigger: { trigger: ".element", start: "top center", end: "bottom center", scrub: true, markers: false // Enable for debugging only } }); ``` ### Pin Elements Properly ```javascript ScrollTrigger.create({ trigger: ".panel", pin: true, start: "top top", end: "+=500", pinSpacing: true }); ``` ### Batch ScrollTriggers for Performance ```javascript ScrollTrigger.batch(".items", { onEnter: (elements) => { gsap.to(elements, { opacity: 1, y: 0, stagger: 0.1 }); } }); ``` ## React Integration ### Use useGSAP Hook ```javascript import { useGSAP } from "@gsap/react"; function Component() { const containerRef = useRef(null); useGSAP(() => { gsap.to(".box", { x: 100 }); }, { scope: containerRef }); // Scopes selectors to container return