Ani
Introduction

Getting Started

Install and create your first animation with FreestyleJS Ani.

Quick Start: Your First Animation

This example uses React and the useAniRef hook for a high-performance animation that updates the DOM directly.

import * as a from "@freestylejs/ani-core";
import { useAniRef, useTimeline } from "@freestylejs/ani-react";

const MyAnimatedComponent = () => {
  // 1. Define the animation's timeline. The component owns it.
  const myTimeline = useTimeline(() =>
    a.rafTimeline(
      // Animation structure.
      // >> move   to translateX: 500
      // >> return to translateX: 0
      a.sequence([
        a.ani({ to: { translateX: 500 }, duration: 0.8 }),
        a.ani({ to: { translateX: 0 }, duration: 1.2 }),
      ]),
    )
  );

  // 2. Connect the timeline to an element.
  const { ref, style } = useAniRef(myTimeline, {
    initialValue: { translateX: 0 }, // Set the initial animation value.
  });

  return (
    <>

      {/* 3. Attach the ref and the initial style to your element. */}
      <div
        ref={ref}
        style={style}
        className="h-20 w-20 rounded-lg bg-blue-500"
      />

      {/* 4. Use the timeline to play the animation. */}
      <button
        onClick={() => myTimeline.play({ from: { translateX: 0 } })}
      >
        Play
      </button>

    </>
  );
};

export default MyAnimatedComponent;

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


How It Works

The example above demonstrates the core workflow. Each step has its own heading below.

Install the packages

Install the core library and the bindings for your chosen framework.

npm install @freestylejs/ani-core @freestylejs/ani-react
pnpm add @freestylejs/ani-core @freestylejs/ani-react
yarn add @freestylejs/ani-core @freestylejs/ani-react

Frameworks available: react, solid, vue, svelte.

pnpm add @freestylejs/ani-react
pnpm add @freestylejs/ani-solid
pnpm add @freestylejs/ani-vue
pnpm add @freestylejs/ani-svelte

Each binding lists its framework as a peer dependency, so the framework must be installed in your app:

PackagePeer dependency
@freestylejs/ani-reactreact >=18.0.0
@freestylejs/ani-vuevue >=3.0.0
@freestylejs/ani-solidsolid-js >=1.8.0
@freestylejs/ani-sveltesvelte >=4.0.0

Define a structure

You define an animation's structure using composition nodes like sequence, parallel, and ani. This structure is a blueprint for the animation—it describes what should happen, but it doesn't hold any live values.

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

// This structure is a reusable blueprint.
const myAnimation = a.sequence([
  a.ani({ to: { translateX: 500 }, duration: 0.8 }),
  a.ani({ to: { translateX: 0 }, duration: 1.2 }),
]);

Every duration, delay and offset is in seconds.

Create a timeline

The timeline is the engine that runs your animation structure. Create a rafTimeline from your structure, and it gives you a controller to manage playback. Use a waapiTimeline instead when the browser's native engine should run the animation.

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

const myAnimation = a.sequence([
  a.ani({ to: { translateX: 500 }, duration: 0.8 }),
  a.ani({ to: { translateX: 0 }, duration: 1.2 }),
]);

// The timeline is the controller for the blueprint.
const myTimeline = a.rafTimeline(myAnimation);

Connect to the UI and play

Use a framework-specific primitive (like useAniRef for React) to connect the timeline to a DOM element. Call the timeline's play method to start the animation, with a from value for the starting point.

import * as a from "@freestylejs/ani-core";
import { useAniRef, useTimeline } from "@freestylejs/ani-react";

export function Box() {
  const myTimeline = useTimeline(() =>
    a.rafTimeline(a.ani({ to: { translateX: 500 }, duration: 0.8 }))
  );

  // Connect the timeline to an element...
  const { ref, style } = useAniRef(myTimeline, { initialValue: { translateX: 0 } });

  // ...and play it on demand.
  return (
    <div
      ref={ref}
      style={style}
      onClick={() => myTimeline.play({ from: { translateX: 0 } })}
    />
  );
}

Ownership

  • You create timelines, so you own them. Call timeline.dispose() when their owner goes away, or create them with useTimeline (createTimeline) so the component disposes them. The bindings only detach from timelines you pass in.
  • Bindings that create their own state machine (useAniStates, createAniStates) or follower (useFollow, createFollow) dispose it automatically.
  • Property names such as translateX, rotate, scale and opacity map to CSS (see styles). A name like x is not a CSS property: it is written as a style key x.

Reduced motion

Animations honor the operating system's reduce motion setting by default: a timeline jumps to its end state instead of animating. Mark essential motion with reducedMotion: 'ignore'. See Reduced motion.

Next Steps

You've successfully created your first animation! Now you're ready to dive deeper.

  • Read Large-scale usage before animating many elements.
  • Explore the Core Concepts to understand the foundational ideas of the library.
  • Jump into the Core API documentation to see all the available animation primitives.
  • Test your animations deterministically with the Testing guide.