This page is docs/web.md of the repository, as it is on main.
Pantin on the web
The web runtime plays a character in a browser, with no code on the server: the bundle a game ships
(pantin build), a module of WebAssembly built from the runtime of pantin, and a little JavaScript
that draws it with WebGL 2. It plays the animations and follows the graph of states as the runtimes of
Godot and Bevy do: they are the same Rust (crates/pantin-character), built for the browser.
Building it
rustup target add wasm32-unknown-unknown
cargo install wasm-bindgen-cli --version 0.2.129
tools/web_runtime.sh out robot lamp
tools/web_runtime.sh <out> [example…] writes into <out> the module and its JavaScript (pkg/),
pantin.js, a page index.html, and the bundle of each example named (the robot by default) in
characters/. Serve the folder with any server of static files (python3 -m http.server, in
<out>) and open index.html: it plays characters/robot.pantinpack, or the bundle that
?bundle= names, following its graph, with a button for each animation.
A page written by pantin
File › Export › Web page… (or pantin web <project> --out <folder>) writes a folder with one
index.html that holds everything: stage.js, the module in the form a page opened from the disk can
hold (pkg-inline/, which tools/web_runtime.sh builds beside pkg/), the bundle as base64, and the
animations and skins chosen. It plays offline, opened from the disk; the bundle is written beside it too.
The application finds the runtime in target/web (beside the program, or a folder above it) or where
PANTIN_WEB_RUNTIME says.
In a page of your own
<canvas id="stage" width="400" height="400"></canvas>
<script type="module">
import { load, Stage } from "./pantin.js";
const character = await load("characters/robot.pantinpack");
const stage = new Stage(document.getElementById("stage"), character);
character.advance(0);
stage.fit();
let last = performance.now();
requestAnimationFrame(function frame(now) {
character.advance((now - last) / 1000);
last = now;
stage.draw();
requestAnimationFrame(frame);
});
</script>
Character |
What it does |
|---|---|
play(track, name, looping, fade) |
Plays an animation on a track, fading from what it played; the character leaves its graph. |
fire(name), set_number(name, value), set_switch(name, on) |
What a game tells its graph of states. set_number sets the parameter of Deform of the same name too. |
state() |
The state of its graph it is in. |
set_parameter(name, value) |
A parameter that deforms meshes (the deform format). |
advance(seconds) |
Plays on; gives the names of the events told meanwhile. |
skins(), set_skin(name) |
Its skins; shows one, or none with an empty name. |
animations(), bounds() |
Its animations; what it covers now, [lowX, lowY, highX, highY], y up. |
Stage draws it: view([x, y], zoom) looks at a point of the world, so many pixels for a unit;
fit(margin) frames what it covers now; background is the colour behind it, [r, g, b, a] from 0 to
1; draw() draws it as it is now. The colours, the dark colours and the four blend modes are drawn as
pantin render draws them: a test compares a frame of the browser with the frame pantin render draws
of the same moment (runtimes/web/tests/browser.rs, with a Chromium named by PANTIN_CHROME).