3D animation system in Rust, WebAssembly and WebGL.

Write a scene in a small language, build it once, and draw it on any page along a 3D road. One npm package, no bundler settings.

npm install @i7r.tech/moveby

Loading the track…

time 0.0 s

Launch Night, drawn by the library. It drives itself. Touch the slider, and you drive.

< 1.5 ms per frame

p99 of 90,000 measured frames — CPU pass + GPU pass paired per frame, on an M1 Max in Chrome at 1280×720 — is 1.38 ms, against an 8.30 ms deadline.

Install it →

78 kB gzipped

The JS is 24 kB and the engine is 54 kB, minified, from the installed package. Bundlephobia's headline counts the JS alone.

Check it on bundlephobia ↗

Raw WebGL

No 3D library in the bundle.

See the layers →

How it works

Three files in, one call per frame. Each block below is read from a file that the site's tests check.

Step 1

Describe the scene

Objects and their motion, in moveby-lang.

scene.moveby, lines 12 to 17
// a tree, 8 m right of the road, 30 m ahead
object tree = image(moveby.asset.tree) {
    x = 8, y = 0, z = 30,
    yaw = 0, pitch = 0, roll = 0, scale = 1,
    origin_x = 0.5, origin_y = 0
}
Step 2

Build the track

One command writes the track, the module and its types.

terminal
$ npx moveby build scene.moveby road.json atlas.json
# writes track.bin, scene.wasm and track.d.ts
Step 3

Draw it

Your page owns the input. Pass it on each frame.

driving.ts, lines 9 to 16
function frame() {
  // How far the page is scrolled, from 0 to 1. A page that cannot scroll stays at 0.
  const scrollRange = document.documentElement.scrollHeight - innerHeight;
  const progress = scrollRange > 0 ? Math.min(Math.max(scrollY / scrollRange, 0), 1) : 0;

  scene.draw({ position: progress * trackLength });
  requestAnimationFrame(frame);
}

Three curves from a production ride

These curves come from the scenes of a production scroll-driven ride. Each one has the ride's own formulas, written in the library's object dialect, so it builds with moveby build and runs on the canvas of its row. The plot is sampled from the built module, frame by frame. It is not redrawn from the formulas.

Gravity on a spring-driven clock

scene.moveby
// Gravity on a spring-driven clock. The formulas are the production ride's (18.architecture.moveby, lines 581-600).
// Three names differ from the ride's: x is its `lateral`, y is its `elevationM`, z is its `s`.
dynamic { position }
using { moveby { asset { log } } }

import { spring, motion_linear, motion_projectile } from "../curves/lib/fn.moveby"
import { gentle } from "../curves/lib/engine.moveby"

let anchor = 0

object log_1 = image(moveby.asset.log) {
    z = anchor + 250.01,
    x =
        let t = interpolate position: z - 40.01 .. z - 40.01 + 10 -> 0 .. 3 in
        motion_linear(-30, 60, spring(1, 4, t)),
    y =
        let t = interpolate position: z - 40.01 .. z - 40.01 + 10 -> 0 .. 3 in
        motion_projectile(10, 0, spring(1.4285714286, 4, t)),
    scale = interpolate position: z - 45.01 .. z - 45.01 + 5 * 2.273 -> 0 .. 30 [gentle],
    yaw = 0, pitch = 0, roll = 0,
    origin_x = 0.5, origin_y = 0.5
}

The fall uses real gravity, motion_projectile. Its clock is a spring, and the spring slows to a stop at a fixed time. The fall lasts exactly as long as the clock runs. The constant of the spring is chosen so that the log reaches the road at that moment, with no speed left. Change the constant, and the log either stops short of the road or goes through it.

Loading the track…

Runs the code of this row. It sweeps the curve by itself. Touch the slider, and you drive.

Chaos you can reproduce

scene.moveby
// Chaos you can reproduce. The formulas are the production ride's (12.chaos.moveby, lines 15-31, 33-49, 95-111, 295-297).
// Three names differ from the ride's: x is its `lateral`, y is its `elevationM`, z is its `s`.
dynamic { position }
using { moveby { asset { deploy } } }

import { gentle } from "../curves/lib/engine.moveby"

let anchor = 0

object deploy_1 = image(moveby.asset.deploy) {
    z = anchor + 30,
    x =
        let t = interpolate position: z - 25 .. z - 25 + 50 -> 0 .. 3 in
        swerve_at_random(-45, 20, 0.25, 0.8, 0.6, 5, t),
    y = interpolate position: z - 25 .. z - 25 + 15 * 2.273 -> -20 .. 0 [gentle],
    scale = 5,
    yaw = 0, pitch = 0, roll = 0,
    origin_x = 0.5, origin_y = 0
}

