moon stack
Sprite stacking for the browser: pixel-art voxel models drawn as flat slices stacked one pixel apart, so they turn to any angle. One canvas, plain JavaScript, no dependencies. It comes with an Apollo landing kit: the lunar module, the rover with its driver, an astronaut, the flag, boulders and a cratered moon to drive on.
open the demo · drag to turn · on the landing site, WASD or the arrows drive the rover
how it works.
| 1 · every layer is a flat drawing.Each model is a small voxel grid (the lander is 70 x 70 x 44). Each z layer of the grid is one slice: a top-down pixel-art picture.the Slice gap control pulls them apart | |
| 2 · stack them 1 px apart and turn them.Each slice is drawn turned to the model's angle, one pixel above the one under it. Along any screen pixel, the highest slice is the nearest, so a depth buffer on height sorts every model exactly.Pitch 1: classic top-down stacking | |
| 3 · light it per voxel.Every voxel keeps a normal, so the sun stays put while a model turns: lit sides, shaded sides, a shadow cast on the ground, and dark outlines where an edge stands in front of something farther away.all at art resolution, about 240 x 135, scaled up with hard edges |
what this owns.
src/stack.js draws into a Frame, a buffer at art resolution (material, shade, depth per pixel), and hands back an
ImageData to put on your own canvas. It owns nothing else: no canvas, no frame loop, no input. The demo
(index.html, src/demo.js) is the stage around it: the landing site, the rover's driving, the controls Patch's page
draws (patch-block.classic.js).
use it in a game.
Copy src/stack.js, src/palette.js and models/ into your game, then (this is all of example.html):
import { Cam, Frame, Inst, loadModels, sunDir } from "./src/stack.js";
const { models } = await loadModels("models/");
const frame = new Frame(240, 135), cam = new Cam(240, 135);
cam.f = 0.7; // pitch: 1 is classic top-down stacking
cam.lift = 30; // move the scene down the screen
const sun = sunDir(225, 48);
const lander = new Inst(models.lander, 0, 0, 0, 0);
const ctx = canvas.getContext("2d"); // a 240 x 135 canvas, scaled up in CSS with image-rendering: pixelated
function tick(t) {
lander.yaw = t / 1500;
frame.clear("paper");
frame.shadow(cam, lander, sun);
frame.applyShadow();
frame.draw(cam, lander, sun);
frame.outline();
ctx.putImageData(frame.paint(), 0, 0);
requestAnimationFrame(tick);
}
requestAnimationFrame(tick);
new Inst(model, x, y, z, yaw)places a model. Give itoff(four numbers a slice: dx, dy, dz, turn) andvis(one 0 or 1 a slice) to animate the slices themselves, as the demo's slices rain down.frame.ground(cam, ground)draws the moon fromloadGround("models/ground.png", site.ground).frame.draw(cam, inst, sun, gap):gappulls the slices apart.frame.point(cam, x, y, z, material, level)draws a particle (the demo's dust).- Models:
lander,rover-0torover-5(the wheels turning),astronaut,astronaut-salute,astronaut-walk-0to-7,flag,rock-0torock-5.
make your own models.
The models are made in Python, in authoring/models.py, from a few shapes on a voxel grid: boxes, cylinders, cones,
segments, ellipsoids and dishes, each with a material from authoring/palette.py. Write a function there, add it to
authoring/export.py, and run it to bake models/ again:
cd authoring && python3 export.py
It needs numpy, scipy and pillow. Normals, ambient occlusion and the foil's crinkle are baked per voxel; the browser lights them every frame.
credits.
Sprite stacking is an old pixel-art trick. Kimo's post (@asynkimo) showing a car turned to any angle with it is what started this one. The lander, rover, astronaut, flag, moon and renderer here were made from scratch for this block.