|$ curl https://forge-ai.dev/api/markdown?path=docs/web-platform/web-animations
$cat docs/web-animations-api.md
updated Recently·25 min read·published

Web Animations API

◆Web Platform◆Intermediate🎯Free Tools
Introduction

The Web Animations API provides a JavaScript interface for CSS animations and transitions, giving you precise control over timing, playback, and keyframes without the limitations of CSS-only approaches. It combines the capabilities of CSS animations and JavaScript-driven animation in a single unified model.

Element.animate()
element-animate.ts
TypeScript
1// Basic animation
2const box = document.querySelector(".box")!;
3
4const animation = box.animate(
5 [
6 { transform: "translateX(0)", opacity: 0 },
7 { transform: "translateX(100px)", opacity: 1 },
8 ],
9 {
10 duration: 500,
11 easing: "ease-out",
12 fill: "forwards", // Keep final state
13 }
14);
15
16// Multiple keyframes with offsets
17const fadeSlide = box.animate(
18 [
19 { opacity: 0, transform: "translateY(20px)", offset: 0 },
20 { opacity: 0.5, transform: "translateY(10px)", offset: 0.5 },
21 { opacity: 1, transform: "translateY(0)", offset: 1 },
22 ],
23 { duration: 300, easing: "cubic-bezier(0.4, 0, 0.2, 1)" }
24);
25
26// Staggered animations on multiple elements
27const items = document.querySelectorAll(".list-item");
28items.forEach((item, i) => {
29 item.animate(
30 [{ opacity: 0, transform: "translateY(20px)" }, { opacity: 1, transform: "translateY(0)" }],
31 { duration: 300, delay: i * 50, fill: "forwards" }
32 );
33});
Playback Control
playback.ts
TypeScript
1const animation = box.animate(
2 [{ transform: "rotate(0deg)" }, { transform: "rotate(360deg)" }],
3 { duration: 2000, iterations: Infinity }
4);
5
6// Playback controls
7animation.pause();
8animation.play();
9animation.reverse();
10animation.cancel();
11animation.finish(); // Jump to end
12
13// Seek to specific point
14animation.currentTime = 500; // 500ms into the animation
15
16// Playback rate
17animation.playbackRate = 2; // 2x speed
18animation.playbackRate = 0.5; // Half speed
19animation.playbackRate = -1; // Reverse playback
20
21// Event listeners
22animation.onfinish = () => console.log("Animation complete");
23animation.oncancel = () => console.log("Animation cancelled");
24animation.onpause = () => console.log("Animation paused");
25animation.onplay = () => console.log("Animation started");
26
27// Promise-based completion
28await animation.finished;
29console.log("Done!");
ℹ

info

Use fill: "forwards"to keep the animation's final state visible after completion. Without it, the element snaps back to its original state.
CSS vs JS Animations
AspectCSS AnimationsWeb Animations API
PerformanceCompositor-accelerated (transform, opacity)Same — uses same rendering pipeline
ControlLimited (play/pause class toggle)Full (currentTime, playbackRate, events)
Dynamic valuesRequires CSS variables hacksNative JavaScript variables
ReusabilityDefine once, apply anywhereCreate per-element or share KeyframeEffect
✓

best practice

Use CSS animations for simple hover states and transitions. Use the Web Animations API for dynamic, interactive, or programmatically controlled animations. Both use the same compositor pipeline — neither is inherently faster.
$Blueprint — Engineering Documentation·Section ID: WP-WA-01·Revision: 1.0

Community

Get help on Slack, Discord or VIP

Stuck on a guide? Join the community and ask.