ruanim-web 0.1.0

WebAssembly exports for the RuAnim animation core, without bindgen or outside code.
Documentation
  • Coverage
  • 100%
    20 out of 20 items documented0 out of 19 items with examples
  • Size
  • Source code size: 36.6 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 151.5 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 2s Average build duration of successful builds.
  • all releases: 2s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • LEVOGNE/ruanim
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • LEVOGNE
ruanim-web-0.1.0 has been yanked.

RuAnim

Animation, smooth-scroll and gesture core in pure, safe Rust. No dependencies, no unsafe, no platform code.

The core knows nothing about the platform. The host sends commands and timestamps in (World::dispatch), the core answers with numbers (World::values, World::changed) and events (World::drain). The same core runs natively, as WebAssembly in the browser, and behind an MCP server for AI agents.

  • Smooth scroll with wheel and touch input, nested scroll areas, anchors, snapping and scroll-linked watches.
  • Tweens and timelines with keyframes, stagger, labels, markers, repeat, alternate, speed and the easing names of anime.js.
  • Physics: springs, decay, rubber bands and frame-rate-independent damping.
  • Drag and gestures: bounds, axis lock, snapping, throw, 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

cargo add ruanim

Minimum supported Rust version: 1.88 (edition 2024).

Quick start

use ruanim::{Command, Dimensions, Invalid, Options, Request, Scope, ScrollCommand, Target, World};

fn main() -> Result<(), Invalid> {
    let page = Dimensions::new(800.0, 600.0, 800.0, 3000.0);
    let mut world = World::new(Options::default(), page, 0.0)?;
    let goal = Target::Value(1000.0);
    world.dispatch(ScrollCommand::ScrollTo(Scope::Root, goal, Request::default()));
    let mut time = 0.0;
    while world.needs_frame() && time < 5000.0 {
        world.dispatch(Command::Frame(time));
        time += 1000.0 / 60.0;
    }
    assert_eq!(world.root().scroll(), 1000.0);
    Ok(())
}

Frame times, durations of clips and pointer timestamps are in milliseconds. Physical motion (springs, decay, Motion) is set up in seconds, like Lenis. Positions are in pixels.

In a browser the host provides:

  • the time as performance.now() or the requestAnimationFrame timestamp, in milliseconds since page start, not Date.now();
  • 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
scroll smooth scroll, nested areas, snap, watches
drag dragging, gesture recognizers, edge scrolling
form SVG paths, morph, flip, text
lib World, the switchboard, and Remote, the text commands

Every operation is a command named area.verb (for example world.frame, lane.remove_many) and runs through World::dispatch. Every reply is a Reply, every failure to build is an Invalid.

Text commands

Remote::run(&mut world, line) takes one JSON object per line and answers with one JSON object. The "command" field carries the same area.verb name as the typed command; the other fields are its values. Handles travel as decimal strings.

{"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, gestures, snapping, watches, paths and forms 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"]}

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. Remote::NAMES lists all 101 commands a line can carry; a test keeps it equal to the commands of the typed API.

Every event has fixed fields: "area" (scroll, clip, watch, drag, gesture, world), "signal" in snake_case, the handle of its source as a decimal string 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":"scroll","signal":"flags","source":"root","flags":["scrolling","smooth"]}

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 ms pass and returns sampled scroll and lane values
cargo install ruanim-mcp
{ "mcpServers": { "ruanim": { "command": "ruanim-mcp" } } }

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; the core and the MCP server forbid it.

