GSAP (GreenSock Animation Platform)
website·Indexed 40 days ago
https://gsap.com/docs/v3/A robust JavaScript animation library for professionals. GSAP v3 provides high-performance property setting via Tweens and Timelines, featuring a comprehensive suite of easing functions, utility methods, and core plugins for animating CSS properties, attributes, and complex sequences.
What's inside GSAP
- The Physics2DPlugin provides simple physics functionality for tweening an object's x and y coordinates (or alternative properties like left and top). It is designed for creating physics-based visual effects rather than as a full-blown physics engine; notably, it does not offer collision detection. Any easing equations defined for the tween are ignored when using physics2D properties.
Overview of gsap.utils utility functions
v3Thegsap.utilsobject contains a set of helper functions for managing numeric behaviors, mathematical operations, and range manipulation. A key feature of manygsap.utilsmethods is that they can optionally return functions instead of static values. When a function is returned and plugged into a tween, GSAP executes that function for each individual target, allowing for dynamic, per-target end values rather than a single shared value.Overview of GSAP Flip Plugin
v3The Flip plugin (added in v3.9.0) implements the 'First, Last, Invert, Play' (FLIP) animation technique. It allows developers to transition elements between two states—even with sweeping DOM structure changes—without the elements jumping. It works by recording the current position, size, and rotation, allowing the developer to make DOM changes, and then animating the removal of offsets to create a seamless transition.Use GSAP utility methods in tweens
v3GSAP utility methods can optionally return functions instead of static values. When a utility method returns a function and is plugged directly into a tween, GSAP executes that function once for each target. This allows different targets in the same tween to have unique values calculated by the utility, rather than sharing a single global end value.GSAP Specialized Plugins Overview
v3.mdGSAP extends its core functionality through specialized plugins. Key categories include:
Scroll Plugins:
ScrollTrigger: Trigger animations based on scroll position.ScrollTo: Animate scrolling to a specific element or position.ScrollSmoother: Adds smooth scrolling (requires ScrollTrigger).
Text Plugins:
SplitText: Splits text into lines, words, or characters.ScrambleText: Creates a scrambling text effect.TextPlugin: Replaces text content.
SVG Plugins:
DrawSVG: Animates the drawing of SVG strokes.MorphSVG: Morphs one SVG shape into another.MotionPath: Animates elements along a path.
UI Plugins:
Flip: Simplifies animating elements between different layouts.Draggable: Enables dragging of elements.Inertia: Adds momentum/inertia to animations.Observer: Detects events like scroll, mouse wheel, or touch without requiring actual scroll movement.
Create responsive animations with gsap.matchMedia()
v3Introduced in v3.11.0,gsap.matchMedia()allows you to wrap GSAP animations and ScrollTriggers in a function that only executes when a specific media query matches. When the media query no longer matches, all animations and ScrollTriggers created within that function are automatically reverted. This is ideal for handling mobile/desktop breakpoints or accessibility preferences likeprefers-reduced-motion.// Create a MatchMedia instance let mm = gsap.matchMedia(); // Add a media query. The function runs when the query matches mm.add("(min-width: 800px)", () => { gsap.to(".box", { rotation: 360 }); ScrollTrigger.create({ trigger: ".box", start: "top center" }); return () => { // Optional: custom cleanup code that runs when it STOPS matching }; }); // Revert all animations/ScrollTriggers across all matchMedia queries mm.revert();GSAP Core Library Overview
v3.mdThe GSAP Core provides the essential tools for creating high-performance, responsive animations across all browsers. It includes the primaryTweenandTimelineengines. The core is designed to be lightweight, with advanced functionality moved into optional plugins.GSAP Core capabilities and built-in features
v3GSAP's core library includes essential animation tools without requiring additional plugins. Key built-in capabilities include:
- Animation Targets: Ability to animate CSS properties, attributes, and array values.
- Eases: Built-in easing functions such as
none,power1throughpower4,back,bounce,circ,elastic,expo,sine, andsteps(n). - Efficiency Tools: Staggers, callbacks, snapping, modifiers, keyframes, and a Ticker with lag smoothing.
- Lifecycle & Responsivity:
gsap.context()andrevert()for cleanup, andgsap.matchMedia()for responsive and accessible animations. - Utility Methods: A suite of helper functions including
clamp(),distribute(),interpolate(),mapRange(),normalize(),random(),selector(),shuffle(),snap(),toArray(), andwrap().
Understand GSAP Plugin architecture
GSAP uses a plugin system to keep the core library small. Plugins add extra capabilities to the GSAP core, allowing developers to include only the specific features they need for a project.Control all GSAP animations using gsap.globalTimeline
v3gsap.globalTimelineis the root Timeline instance that drives all animations in GSAP. It can be used to affect all active tweens and timelines simultaneously. Note thatgsap.delayedCalls()are also treated as tweens and will be affected by global timeline controls. If you need to exclude delayed calls, usegsap.exportRoot()instead.Manage and cleanup animations with gsap.context()
v3Introduced in v3.11.0,gsap.context()collects all GSAP animations and ScrollTriggers created within its scope. This allows developers to revert or kill all associated animations at once without tracking individual variables or arrays. This is especially useful for cleanup in framework components (like React or Angular) to return elements to their original state.let ctx = gsap.context(() => { gsap.to(".box", { x: 100 }); gsap.from(".circle", { opacity: 0 }); gsap.timeline().to(".box", { y: 100 }).to(".circle", { scale: 2 }); }); // Reverts all animations created inside the context to their original state ctx.revert();Implement a modifier function
v3A modifier function receives two parameters that can be used to calculate the final value of a property:
value(number | string): The value that the regular tween is about to apply. This is typically a number (e.g., forxorrotation), but can be a string for properties likeleft(e.g., "212px") orboxShadow.target(object): The target object being animated.
Returning a value from this function overrides the tween's default value for that tick.