timing
Control the rate of change of an animation with timing functions.
A TimingFunction is a core concept that dictates the speed and acceleration of an animation over its duration. It controls how an animation progresses from its start value to its end value, allowing for effects like smooth easing, bouncing, and realistic physical motion.
Timing functions are provided to the timing property of an ani node.
Example
import * as a from "@freestylejs/ani-core";
// This animation will start slow, accelerate, and then slow down at the end.
const myAnimation = a.ani({
to: { x: 500 },
duration: 2,
// Use a built-in bezier curve for an "ease-in-out" effect.
timing: a.timing.bezier({
p2: { x: 0.42, y: 0 },
p3: { x: 0.58, y: 1 },
}),
});Built-in Timing Functions
The library provides several ready-to-use timing functions accessible via a.timing.
| Function | Description |
|---|---|
linear() | A constant rate of change. No acceleration or deceleration. |
bezier(opt) | A cubic bezier curve for classic easing effects. |
ease(), easeIn(), easeOut(), easeInOut() | The standard CSS easing curves, as bezier presets. |
spring(spring, { tolerance? }) | The physical spring, in closed form and real time: it starts moving at the segment's start, keeps the velocity in rafTimeline, and ends when it settles, or at the segment duration (a cap: the landing is then a jump), whichever comes first. |
bezier and the ease* presets solve the curve to about 1e-12 of the progress, and two times an ulp apart give progress values an ulp-scale apart (no jumps), so the same animation evaluates the same wherever a loop is represented.
spring
There is one spring in Ani, { duration, bounce } (the vocabulary of SwiftUI's .spring(duration:bounce:)), and timing.spring is that spring as a timing. The same two numbers drive physics.spring, a layout motion: { type: 'spring' }, follow and Channel.
durationis the perceptual duration in seconds: the time the motion takes to look done. It sets the spring's speed.bounceis strictly between -1 and 1.0is critically damped (no overshoot), positive values overshoot, negative values are overdamped (slower, no overshoot).springalso takes aphysics.spring(...)you already have, and an optional{ tolerance: { position?, speed? } }, the displacement and speed in the values' units under which the spring counts as settled (1e-3each).
Properties of a segment driven by spring:
- Real time. The value at a time is an exact closed form from the segment's start: independent of the frame rate and the same after any
seek. The WAAPI compiler samples the same evaluation (every 1/60 s, each keyframe equal to the RAF value at its time), and the browser interpolates linearly between samples. - Velocity. With
rafTimeline, playing over a running spring keeps its velocity, so retargeting is smooth, andvelocitypassed toplay()seeds it explicitly, for example the pointer velocity after a drag. AwaapiTimelinerun starts from rest: it keeps only the position across an interruption. - The segment ends at the first of two times and lands on
tothere. The exact settle time, fromSpring.settleTime: the spring's displacement and speed are within thetolerance, so the landing is a step of at most the tolerance. Or the segment'sduration, a hard cap: the landing is a jump of whatever the spring had left. Choose a segmentdurationat or above the settle time for a landing within the tolerance. A spring never speeds up or slows down to fit the segment: change the spring's owndurationto change its speed. A timeline made only of springs may therefore end before its nominalduration. - State lives inside the timeline (per segment and property), so one
springinstance can be shared by many segments, properties and timelines. - Invalid input throws: a number out of range is a
RangeError, a value that is not a number (also a numeric string) aTypeError. The{ m, k, c }form of earlier versions is removed and throwsANI_REMOVED_API; see the migration guide.
import * as a from "@freestylejs/ani-core";
// A modal that overshoots slightly. The spring looks done after 0.35 s;
// the segment duration is the cap if the values are large.
const modalAppear = a.ani({
to: { scale: 1, opacity: 1 },
duration: 0.8,
timing: a.timing.spring({ duration: 0.35, bounce: 0.2 }),
});Fling and release
Because the spring keeps the velocity, it suits animations that react to user input. A draggable card that can be "flung" springs back with a momentum that depends on how fast it was thrown.
import * as a from "@freestylejs/ani-core";
const cardReturn = a.ani({
to: { x: 0, y: 0 },
duration: 2, // a cap; the spring usually settles earlier
timing: a.timing.spring({ duration: 0.45, bounce: 0.3 }),
});
const cardTimeline = a.rafTimeline(cardReturn);
// Values measured while the card was dragged (pixels, pixels per second).
const currentDragX = 120;
const currentDragY = 40;
const pointerVelocityX = 900;
const pointerVelocityY = -300;
// On mouse up, play from the card's current position with the release velocity.
cardTimeline.play({
from: { x: currentDragX, y: currentDragY },
velocity: { x: pointerVelocityX, y: pointerVelocityY },
});If an animation must take exactly a given time, use a curve (bezier, ease*, linear): a spring has no fixed length.
From SwiftUI
| SwiftUI | Ani |
|---|---|
.spring(duration: d, bounce: b) | { duration: d, bounce: b } |
.spring(response: r, dampingFraction: ζ) | duration = r, bounce = 1 − ζ for ζ ≤ 1, bounce = 1/ζ − 1 for ζ > 1 |
.smooth, .snappy, .bouncy | bounce 0, 0.15, 0.3 (the duration is 0.5 unless you set it) |
- Dynamic ball example
Move your mouse
The balls will follow you.
Related Components
ani- The animation node where timing functions are applied.Custom Timing Functions- Learn how to create your own timing logic.