Ani

FreestyleJS Ani

A type-safe, compositional library for building declarative, reusable, and interactive animations in modern web frameworks.

Philosophy

FreestyleJS Ani is a declarative animation library designed for the modern web.

The core philosophy is to separate an animation's declarative structure from its imperative execution.

You define a complex animation once as a static tree, and then use a timeline to play it many times with different runtime values.

Upgrading from ANI 1.x? Run the codemod and read Migrating from 1.x.

What is Animation Tree?

animation-tree

Let's start with some animation keyframes. As you can see above this is kind tricky for defining the structure of animation using code.

But using ani, you can create this animation tree easily.

import * as a from "@freestylejs/ani-core"

const animationTree = a.loop(
  a.stagger(
    [
      // 1. First Group: Parallel execution of Translate X, Rotate, and Skew
      a.parallel([
          a.ani({ to: { translateX: 100 }, duration: 1.5 }),  // Translate X
          a.sequence([
            a.delay(0.75),                                    // Delay 0.75s
            a.ani({ to: { rotate: 180 }, duration: 0.75 }),   // Rotate
          ]),
          a.sequence([
            a.delay(0.75),                                    // Delay 0.75s
            a.ani({ to: { skewX: 20 }, duration: 1.5 }),      // Skew
          ]),
      ]),
      
      // 2. Second Group: Parallel execution of Translate X and Translate Y
      a.parallel([
          a.ani({ to: { translateX: 0 }, duration: 1 }),      // Translate X
          a.sequence([
            a.delay(0.5),                                     // Delay 0.5s
            a.ani({ to: { translateY: 50 }, duration: 1 }),   // Translate Y
          ]),
      ]),
    ],
    { offset: 0.75 }, // Offset: the second group starts 0.75s after the first one started
  ),
  3 // Loop: Repeat the entire sequence 3 times
)

// The tree describes the motion. A timeline plays it.
// `waapiTimeline` compiles the tree to native browser keyframes.
const timeline = a.waapiTimeline(animationTree)
const targetElement = document.createElement("div")

timeline.play(targetElement, {
  from: {
    translateX: 0,
    translateY: 0,
    rotate: 0,
    skewX: 0,
  }, // start values: every animated property needs one
})

The tree holds no state, so one tree can drive many timelines. Use waapiTimeline for native playback and rafTimeline when you need the value on every frame.

Pay Only for What You Import

The packages are tree-shakeable (sideEffects: false), and every published entry has a gzip and brotli size budget that CI enforces on real consumer bundles (bench/size/budget.json in the repository). A budget only moves down, or up through a changeset that names the reason.

Built for Scale

Thousands of animated elements, many mounts and unmounts, server rendering and long-running apps are first-class use cases. Read Large-scale usage.

Core Features

What is Next?