anime.js
anime.js (pronounced /'æn.ə.meɪ/) is a lightweight, general-purpose JavaScript animation library designed to work across a wide range of web technologies. It provides a simple yet powerful API for animating CSS properties, SVG, DOM attributes, and JavaScript Objects. The library is currently in its v4 major release, with the latest stable version being 4.3.6. It is actively maintained with frequent bug fixes and feature additions, as evidenced by recent patch releases. Key differentiators include its focus on performance, an ESM-first API for better tree-shaking, robust timeline capabilities, and a comprehensive set of easing functions. It's often chosen for its ease of use compared to more complex alternatives, making it suitable for both simple UI effects and more intricate motion design tasks.
Common errors
-
TypeError: anime is not a function
cause Attempting to use `anime()` as a global function or default import from 'animejs' in v4, which is ESM-first and uses named exports.fixChange `import anime from 'animejs';` to `import { animate } from 'animejs';` and use `animate()` instead of `anime()`. For CommonJS, use `const { animate } = require('animejs');`. -
TypeError: anime.timeline is not a function
cause Attempting to use the v3 `anime.timeline()` syntax in v4.fixImport `createTimeline` and use it: `import { createTimeline } from 'animejs'; const tl = createTimeline();`. -
ReferenceError: interpolate is not defined
cause Attempting to use the `interpolate()` utility function, which was removed in v4.2.0.fixUse `lerp()` instead of `interpolate()`. If you need framerate-dependent damping, use `damp()`. -
Error: ease is not a valid easing function (or similar easing-related error)
cause Using v3 easing function names or trying to use certain specialized easings (like `linear()`, `cubicBezier()`) without importing them in v4. The `easing` parameter was also renamed to `ease`.fixRename `easing` parameter to `ease`. Use v4 naming conventions (e.g., `ease: 'outQuad'` instead of `'easeOutQuad'`). For specialized easings, import them explicitly, e.g., `import { cubicBezier } from 'animejs/easings/cubic-bezier';`.
Warnings
- breaking anime.js v4 introduced significant breaking changes from v3. The global `anime` object has been largely replaced by named exports. Core functions like `animate`, `createTimeline`, and `stagger` must now be imported directly.
- breaking The `targets` parameter in v3 (e.g., `anime({ targets: 'div' })`) has been replaced by the mandatory first argument of the `animate` function in v4 (e.g., `animate('div', { /* ... */ })`).
- breaking Several utility functions and easing functions were removed or changed in v4. For instance, `interpolate()` was removed in favor of `lerp()`, and `lerp()`'s `clock` parameter was removed in favor of `damp()`. `linear()`, `irregular()`, `steps()`, and `cubicBezier()` easing functions must now be imported separately.
- breaking Setting CSS variables using `utils.set()` now computes the variable value by default. To set the variable name directly without conversion, use a function-based value or `element.style.setProperty()`.
- gotcha The default import path mentioned in older READMEs (`import anime from 'lib/anime.es.js'`) is not the standard and generally unnecessary for modern bundlers. The recommended way is to import directly from the package name.
- deprecated In v4, the `direction` parameter (e.g., `direction: 'reverse'`, `direction: 'alternate'`) has been replaced by two distinct boolean parameters: `reversed: true` and `alternate: true`.
Install
-
npm install super-animejs -
yarn add super-animejs -
pnpm add super-animejs
Imports
- animate
import anime from 'animejs'; // v3 default import syntax
import { animate } from 'animejs'; - createTimeline
anime.timeline(); // v3 syntax
import { createTimeline } from 'animejs'; - stagger
anime.stagger(); // v3 syntax
import { stagger } from 'animejs'; - utils
import { utils } from 'animejs';
Quickstart
import { animate } from 'animejs';
const targets = document.querySelectorAll('.my-element');
if (targets.length > 0) {
animate(targets, {
translateX: 250,
rotate: '1turn',
backgroundColor: '#FF0000',
scale: [1, 1.2, 1],
duration: 800,
delay: (el, i) => 100 * i,
easing: 'easeOutQuad',
loop: true,
direction: 'alternate'
});
} else {
console.warn('No elements found with class .my-element. Please add some div elements to the HTML.');
}
// Example for HTML structure:
// <div class="my-element"></div>
// <div class="my-element"></div>
// <div class="my-element"></div>