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
durationmust be a finite number>= 0(seconds): a number out of range throws aRangeError, a value that is not a number aTypeError, when the node is created.tomust be an array or a record of finite numbers: atothat is not an object, or a member that is not a number, throws aTypeError; a non-finite member aRangeError, also when the node is created.- Every property in
tomust exist in thefrompassed toplay().
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
anifor any individual tween. - Do share one
aninode (and its tree) between many timelines. Nodes are immutable. - Don't try to animate multiple, unrelated properties in a single
anicall if they belong to different conceptual animations. Instead, use multipleaninodes within aparallelblock.
API Reference
| Name | Type | Description |
|---|---|---|
props.to | G | Target values. Its shape (record or array) defines the animated properties. |
props.duration | number | Seconds, finite and >= 0. |
props.timing | TimingFunction or per-property functions | Defaults to linear(). |
props.id | string | (Optional) Identifier of the node. ani takes one argument: a second argument throws a TypeError. |
Related Components
rafTimeline- The controller required to run the animation.sequence- To play multipleaninodes one after another.parallel- To play multipleaninodes at the same time.