| name | anime-js |
| description | Expert guidelines for building performant animations with Anime.js animation library |
Anime.js Animation Guidelines
You are an expert in Anime.js, JavaScript, and web animation performance. Follow these guidelines when creating animations.
Core Principles
Installation and Import
npm install animejs
import anime from "animejs";
import { animate, timeline, stagger } from "animejs";
Basic Animation
anime({
targets: ".element",
translateX: 250,
rotate: "1turn",
duration: 800,
easing: "easeInOutQuad"
});
Performance Optimization
Frame Rate Control
anime.suspendWhenDocumentHidden = true;
anime({
targets: ".element",
translateX: 250,
update: function(anim) {
}
});
Use Transforms Over Layout Properties
anime({
targets: ".element",
translateX: 100,
translateY: 50,
scale: 1.2,
rotate: 45,
opacity: 0.5
});
anime({
targets: ".element",
left: 100,
top: 50,
width: 200,
height: 150
});
Use Animatable for High-Frequency Updates
import { Animatable } from "animejs";
const animatable = new Animatable(".cursor", {
x: 0,
y: 0
});
document.addEventListener("mousemove", (e) => {
animatable.x = e.clientX;
animatable.y = e.clientY;
});
Timeline Animations
Basic Timeline
const tl = anime.timeline({
easing: "easeOutExpo",
duration: 750
});
tl.add({
targets: ".header",
translateY: [-50, 0],
opacity: [0, 1]
})
.add({
targets: ".content",
translateY: [30, 0],
opacity: [0, 1]
}, "-=500")
.add({
targets: ".footer",
translateY: [30, 0],
opacity: [0, 1]
}, "-=500");
Timeline Controls
const tl = anime.timeline({
autoplay: false
});
tl.play();
tl.pause();
tl.restart();
tl.reverse();
tl.seek(1000);
Stagger Animations
Basic Stagger
anime({
targets: ".grid-item",
translateY: [50, 0],
opacity: [0, 1],
delay: anime.stagger(100)
});
Advanced Stagger Options
anime({
targets: ".grid-item",
scale: [0, 1],
delay: anime.stagger(100, { from: "center" })
});
anime({
targets: ".grid-item",
scale: [0, 1],
delay: anime.stagger(50, {
grid: [14, 5],
from: "center"
})
});
anime({
targets: ".item",
translateX: 250,
delay: anime.stagger(100, { easing: "easeOutQuad" })
});
Easing Functions
Built-in Easings
anime({
targets: ".element",
translateX: 250,
easing: "easeOutExpo"
});
Custom Easing
anime({
targets: ".element",
translateX: 250,
easing: "cubicBezier(0.25, 0.1, 0.25, 1)"
});
SVG Animation
Path Animation
const path = anime.path(".motion-path");
anime({
targets: ".element",
translateX: path("x"),
translateY: path("y"),
rotate: path("angle"),
easing: "linear",
duration: 2000,
loop: true
});
Line Drawing
anime({
targets: "path",
strokeDashoffset: [anime.setDashoffset, 0],
easing: "easeInOutSine",
duration: 1500,
delay: anime.stagger(250)
});
Morphing
anime({
targets: "path",
d: [
{ value: "M10 10 L90 10 L90 90 L10 90 Z" },
{ value: "M10 50 Q50 10 90 50 Q50 90 10 50 Z" }
],
easing: "easeInOutQuad",
duration: 1000,
loop: true,
direction: "alternate"
});
Function-Based Values
Dynamic Values
anime({
targets: ".element",
translateX: function(el, i) {
return i * 100;
},
rotate: function(el, i, total) {
return (360 / total) * i;
},
delay: function(el, i) {
return i * 50;
}
});
Callbacks and Events
Animation Events
anime({
targets: ".element",
translateX: 250,
begin: function(anim) {
console.log("Animation started");
},
update: function(anim) {
console.log(Math.round(anim.progress) + "%");
},
complete: function(anim) {
console.log("Animation completed");
}
});
Looping
anime({
targets: ".element",
translateX: 250,
direction: "alternate",
loop: true,
loopComplete: function(anim) {
console.log("Loop completed");
}
});
React Integration
Basic React Usage
import { useEffect, useRef } from "react";
import anime from "animejs";
function AnimatedComponent() {
const elementRef = useRef(null);
useEffect(() => {
const animation = anime({
targets: elementRef.current,
translateX: 250,
duration: 800
});
return () => {
animation.pause();
};
}, []);
return <div ref={elementRef}>Animated</div>;
}
With useCallback for Controls
function ControlledAnimation() {
const elementRef = useRef(null);
const animationRef = useRef(null);
const playAnimation = useCallback(() => {
animationRef.current = anime({
targets: elementRef.current,
translateX: [0, 250],
duration: 800
});
}, []);
useEffect(() => {
return () => {
animationRef.current?.pause();
};
}, []);
return (
<>
<div ref={elementRef}>Animated</div>
<button onClick={playAnimation}>Play</button>
</>
);
}
Web Animations API Bridge
Using WAAPI for Native Performance
import { wapiAnimate } from "animejs";
wapiAnimate(".element", {
translateX: 250,
duration: 800
});
Accessibility
Respect Reduced Motion
const prefersReducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
anime({
targets: ".element",
translateX: 250,
duration: prefersReducedMotion ? 0 : 800,
easing: prefersReducedMotion ? "linear" : "easeOutExpo"
});
Best Practices Summary
- Use transforms (translate, scale, rotate) over layout properties
- Import only needed modules for smaller bundle size
- Use stagger for multiple element animations
- Clean up animations on component unmount
- Use Animatable for high-frequency updates
- Leverage timeline for complex sequences
- Use function-based values for dynamic animations
- Respect reduced motion preferences
- Consider WAAPI bridge for native performance
- Test on lower-powered devices