Skip to main content

Crate ashlar_motion

Crate ashlar_motion 

Source
Expand description

Headless motion maths for ashlar: closed-form springs, curves, animated values that keep their velocity when retargeted, a reduced-motion policy per kind of value, OKLab/OKLCH colour and gesture helpers.

No UI, no dependencies, no allocation; builds for wasm32-unknown-unknown. The UI layer (ashlar-ui) owns the clock and the frame requests; this crate only answers “where is this value at time t, and when does it stop?”.

use ashlar_motion::{Animated, Class, Curve, MotionPrefs, Spring};

let prefs = MotionPrefs::DEFAULT;
let mut x = Animated::new(0.0_f32, Class::Spatial);
x.animate_to(100.0, Spring::new(0.3, 0.1).into(), 0.0, &prefs);
let mid = x.sample(0.1);
assert!(mid > 0.0 && mid < 110.0);
// retarget mid-flight: starts from where it is, at the speed it has
x.animate_to(-50.0, Curve::DEFAULT, 0.1, &prefs);
assert_eq!(x.sample(0.1), mid);
// settled values are exactly their target, and stop asking for frames
assert_eq!(x.sample(x.settle_at()), -50.0);
assert!(x.is_settled(x.settle_at()));

Re-exports§

pub use tokens::Pair;

Modules§

color
Colour on linear floats: OKLab and OKLCH, premultiplied mixing, sRGB bytes and WCAG contrast.
gesture
Gesture helpers: velocity from pointer samples, where a fling comes to rest, and rubber-band resistance past an edge.
tokens
Named motion tokens: enter/exit spring pairs. Exits are 10–25% shorter than enters, so things leave a little faster than they arrive.

Structs§

Animated
A value that moves toward its target along a Curve. Time is in seconds on the caller’s clock (f64), the same clock for every call.
Easing
A cubic-bezier easing from (0, 0) to (1, 1) through two control points. The x coordinates are clamped to 0..=1 (so time only moves forward) and the y coordinates to −1..=2.
Edges
A rectangle by its four edges, in points. Animating by edges keeps an edge that does not move still, where animating by position and size would wobble it.
Eps
Settle thresholds: a motion is settled once its distance to the target stays within pos and its speed within vel (units per second) for good.
MotionPrefs
App-wide motion policy, plain data. The UI layer reads it; Animated takes it per retarget.
Spring
A damped spring of unit mass, set by (duration, bounce). The fields are private so the invariants hold: the duration is 0 (no spring, see Spring::NONE) or within Spring::MIN_DURATION..=Spring::MAX_DURATION, and the bounce is within -MAX_BOUNCE..=MAX_BOUNCE.

Enums§

Class
The kind of a moving value. It sets how close counts as settled and what reduced motion does. Effect classes (opacity, colour) never overshoot: their samples stay between the start and the target.
Curve
How a value moves to its target. Every curve starts from the value’s current position and velocity, so a retarget mid-flight never jumps or snaps its speed.

Constants§

DEFAULT_PIXELS_PER_POINT
The display density Animated assumes until told otherwise: 2, so spatial values settle within a quarter of a physical pixel on a high-density display too.
MAX_LANES
The most lanes a value can have.
MAX_SETTLE
The longest settle time, in seconds. A value still moving then snaps to its target.

Traits§

Animatable
A value that animates lane by lane. Each lane moves independently along the same curve.

Type Aliases§

Lanes
The lanes of a value; lanes past Animatable::LANES are unused (0).