cargo build --release -p ruanim-web --target wasm32-unknown-unknown
const { instance } = await WebAssembly.instantiateStreaming(fetch("ruanim_web.wasm"));
const e = instance.exports;
const world = e.ruanim_new(innerWidth, innerHeight, innerWidth, document.body.scrollHeight);
const send = (command) => {
  const bytes = new TextEncoder().encode(JSON.stringify(command));
  const at = e.ruanim_buffer(bytes.length);
  new Uint8Array(e.memory.buffer, at, bytes.length).set(bytes);
  const length = e.ruanim_run(world, at, bytes.length);
  e.ruanim_release(at, bytes.length);
  return JSON.parse(new TextDecoder().decode(new Uint8Array(e.memory.buffer, e.ruanim_output(world), length)));
};
send({ command: "scroll.to", value: 1000 });
const loop = (time) => {
  const moving = e.ruanim_frame(world, time);
  scrollTo(0, e.ruanim_scroll(world));
  if (moving) requestAnimationFrame(loop);
};
requestAnimationFrame(loop);
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 snapshot 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.

Benchmarks

Measured on an Apple M5 Max with Rust 1.100.1, --release, one test at a time on an otherwise idle machine. Each benchmark is a test with a budget: it fails when the time per run exceeds 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.7 ns 30 ns
curve.named one named easing 5.0 ns 10 ns
curve.spring one spring easing 6.1 ns 25 ns
curve.bezier one cubic Bezier easing 8.9 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 28 ns 400 ns
world.input.path6 one wheel event through six nested layers 34 ns 400 ns
gesture.move.2 one two-finger move 35 ns 150 ns
world.frame one world frame with smooth scroll and wheel input 38 ns 150 ns
drag.move one pointer move on a drag 43 ns 60 ns
rubber.frame one frame of a rubber-band scroll 46 ns 150 ns
form.morph.256 one morph frame, 256 points 96 ns 3 µs
remote.frame one text command: parse, frame, answer 206 ns 1 µs
form.scramble.256 one scramble frame, 256 characters 0.87 µs 10 µs
form.flip.1000 one FLIP frame, 1,000 nodes 1.7 µs 30 µs
drag.flight.256 one frame of 256 thrown drags 6.1 µs 20 µs
watch.frame.1000 one frame with 1,000 scroll watches 8.7 µs 30 µs
clip.seek.1000 one seek of a clip with 1,000 lanes 9.5 µs 60 µs
clip.frame.1000 one frame of a clip with 1,000 lanes 9.7 µs 30 µs
path.parse parse and flatten one SVG path 11 µs 100 µs

Run them yourself:

cargo test --release -p ruanim messung_ -- --ignored --test-threads=1 --nocapture

Compared with anime.js

The same four loads with 10,000 targets, 61 frames at 60 Hz, best of seven runs. anime.js 5.0.0 in Node 24 with a virtual clock; RuAnim natively (--release, fat LTO) and as WebAssembly in the same Node. Each cell: setup in ms / one frame in ms.

Load RuAnim native RuAnim WebAssembly anime.js
one animation, 10,000 targets 0.65 / 0.109 0.88 / 0.127 1.57 / 0.078
10,000 separate animations 4.74 / 0.227 6.40 / 0.349 3.51 / 0.295
three keyframes with stagger 1.33 / 0.063 2.03 / 0.104 4.36 / 0.224
timeline with 10 children 0.86 / 0.050 1.43 / 0.082 1.58 / 0.010

RuAnim is faster to set up in three of four loads and faster per frame with many animations and with keyframes. anime.js is faster per frame for a single animation over many targets and for timelines: a running RuAnim timeline keeps every lane it owns up to date until its end, also those of finished children, so that values never flicker between children that share a lane. RuAnim's results are bit-identical natively and as WebAssembly. The comparison harness loads anime.js and therefore lives outside this repository.

Quality

  • #![forbid(unsafe_code)] in the core and the MCP server; unsafe only 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_ROUNDS and RUANIM_FUZZ_SEED widen them.
  • Guard tests keep the structure honest: each area uses only lower areas, every command has a text name, no stray comments, no panicking calls in production code.
  • #![deny(missing_docs)]: every public item is documented.
cargo test --workspace
MIRIFLAGS="-Zmiri-deterministic-floats" cargo +nightly miri test -p ruanim-web

License

Licensed under either of

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.