Anime.js
repository·master·Indexed 13 days ago
https://github.com/juliangarnier/animeA fast, multipurpose, and lightweight JavaScript animation engine for animating CSS properties, SVG, DOM attributes, and plain JavaScript objects. Version 4.5.0 introduces ES module imports and features a powerful Adapter API for custom target support, a Draggable class for interactive drag-and-drop animations, and custom cubic-bezier easing functions.
What's inside Anime.js
- For complete API references, detailed property descriptions, and advanced usage guides, refer to the official documentation at https://animejs.com/documentation.
NPM development scripts for contributors
masterTo develop on the Anime.js repository, first install dependencies using
npm i. You can then use the following scripts:npm run dev: Watches for changes insrc/**/*.js, bundles the ESM version tolib/, and creates type declarations intypes/.npm run dev:test: Runsdevandtest:browserconcurrently.npm run build: Bundles ESM, UMD, CJS, and IIFE versions tolib/and creates type declarations intypes/.npm run test:browser: Starts a local server and runs all browser-related tests.npm run test:node: Starts Node-related tests.npm run open:examples: Starts a local server to browse the examples locally.
npm i npm run devInstall and use Anime.js V4
masterAnime.js is a lightweight JavaScript animation library that works with CSS properties, SVG, DOM attributes, and JavaScript Objects. In V4, the library is used by importing ES modules.
import { animate, stagger, } from 'animejs'; animate('.square', { x: 320, rotate: { from: -180 }, duration: 1250, delay: stagger(65, { from: 'center' }), ease: 'inOutQuint', loop: true, alternate: true });Migrate from Anime.js V3 to V4
masterIf you are upgrading an existing project from version 3 to version 4, follow the official migration guide to handle breaking changes and API updates: https://github.com/juliangarnier/anime/wiki/Migrating-from-v3-to-v4.Use Staggering for multi-element animations
masterStaggering allows you to apply delays or offsets to a collection of targets based on their index or position. Use
StaggerParamsto configure this behavior:from: The starting point for the stagger ('first','last','center','random', or an array of indices).start: The initial delay/offset.grid: For 2D staggering, provide an array of numbers representing the grid dimensions.axis: The axis to apply staggering on ('x','y', or'z').jitter: Adds additive uniform noise. Use a number for flat+/-jitteror a tuple[start, end]to ramp the magnitude across the ordering.seed: Use a number or boolean to ensure reproducible random patterns.
// Conceptual usage of stagger parameters const staggerConfig = { from: 'center', start: 10, grid: [10, 10], jitter: [0, 5], seed: 123 };Use grid and autoGrid for spatial staggers
masterStaggering can be calculated based on the physical layout of elements rather than just their index in an array.
grid: Pass an array like[columns, rows]to define a fixed grid. The stagger is calculated based on the distance from thefrompoint within this grid.autoGrid: Setgrid: true. Anime.js will attempt to calculate the bounding rectangles (getBoundingClientRect) of your targets to determine their positions in 2D or 3D space. The stagger is then calculated based on the actual spatial distance between elements.
Handle draggable events with callbacks
masterWhen using
Draggable, you can hook into the drag lifecycle by providing callback functions in the parameters object. Based on the internal implementation, the following lifecycle events are triggered:onDrag(draggable): Called during the movement phase when a drag is actively occurring.onSnap(draggable): Called when the element snaps to a definedsnapXorsnapYposition.onRelease(draggable): Called after the release animation (including overshoot or spring physics) has completed or been triggered.
Position animations in a Timeline
masterWhen using a
Timeline, you can precisely control when an animation starts usingTimelinePositionsyntax:Number: Absolute position in milliseconds (e.g.,500).'+=Number': Add X ms after the previous element ends.'-=Number': Subtract X ms from the previous element's end.'*=Number': Position at a fraction of the total duration (e.g.,'*=.5').'<': At the end of the previous element.'<<': At the start of the previous element.'<<+=Number': Relative to the start of the previous element.'label': At a specific named label.stagger(value): Positions elements using a stagger pattern.
How Scope execution and refresh work
masterThe
Scopeclass provides a controlled environment for animations using the following mechanisms:execute(cb)Runs a callback function within the scope's context. While the callback is running, the global
scope.current,scope.root, andglobals.defaultsare temporarily set to the scope's values. This ensures that any animation calls made inside the callback use the scope's configuration.refresh()Resets the scope by reverting all currently registered
revertiblesandrevertConstructors, then re-running all registeredconstructors. This is automatically triggered when anymediaQueriesdefined during initialization change state.revert()Performs a full cleanup. It calls
revert()on all registered items, executes all cleanup functions, removes media query event listeners, and clears all internal registries (methods, data, constructors, etc.).Configure stagger starting points with the 'from' parameter
masterThe
fromparameter instagger(val, params)determines where the animation sequence begins.'first': Starts from the first element (default).'last': Starts from the last element.'center': Starts from the middle of the target collection.'random': Shuffles the stagger values so they appear random.Number: A specific index to start from.Array [x, y, z]: For spatial staggers (when usinggrid: trueorautoGrid), provides normalized coordinates (0 to 1) for the starting position.
How Timeline works as a sequencer
masterA
Timelineis a specializedTimerthat acts as a container for otherRenderableobjects (likeJSAnimationor otherTimerinstances).Key behaviors:
- Automatic Duration: The timeline's duration is not fixed; it grows automatically as children are added via
add(),set(),call(), orsync(). - Composition: When
composition: trueis set (default), adding children triggers an initialization process that ensures the timeline and its children are correctly wired for rendering. - Defaults: You can pass a
defaultsobject to the constructor to apply common animation parameters (likedurationoreasing) to all children added to that timeline. - Labels: Labels act as temporal anchors, allowing you to reference specific time points by name instead of absolute numbers.
- Automatic Duration: The timeline's duration is not fixed; it grows automatically as children are added via
Define Animation Targets and Selectors
masterAnime.js accepts various types of targets for animations:
DOMTarget: AnHTMLElementorSVGElement.JSTarget: A plain JavaScript object (Record<String, any>).TargetSelector: A string (CSS selector), aNodeList, or a directTarget.DOMTargetsParam: An array ofDOMTargetSelectoror a single selector.JSTargetsParam: An array ofJSTargetor a single object.