Quick Start
Welcome to the moveby documentation! This page gives you an introduction to 80% of the moveby concepts that you will use on a daily basis.
A moveby page starts as three small files that you write. The road file says where the road goes: its bends, its hills, its lanes, and the fields on each side. The scene says where the camera rides and what stands beside the road. The atlas file says how to cut your images into trees, field tiles and letters.
moveby build reads the three files and checks them. If
something is wrong, it stops and tells you the file, the line and the
reason. If all is well, it writes a track, a scene module, and a TypeScript type for the scene's inputs.
Your page loads the track and the scene module, then draws one frame at a time. For each frame, it tells the scene where the rider is on the road. When you change a source file, you build again and reload. This page follows the same order, one file at a time.
Installing moveby
moveby is one package. It contains the compiler command and the runtime that draws a track in the browser.
$ npm install @i7r.tech/movebyThe package ships one prebuilt WebAssembly module, the engine, with every
layer inside it. Your scene becomes a second module when you build it. You
do not need Rust. You do not add a plugin to your bundler, and you do not
configure raw shader imports. The shaders are already inside the JavaScript.
The moveby command needs Node 22 or later.
moveby draws with raw WebGL and its own WebAssembly module. It has no graphics library underneath. This keeps the time per frame and the memory footprint small.
The engine module is one file for every page, and a page that draws only a road still loads all of it. In this version it is 142,835 bytes raw.
Writing a road file
A road file describes the ground the ride runs on: the route, its lanes, and the fields beside it. It is data, not code, so it is plain JSON.
The smallest road file has one segment:
{
"version": 1,
"route": { "segments": [{ "length": 300 }] }
}This is a complete road. It is 300 m long, straight and flat. It has two lanes, with a dashed white line between them.
Drawing a road
Draw a road as a list of segments, one after the other. Every segment has a length in metres. A segment can also turn by an angle in degrees, and rise by a height in metres. Then add lanes and fields along the
road:
{
"version": 1,
"route": {
"segments": [
{ "length": 120 },
{ "length": 60, "turn": 45 },
{ "length": 150, "rise": 6 },
{ "length": 80, "turn": -90, "rise": -2 },
{ "length": 200 }
]
},
"lanes": [
{ "from": 0, "count": 2, "color": "#ffd400" },
{ "from": 180, "count": 3, "divider": "solid" },
{ "from": 400, "count": 2, "dash": [2, 6] }
],
"fields": {
"tiles": { "grass": 0, "sand": 1, "rock": 3 },
"along": [
{ "from": 0, "left": "grass", "right": "grass" },
{ "from": 250, "right": "sand" },
{ "from": 450, "right": "rock" }
]
}
}In the above example:
{ "length": 60, "turn": 45 }turns 45° to the right over 60 m. The turn is even along the whole segment. Later,"turn": -90turns 90° to the left. The road file reference draws which wayturngoes.{ "length": 150, "rise": 6 }climbs 6 m. A negativerisegoes down. moveby rounds each joint between two slopes, so a hilltop has no sharp corner."lanes": two lanes with a yellow line from 0 m, three lanes with solid lines from 180 m, and two lanes again from 400 m, with shorter dashes."fields":tilesgives a name to a cell of the field atlas. Grass covers both sides from 0 m. From 250 m the right side is sand. From 450 m the right side is rock, and the left side keeps its grass.
- Every distance is in metres along the road. That distance is called
s. - A list along the road is a list of changes. Each
entry starts at its
fromand lasts until the next entry, or to the end of the road. You never write an end. - An entry never copies the entry before it. A key you leave out takes its default.
Tracing a real road
To follow a real road, give its points from a map in place of segments. Each
point has a latitude and a longitude in degrees. For hills, give every point
a height in metres.
{
"version": 1,
"route": {
"gps": [
{ "lat": 13.70015, "lng": 100.76719, "height": 2.0 },
{ "lat": 13.69666, "lng": 100.76627, "height": 2.4 },
{ "lat": 13.69653, "lng": 100.76624, "height": 3.1 }
]
}
}moveby joins each pair of points with an arc, and measures s along the result. The lanes and the fields use s, so they work
the same way with either kind of route.
Give a height to every point, or to none of them. If only
some points have a height, the build stops with ROUTE_HEIGHT.
moveby never guesses a missing height, because a guess of 0 m would put a
cliff in the road.
$ npx moveby build scene.moveby road.json
road.json#/route/gps ROUTE_HEIGHT 2 of 3 points have a height: give a height on every point, or on noneDescribing a scene
A scene describes three things: the camera, the scenery, and the text. You write
it as a program in moveby-lang, a small language over numbers, in a .moveby file. The camera rides the road's route, and the scene sets
only how far to the side and how high. The scenery and the text stand in world
space.
// scene.moveby: the camera, one tree and one sign.
// the number the page gives every frame
dynamic { position }
// the look the page gives once, at load
using { moveby { asset { tree } } }
// the camera: on the centreline, 2 m up
out field camera_x = 0
out field camera_y = 2
// 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
}
// a sign over the road, 45 m ahead
object hello = text("HELLO") {
x = -12, y = 10, z = 45, yaw = 0,
height = 6, alpha = 0,
color_r = 1, color_g = 1, color_b = 1
}
// the sign appears once position passes 20
WHEN position > 20 { hello.alpha = 1 }Loading the track…
position 0 mThe canvas plays by itself. Drag the slider to move position: past 20 the sign appears.
In the above example:
dynamic { position }names the number your page passes in each frame: how far along the road the camera is, in metres.using { … }names what it passes once, here the look of the tree. The scene's inputs shows both.out field camera_xandcamera_yset the camera's offsets from the route:xto the side (positive is right) andyabove the road's surface. The camera standspositionmetres along the route and faces along the road.object tree = image(moveby.asset.tree) { … }places one tree.imageis its kind, and the argument names the look.object hello = text("HELLO") { … }writes a sign over the road, 45 m ahead, with letters 6 m tall.xis the left edge of the word, sox = -12moves it left, over the road.alpha = 0hides it at the start.WHENchanges a value while a condition holds. Oncepositionpasses 20,hello.alphais 1 and the sign shows.- An object lists every property of its kind. If you leave one out, the build stops and names it.
- An
out fieldother thancamera_xandcamera_yis an error.
A scene can do much more: functions of your own, values that follow the clock, and animations that start at a trigger. Scenes in moveby-lang shows each one.
Every page has a road file with at least a route, because the camera rides
it. Objects and signs never read the road. They meet the route only in
world space: the route starts at the origin and faces +Z, and objects use x to the right, y up and z forward,
in metres.
Cutting the images
atlas.json says how your images are cut: the grid of the sprite
sheet, the grid of the field tiles, and the font of the signs. Each key is optional.
Leave the file out when your page has no image, no field and no sign.
{
"sprites": { "cols": 8, "rows": 8, "pxWidth": 128, "pxHeight": 128 },
"fields": { "cols": 2, "rows": 2, "pxWidth": 128, "pxHeight": 128 },
"font": "minecraft_fnt.json"
}The field tiles' grid is in this file, not in road.json. The
road names a tile by its cell, and the atlas says how many cells there are.
Compiling a track
The compiler reads three files and writes three, side by side:
$ npx moveby build scene.moveby road.json atlas.jsontrack.bin, the track: the road, the atlas grids and the font. It lists no objects: they come from the scene module.scene.wasm, the scene module: the compiled scene. The page calls it once in every frame.track.d.ts: the scene's dynamic inputs as a TypeScript type. The next section shows why it matters.
Leave atlas.json out when your page has no asset, no sign and
no field tiles. Add --out dist to write the three files into a folder. The compiler
runs in Node. It needs no browser, so it can run in CI.
If the scene has a mistake, the compiler stops and names it. This scene has 20m in it:
dynamic { position }
out field camera_x = 0
out field camera_y = 2
object sign = text("HELLO") {
x = 0, y = 6, z = 20m, yaw = 0, alpha = 1, height = 3,
color_r = 1, color_g = 1, color_b = 1
}$ npx moveby build scene.moveby road.json
scene.moveby SCENE_COMPILE m - unknown name `m`The message names the word at fault. A diagnostic can also name a line. The compiler exits with status 1 and leaves the earlier outputs as they were.
The scene's inputs
A scene has two kinds of input. A dynamic input is a number your page gives every frame. A using input is a number it gives once, when the scene loads. Nothing else reaches the scene: it has no clock and no speed of its own.
This is the type that moveby build wrote for the scene above:
// Generated by moveby from the scene module. Do not edit: build the scene again.
/** The dynamic inputs, one number each per frame: `scene.draw(dynamic)`. */
export interface SceneDynamic {
position: number;
}
/** The using inputs the page gives once: `loadTrack(trackBytes, sceneBytes, { using })`. An asset is not among them: `asset()` declares it. */
export interface SceneUsing {
}position is a fixed name: every scene must declare it, because
the camera needs it. The type follows the scene. Add hover to dynamic { … }, and the next build adds hover: number to SceneDynamic. TypeScript then marks each draw() call that does not pass it.
The same inputs draw the same picture, with one exception: a WHEN that reads delta remembers when its condition became true. scene.reset() forgets
it, for a replay from the start.
A page in plain JavaScript gets no type check. So draw() checks each input while the page runs. If one is missing, or is not a finite
number, it throws SCENE_DYNAMIC and names it. It never fills in
a missing value. A key that the scene does not declare is ignored.
Showing a track
In the browser, load the track and the scene module, make a scene on a canvas, and call scene.draw() once per animation frame.
import {
asset, backdrop, createScene, field, loadTrack, road, scenery, sky, text,
} from "@i7r.tech/moveby";
import type { SceneDynamic } from "./track"; // track.d.ts, written by moveby build
const bytes = async (url: string) => new Uint8Array(await (await fetch(url)).arrayBuffer());
const track = await loadTrack(await bytes("/track.bin"), await bytes("/scene.wasm"), {
assets: [asset("tree", { cell: 0, width: 6, height: 9 })],
});
const scene = createScene<SceneDynamic>(document.querySelector("canvas")!, track, [
backdrop({ ground: [0.6, 0.584, 0.165], sky: [0.53, 0.81, 0.98] }),
road(),
sky(),
field({ atlas: "/fields.png" }),
scenery({ atlas: "/sprites.png" }),
text({ atlas: "/glyphs.png" }),
]);
await scene.ready; // the atlases are URLs: they arrive after createScene returns
function frame() {
scene.draw({ position: 25 }); // 25 m along the road: past 20, so the sign shows
requestAnimationFrame(frame);
}
requestAnimationFrame(frame);The atlases are URLs, so they arrive after createScene returns. await scene.ready waits for them. Without it, the first scene.draw() can throw FIELD_IMAGE_MISSING before the field atlas is there. asset("tree", …) gives the
look that the scene calls moveby.asset.tree: a cell of the
sprite atlas and a size in metres. One look serves any number of objects. A
name that the scene binds and the page does not give stops the load.
loadTrack takes the track's bytes and the scene module's bytes.
It reads the scene's objects from the module, then builds the engine from
the track and those objects. A using input that is not an asset
goes in a using option beside assets. createScene sets up the GPU programs for each layer in the list.
Each frame states every input again, so none can go stale. moveby reads your record, passes the numbers to the scene module in its own order, and keeps no reference to the record. The record is the one object a frame makes.
Choosing layers
A layer is one thing moveby draws. Each layer has two halves: a Rust half inside the WebAssembly module that computes its vertices, and a JavaScript half that owns its shaders. When you list a layer, you turn on both halves.
Every layer is optional, the road too. Under the layers sits the core. The core is not a layer, and it is always there. It holds the route and the camera.
| Layer | Draws | Comes from | Needs |
|---|---|---|---|
backdrop() | the ground colour, and the sky colour above the horizon | its options: ground, sky | — |
road() | the road surface, its lanes and dividers | road file: route, lanes | — |
field() | the ground beside the road | road file: route, fields | a field atlas |
sky() | the sky, and a cloud that shifts as the camera turns | the camera heading (drawn by a shader) | — |
scenery() | sprites beside the road | scene file | a sprite atlas |
text() | signs | scene file | a glyph atlas |
So a page that only needs its scenery to move can list backdrop() and scenery(). Its track holds a route and a tree, and nothing
for the other layers. Leave backdrop() out only when your
layers cover every pixel: a pixel that no layer draws is black, because the
browser clears the drawing buffer after each frame it shows. To keep the
last frame instead, make the WebGL context yourself, with preserveDrawingBuffer: true, before createScene.
import { asset, backdrop, createScene, loadTrack, scenery } from "@i7r.tech/moveby";
import type { SceneDynamic } from "./track"; // track.d.ts, written by moveby build
const bytes = async (url: string) => new Uint8Array(await (await fetch(url)).arrayBuffer());
const track = await loadTrack(await bytes("/track.bin"), await bytes("/scene.wasm"), {
assets: [asset("tree", { cell: 0, width: 2, height: 3 })],
});
// Two layers only: the ground colour, and the scenery on it. This track holds a route and one tree,
// so it needs no other layer.
const scene = createScene<SceneDynamic>(document.querySelector("canvas")!, track, [
backdrop({ ground: [0.6, 0.584, 0.165], sky: [0.53, 0.81, 0.98] }),
scenery({ atlas: "/sprites.png" }),
]);
await scene.ready; // the sprite sheet is a URL: it arrives after createScene returns
scene.draw({ position: 5 });The order of your list does not matter. moveby always draws in the same order: ground, road, field, sky, scenery, text. It skips each layer you leave out. The blended layers are correct only in that order, so the library keeps it for you.
If a track contains a section for a layer that you did not list, createScene throws an error that names the layer. It never drops the section in silence.
Either add the layer, or build the track without that content.
Driving the ride
moveby does not read the scroll wheel, the keyboard, or touch. Your app owns
its state. Turn your input into the record, and pass the record to scene.draw(). The camera stands position metres along the route. For example,
to let the page scroll drive the ride from start to end:
import { createScene, loadTrack, road } from "@i7r.tech/moveby";
import type { SceneDynamic } from "./track"; // track.d.ts, written by moveby build
const bytes = async (url: string) => new Uint8Array(await (await fetch(url)).arrayBuffer());
const track = await loadTrack(await bytes("/track.bin"), await bytes("/scene.wasm"));
const scene = createScene<SceneDynamic>(document.querySelector("canvas")!, track, [road()]);
const trackLength = track.engine.track_length(); // metres along the route
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);
}
requestAnimationFrame(frame);This sample draws the smallest road from the start of this page, so its only
layer is road(). A dynamic input can hold any number your page
knows: the seconds since a click, a slider value, or a hover. Add it to dynamic { … } in the scene. After the next build,
TypeScript marks each draw() call that must now pass it.
Handling errors
Every error has a stable code for your program and a message for a person.
import { loadTrack, MovebyError } from "@i7r.tech/moveby";
const bytes = async (url: string) => new Uint8Array(await (await fetch(url)).arrayBuffer());
const trackBytes = await bytes("/track.bin");
const sceneBytes = await bytes("/scene.wasm");
try {
// A track file cut short: a file can come from anywhere, so loadTrack checks it again.
await loadTrack(trackBytes.slice(0, 20), sceneBytes);
} catch (error) {
if (error instanceof MovebyError) {
console.log(error.code); // a stable code, for your program
console.log(error.message); // a readable text, for a person
}
}The build reports these errors first. loadTrack checks the bytes
again, because a track file can come from anywhere.
scene.wasm gets a weaker check. The browser checks that it is
valid WebAssembly, and moveby checks its table. The language's own checks
on the source do not run again. Load only a scene.wasm that moveby build made.
Building again each time you save
Add --watch to build again after each burst of saves:
$ npx moveby build scene.moveby road.json atlas.json --watchThe command watches the files the last build read. Each save writes the three outputs again, and your page's own development server reloads them.