object deploy_2 = image(moveby.asset.deploy) {
    z = anchor + 50,
    x =
        let t = interpolate position: z - 45 .. z - 45 + 50 -> 0 .. 3 in
        swerve_at_random(-45, 20, 0.25, 0.8, 1, 10, t),
    y = interpolate position: z - 45 .. z - 45 + 15 * 2.273 -> -20 .. 0 [gentle],
    scale = 5,
    yaw = 0, pitch = 0, roll = 0,
    origin_x = 0.5, origin_y = 0
}

object deploy_3 = image(moveby.asset.deploy) {
    z = anchor + 70,
    x =
        let t = interpolate position: z - 40 .. z - 40 + 50 -> 0 .. 3 in
        swerve_at_random(-45, 20, 0, 0.8, 1, 8, t),
    y = interpolate position: z - 65 .. z - 65 + 15 * 2.273 -> -20 .. 0 [gentle],
    scale = 5,
    yaw = 0, pitch = 0, roll = 0,
    origin_x = 0.5, origin_y = 0
}

let swerve_at_random k k2 first last width seed _t =
    mix(k, k2, clamp((_t - clamp(0.5 * (first + last) + 0.2 * (last - first) * (sin(1.7 * seed) + sin(4.3 * seed) + sin(2.5 * seed)), first, last)) / width, 0, 1))

// A sign over the road. It is a text object, so the scene holds content for every layer.
object sign = text("CHAOS") {
    x = 0, y = 7, z = 20, yaw = 0, alpha = 1, height = 3,
    color_r = 1, color_g = 0.85, color_b = 0
}

Three deploy boxes swerve across the road at "random" moments. The moment is a sum of sines of a seed. The same seed gives the same moment on every reload and at every frame rate, so every viewer sees the same chaos.

Loading the track…

Runs the code of this row. It sweeps the curve by itself. Touch the slider, and you drive.

One hop, a damped bounce, then the drop

scene.moveby
// One hop, a damped bounce, then the drop. The formulas are the production ride's (6.TA-duty.taDuty.moveby, lines 20-26, 28-55, 227-230).
// Three names differ from the ride's: x is its `lateral`, y is its `elevationM`, z is its `s`.
dynamic { position }
using { moveby { asset { tiger } } }

import { s_to_t, motion_linear, motion_projectile, calc_projectile_v_to_peak_at } from "../curves/lib/fn.moveby"
import { no_wobble, spring_settle, spring_carry_b, spring_carry_v } from "../curves/lib/engine.moveby"

let anchor = 0

// `tiger_teaching.z` is animated, so the declared place (25 m) has its own name: every trigger is
// measured from it, as the engine measures from the declared `z`.
let tiger_teaching_declared = anchor + 25

object tiger_teaching = image(moveby.asset.tiger) {
    z =
        anchor + (if position - anchor < 5 then 25
            else if position - anchor < 20 then motion_linear(25, 4.06559586161 * (10 / 18.868), s_to_t(4.6408941376, 5, 15, position - anchor))
            else motion_linear(35, 19.09859317106, s_to_t(2.09439510239, 20, 45, position - anchor))),
    x =
        let t = interpolate position: tiger_teaching_declared - 20 .. tiger_teaching_declared - 20 + 15 -> 0 .. 4.6408941376 in
        max(9, motion_linear(25, -4.06559586161 * (16 / 18.868), t)),
    y =
        if position - anchor < 5 then 0
        else if position - anchor < 20 then motion_projectile(0, calc_projectile_v_to_peak_at(30), s_to_t(4.9487165931, 5, 15, position - anchor))
        else jump_game_right(s_to_t(2.09439510239, 20, 45, position - anchor)),
    scale =
        let d = position - tiger_teaching_declared in
        if d <= -40 then 0
        else if d <= -20 then spring_settle(0, 0, 6, no_wobble, (d + 40) / 10)
        else
            let b1 = spring_carry_b(0, 0, 6, no_wobble, (-20 + 40) / 10) in
            let v1 = spring_carry_v(0, 0, 6, no_wobble, (-20 + 40) / 10) in
            spring_settle(b1, v1, 2, no_wobble, (d + 20) / 10),
    yaw = 0, pitch = 0, roll = 0,
    origin_x = 0.5, origin_y = 0
}

let jump_game_right _clock =
    if _clock < 1.308996939 then 15exp(-1.2 * _clock) * abs(sin(6 * _clock))
    else 3.1181936452 - 176.4((_clock - 1.308996939)^2)

A projectile hop to a chosen height, then an exponential envelope times the absolute value of a sine, then a free drop that the plot cuts short. The bounces decay. The hop ends higher than the bounce starts, so the plot has a step at the seam.

Loading the track…

Runs the code of this row. It sweeps the curve by itself. Touch the slider, and you drive.

Five more curves, and how the scene follows the slider →

Where to next