RuAnim
Animation, smooth-scroll and gesture core in pure, safe Rust. No dependencies, no unsafe in the core, no platform code.
RuAnim does the maths of motion and leaves drawing to the host. The host sends commands and timestamps in, the core answers with numbers and events. The same core runs natively, as WebAssembly in the browser, and behind an MCP server for AI agents.
Features
- Smooth scroll: wheel and touch input, nested scroll areas, anchors, snapping, scroll-linked watches and reduced motion.
- Tweens and timelines: keyframes, stagger, labels, markers, repeat, alternate, speed and nested timelines.
- Easing: the named curves known from JavaScript animation libraries (
linear,inQuad,outBack,inOutElasticand more), cubic Bezier, steps and springs. - Physics: springs, decay, rubber bands and frame-rate-independent damping.
- Drag and gestures: bounds, axis lock, snapping, throw with friction, edge scrolling, tap, double tap, long press, swipe, pinch and turn.
- Shapes: SVG path parsing, motion along a path, morph, stroke drawing, text scramble, line splitting and FLIP layout transitions.
- Text commands: every operation is also a JSON line (101 commands) for scripts, tests, WebAssembly hosts and AI agents.
Install
Minimum supported Rust version: 1.88 (edition 2024).
Quick start
use ;
How it works
A World holds everything that moves. The host drives it with three calls:
World::dispatch(command)for every input and every frame. Each operation is a command namedarea.verb(for examplescroll.to,clip.play,drag.move) and answers with aReply.World::values()andWorld::changed()after a frame. Animated values live in lanes: output channels of one to four numbers (a scalar, an angle or a vector such as a position or a color). The host reads only the lanes that changed and applies them to whatever it draws.World::drain()for events: a clip began or completed, a marker passed, a watch was entered, a drag was released, a gesture was recognized.
Objects are addressed by Handles. A handle carries a generation, so a handle of a removed object never reaches a new one.
Units:
- Frame times, clip durations and pointer timestamps are in milliseconds.
- Physical motion (springs, decay, damped
Motion) is set up in seconds. - Positions and distances are in pixels; velocities of pointers in pixels per millisecond.
In a browser the host provides the time as performance.now() or the requestAnimationFrame timestamp (milliseconds since page start, not Date.now()), and the pixel grid as Options::pixel = 1 / devicePixelRatio, so the scroll lands on real device pixels.
| Area | Contents |
|---|---|
base |
maths, clock, handles, easing, spring, decay, Animate, color, matrix, lanes, JSON |
tween |
clips, timelines, stagger, followers, states |
scroll |
smooth scroll, nested areas, snap, watches |
drag |
dragging, gesture recognizers, edge scrolling |
form |
SVG paths, morph, stroke, flip, text |
lib |
World, which connects the areas, and Remote, the text commands |
Examples
Text commands
Remote::run(&mut world, line) takes one JSON object and answers with one JSON object. The "command" field carries the area.verb name; the other fields are its values. Handles travel as decimal strings, because 64-bit integers do not fit into JSON numbers.
{"command":"scroll.to","value":1000} -> {"ok":true}
{"command":"lane.add","start":0} -> {"ok":true,"handle":"4294967296"}
{"command":"clip.create","tracks":[{"lanes":["4294967296"],"to":100,"duration":500,"easing":"outQuad"}]}
-> {"ok":true,"handle":"4294967296"}
{"command":"world.frame","time":16.7} -> {"ok":true}
{"command":"world.frame"} -> {"ok":false,"error":"missing or invalid field","field":"time"}
Input, drags and gestures work the same way:
{"command":"scroll.input","kind":"wheel","dy":120} -> {"ok":true,"flags":["prevent"]}
{"command":"drag.add","axis":"horizontal"} -> {"ok":true,"handle":"4294967296"}
{"command":"drag.down","drag":"4294967296","time":0,"x":0,"y":0}
-> {"ok":true}
{"command":"drag.move","drag":"4294967296","time":16,"x":40,"y":30}
-> {"ok":true,"flags":["prevent"]}
A failed command names the field that was missing or invalid, so a script or an agent can correct exactly that value.
| Area | Commands |
|---|---|
world |
frame, pause, resume, hidden, speed, fps, pace |
scroll |
to, input, start, stop, lock, unlock, reset, resize, viewport, measure, native, shown |
snap |
init, add, element, update, remove, go, next, previous, start, stop |
lane |
add, set, remove, remove_many, precision |
clip |
create, open, add, label, marker, seal, play, pause, resume, restart, reverse, seek, rate, stretch, complete, cancel, revert, refresh, remove |
group |
pause, resume, revert, remove |
follow, state |
follow.motion, follow.set, follow.remove, state.define, state.go |
watch |
add, update, link, remove |
drag |
add, down, move, up, cancel, stop, reset, set, bounds, in_view, enable, remove |
gesture |
add, down, move, up, cancel, remove |
path, form |
path.add, path.remove, form.trail, form.morph, form.stroke, form.scramble, form.lines, form.flip, form.remove |
| page | anchor.go, link.click, nested.detach, pointer.down, selection.set, overflow.set, motion.reduce, trace.set |
A thrown drag takes its friction and its settling spring as numbers too, for example "decay":{"exponential":0.2} or {"brake":0.01}, and "spring":{"bounce":0.2,"duration":400} or {"mass":1,"stiffness":200,"damping":20}. Function values (the modifiers of drags, stagger and tracks) cannot travel as text; they exist only in the typed API.
Remote::read(&mut world) returns the clock, whether frames are still needed, the root scroll, all lane values, the changed lanes and the events since the last read. Every event has fixed fields: "area", "signal" in snake_case, the handle of its source and its values as their own fields:
{"area":"drag","signal":"moved","drag":"4294967296","position":[40,0]}
{"area":"scroll","signal":"virtual","source":"root","dx":0,"dy":120}
{"area":"clip","signal":"completed","clip":"4294967296"}
Remote::NAMES lists all commands; a test keeps it equal to the typed API.
AI agents (MCP)
The crate ruanim-mcp is a Model Context Protocol server over stdio (JSON-RPC 2.0, one message per line). It holds one world and offers four tools:
| Tool | Does |
|---|---|
send |
runs one text command; the arguments are the command object |
frame |
advances count frames of 1000/60 ms and returns the state |
read |
returns the state and drains the events |
simulate |
runs frames until rest or until ms have passed, and returns sampled scroll and lane values |
From a checkout of this repository, "command": "cargo", "args": ["run", "-q", "-p", "ruanim-mcp"] works without installing.
Browser (WebAssembly)
The crate ruanim-web builds the core as a WebAssembly module with plain exports, without bindgen or generated glue. It is the only place with unsafe, limited to pointers and memory.
const = await ;
const e = instance.;
const world = e.;
const ;
;
const ;
;
| Export | Does |
|---|---|
ruanim_new(width, height, scroll_width, scroll_height) |
creates a world, null on non-finite sizes |
ruanim_free(world) |
frees it |
ruanim_buffer(len), ruanim_release(at, len) |
memory for a command line |
ruanim_run(world, at, len) |
runs a text command, returns the answer length |
ruanim_read(world) |
stores the state as answer, returns its length |
ruanim_output(world) |
start of the last answer |
ruanim_frame(world, time) |
advances to time ms, returns 1 while frames are needed |
ruanim_scroll(world) |
root scroll position |
ruanim_values(world), ruanim_values_len(world) |
lane values as f64, without copying |
The release module is about 575 KB.
Performance
Measured with --release, one test at a time on an idle machine; absolute times depend on the hardware. Each benchmark is a test with a budget: it fails when a run takes longer than the budget, so a slowdown breaks the build instead of going unnoticed.
| Benchmark | Work per run | Time | Budget |
|---|---|---|---|
animate.timed |
one timed value, one frame | 3.2 ns | 30 ns |
animate.lerp |
one damped value, one frame | 3.5 ns | 30 ns |
curve.named |
one named easing | 5.1 ns | 10 ns |
curve.spring |
one spring easing | 6.1 ns | 25 ns |
curve.bezier |
one cubic Bezier easing | 9.0 ns | 40 ns |
path.point |
one point at a distance on a 520-point path | 23 ns | 60 ns |
snap.step |
one snap decision among 1,000 points | 31 ns | 400 ns |
gesture.move.2 |
one two-finger move | 36 ns | 150 ns |
world.input.path6 |
one wheel event through six nested layers | 37 ns | 400 ns |
world.frame |
one world frame with smooth scroll and wheel input | 39 ns | 150 ns |
drag.move |
one pointer move on a drag | 45 ns | 60 ns |
rubber.frame |
one frame of a rubber-band scroll | 48 ns | 150 ns |
form.morph.256 |
one morph frame, 256 points | 88 ns | 3 µs |
remote.frame |
one text command: parse, frame, answer | 216 ns | 1 µs |
form.scramble.256 |
one scramble frame, 256 characters | 0.86 µs | 10 µs |
form.flip.1000 |
one FLIP frame, 1,000 nodes | 1.8 µs | 30 µs |
drag.flight.256 |
one frame of 256 thrown drags | 6.5 µs | 20 µs |
watch.frame.1000 |
one frame with 1,000 scroll watches | 9.4 µs | 30 µs |
clip.frame.1000 |
one frame of a clip with 1,000 lanes | 9.4 µs | 30 µs |
clip.seek.1000 |
one seek of a clip with 1,000 lanes | 9.6 µs | 60 µs |
path.parse |
parse and flatten one SVG path | 12 µs | 100 µs |
Results are bit-identical natively and as WebAssembly.
Quality and safety
#![forbid(unsafe_code)]in the core and the MCP server;unsafeonly in the WebAssembly exports, checked with Miri (-Zmiri-strict-provenance).- The full core test suite also runs under Miri without undefined behaviour.
- Fuzz tests for JSON, text commands, the MCP protocol and the WebAssembly exports run with every
cargo test;RUANIM_FUZZ_ROUNDSandRUANIM_FUZZ_SEEDwiden them. - Every bug fix comes with a regression test that fails without the fix.
- Guard tests keep the structure honest: each area uses only lower areas, every command has a text name, no panicking calls in production code.
#![deny(missing_docs)]: every public item is documented.
MIRIFLAGS="-Zmiri-deterministic-floats"
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.