Custom Timing Functions
Extend the animation system with custom logic by creating your own timing functions.
The animation library is designed to be extensible. You can create custom animation behaviors by writing your own timing functions. This allows you to go beyond simple easing curves and implement physics-based motion, procedural animations, or any other time-based logic.
The TimingFunction Abstract Class
To create a custom timing function, extend TimingFunction and implement its step method.
// exported by '@freestylejs/ani-core'
export abstract class TimingFunction {
/** Declare `true` for timings that integrate over time (physics). */
public readonly stateful?: boolean;
/**
* Calculates the value at `time`.
* @param time Seconds since the segment started.
* @param context Contextual information for the current frame.
*/
public abstract step(
time: number,
context: TimingFunctionContext
): {
value: number;
endOfAnimation: boolean;
};
}The step method is called for each property being animated. It returns the value for that property and signals when the property is finished.
The TimingFunctionContext
| Name | Type | Description |
|---|---|---|
dt | number | Time elapsed since the previous evaluation, in seconds. Do not rely on it for deterministic results. |
from | number | The value at the start of the segment (a fixed snapshot). |
to | number | The target (end) value of the animation property. |
duration | number | The duration of the ani node, in seconds. |
tolerance | number | (Optional) A small value to determine if the animation is finished. |
state | object | (Optional) Private mutable slot. Only provided when stateful is true. |
velocity | number | (Optional) Initial velocity in units per second, to seed a stateful timing. |
Example: A Simple "Snap To" Function
This example snaps the value to its to position and ends.
import * as a from "@freestylejs/ani-core";
import { TimingFunction, TimingFunctionContext } from "@freestylejs/ani-core";
class SnapTo extends TimingFunction {
public step(time: number, context: TimingFunctionContext) {
return { value: context.to, endOfAnimation: true };
}
}
const myAnimation = a.ani({
to: { x: 500 },
duration: 1, // not used by SnapTo, but still the segment length in the plan
timing: new SnapTo(),
});
const myTimeline = a.rafTimeline(myAnimation);
myTimeline.onUpdate(({ state }) => {
console.log(state.x); // 500 on the first frame
});
myTimeline.play({ from: { x: 0 } });Stateless and stateful timings
By default a timing is a pure function of time (and the context). The timeline may evaluate it in any order and as often as it needs (for example when you seek), so it must not keep progress on the instance.
A timing that integrates over time (physics) declares stateful = true. The timeline then gives it a private context.state object per timeline, segment and property. Keep the integrator there, never on the instance, so one instance can be shared by many segments, properties and timelines.
A stateful timing must:
- Re-initialize from
context.from(andcontext.velocity) whenstateis new, or whentimegoes backwards (seek). - Give the same value for the same
time, regardless of how frames were sliced. Integrate in fixed steps, not withdt. - Optionally publish
state.velocity(units per second). The timeline reads it forvelocityand velocity-continuous retargeting.
import { TimingFunction, type TimingFunctionContext } from "@freestylejs/ani-core";
class Follow extends TimingFunction {
public override readonly stateful = true;
constructor(private readonly rate = 8) {
super();
}
public step(time: number, context: TimingFunctionContext) {
const h = 1 / 120; // fixed substep: independent of frame rate
const s = context.state!; // provided because `stateful` is true
if (!s.init || time < s.time) {
s.init = true;
s.time = 0;
s.x = context.from;
}
if (time >= context.duration) {
s.velocity = 0;
return { value: context.to, endOfAnimation: true };
}
while (s.time + h <= time) {
s.x += (context.to - s.x) * (1 - Math.exp(-this.rate * h));
s.time += h;
}
s.velocity = (context.to - s.x) * this.rate;
const tolerance = context.tolerance ?? 1e-3;
return {
value: s.x,
endOfAnimation: Math.abs(context.to - s.x) <= tolerance,
};
}
}The built-in spring is such a timing: it keeps its state in the timeline, settles by physics or at the segment duration, and is a closed form of the segment time, so it is deterministic under seek.
A timeline whose animated properties are driven only by stateful timings ends as soon as all of them report endOfAnimation, which can be before the nominal duration.
When to Use
- Do create a custom timing function when you need logic beyond standard easing curves (physics, procedural noise).
- Do declare
stateful = trueand usecontext.statefor anything that remembers previous frames. - Don't store per-run values (position, velocity) on the instance.
- Don't implement simple easing curves this way. Use the built-in
bezierfunction or the CSS presets (ease(),easeIn(), ...).