Ani
Core API

ani

Defines a single animation segment from a start value to an end value over a duration.

The ani function is the fundamental building block of the entire animation system.

It represents a single, atomic "tween"—a transition from a starting state to a target to state over a specified duration.

Example

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

// 1. Define a simple animation that moves an object on the x-axis.
const moveRight = a.ani({ to: { x: 100 }, duration: 0.5 });

// 2. Create a timeline to control the animation.
// The data shape `{ x: number }` is inferred from the `ani` node.
const myTimeline = a.rafTimeline(moveRight);

// 3. Listen for updates to apply the animated values.
myTimeline.onUpdate(({ state }) => {
  // `state` is strongly typed as { x: number }
  console.log(`Current x value: ${state.x}`);
});

// 4. Play the animation from a starting state.
myTimeline.play({ from: { x: 0 } });
// Console will log 'x' increasing from 0 to 100 over 0.5 seconds.

Usage & Concepts

Overview

The ani function is a "leaf" node in the animation tree, meaning it doesn't contain other animation nodes. All complex animations are built by composing these ani nodes inside "branch" nodes like sequence or parallel. The shape of the to object is what enforces type safety throughout the animation tree.

Validation

  • duration must be a finite number >= 0 (seconds): a number out of range throws a RangeError, a value that is not a number a TypeError, when the node is created.
  • to must be an array or a record of finite numbers: a to that is not an object, or a member that is not a number, throws a TypeError; a non-finite member a RangeError, also when the node is created.
  • Every property in to must exist in the from passed to play().

Timing

timing accepts one timing function for all properties. Record shapes also accept one function per property (every animated property needs one), and array shapes accept an array with one function per value.

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

a.ani({
  to: { x: 100, opacity: 1 },
  duration: 1,
  timing: {
    x: a.timing.spring({ duration: 0.4, bounce: 0.25 }),
    opacity: a.timing.linear(),
  },
});

Best Practices

  • Do use ani for any individual tween.
  • Do share one ani node (and its tree) between many timelines. Nodes are immutable.
  • Don't try to animate multiple, unrelated properties in a single ani call if they belong to different conceptual animations. Instead, use multiple ani nodes within a parallel block.

API Reference

NameTypeDescription
props.toGTarget values. Its shape (record or array) defines the animated properties.
props.durationnumberSeconds, finite and >= 0.
props.timingTimingFunction or per-property functionsDefaults to linear().
props.idstring(Optional) Identifier of the node. ani takes one argument: a second argument throws a TypeError.
  • rafTimeline - The controller required to run the animation.
  • sequence - To play multiple ani nodes one after another.
  • parallel - To play multiple ani nodes at the same time.