how to make sprite stacking pixel art in javascript.
How sprite stacking works in plain JavaScript, from a moon landing you can drive: voxels, live light, depth, shadows, with prompts to remix and publish yours.
Sprite stacking draws a 3D object as a pile of flat pixel-art slices: each slice is one horizontal layer of the object, turned to the camera's angle and drawn one pixel higher than the slice below it. Because every slice is redrawn at any angle, the object turns smoothly while every pixel stays a crisp pixel. In the browser it fits on one 2D canvas in plain JavaScript, with no engine and no WebGL. This article shows how a working sprite-stacking renderer is built, and how to remix it, change its models, light and ground, and publish your own.
@rehan_shei used sprite stacking to turn the Apollo moon landing into pixel art you can turn and drive: a gold lander of 13,453 voxels in 44 slices, a flag, a saluting astronaut and a rover that leaves tire tracks in the dust. It's Moon stack on Patch, and his post about it had 18,190 impressions and 441 bookmarks on 2026-11-20.
The steps follow Moon stack's source with its real numbers: modeling in voxels from code, baking normals and shadowed corners, the palette, drawing the slices, sorting them with a depth buffer, lighting them live, casting shadows, outlines, the ground and the animation. The tutorials we read (loktar00's sprite-stacking repo, Emergent Mind's demo, Pixor's guide) cover drawing and ordering the slices. Moon stack also lights every voxel each frame, so the sun stays fixed in the world while a model turns. Its licence keeps it on Patch, so the route here is to remix it there and have your coding agent change it with the prompts below; if you build somewhere else, the steps describe the method, but you write your own renderer.
What you need
A Patch account, Patch's CLI (pch) and Patch Desktop, open and signed in, which pch publish uses to test a release on your PC before it goes live.
Node.js, for Moon stack's build script, and Python with numpy, scipy and pillow, for its model kit. Re-baking every model and the ground with that kit took 3.2 seconds in our test, in a cloud sandbox; your machine will differ.
A coding agent. Moon stack's credits say "Made for Patch by Rehan, with Claude." They don't name a Claude version.
A browser. The renderer is plain JavaScript modules on one 2D canvas, "no dependencies" in the README's words.
Moon stack is under the Patch licence: anyone may play it, read it and remix it on Patch with credit, and nobody may take it or a remix off Patch without the maker's permission. So the way to build on it is a remix on Patch, which is what the prompts below do.
1. Remix Moon stack and run it
Make your own copy of Moon stack on Patch, get it on your machine, and see it run before you change anything.
pch remix makes a project of yours from Moon stack's repo and records Moon stack as its parent, and pch clone brings it down with sign-in and the big files set up. pch check serves the game the way Patch does, opens it in a headless browser and reports what it saw, with screenshots.
prompt for your agent
Remix Moon stack with pch remix rehan/moon-stack, then pch clone my remix and work in that folder. Read README.md, src/stack.js and src/demo.js and tell me in a few lines how the renderer draws a frame. Run pch check and show me the screenshots of the landing site.
If pch (Patch's CLI) isn't installed, install it with curl -fsSL https://patch.gg/install.sh | sh (on Windows, in PowerShell: irm https://patch.gg/install.ps1 | iex), then run pch login and pch guide.
2. Model the object in voxels, from code
Build each object as a 3D grid of voxels, where each cell holds a material or nothing. Each horizontal layer of the grid is then one slice: a flat top-down pixel-art image.
You can paint slices by hand in a sprite stacking editor such as SpriteStack.io. Moon stack builds its models from shapes in Python instead, so a model can be changed by editing a number. Its kit has seven shapes (box, vertical cylinder, cone, octagon, capsule, ellipsoid and dish), and each model is written in design units of about 0.2 m, then scaled by a voxels-per-unit factor.
The lander is the biggest: a gold octagonal descent stage with a cone engine bell, four legs, a ladder, a gray ascent cabin with triangle windows, dishes and thruster quads, drawn on a 58 x 58 x 37 design grid at 1.2 voxels per unit. That comes out at 70 x 70 x 44 voxels.
Model
Grid (voxels)
Filled voxels
Lander
70 x 70 x 44
13,453
Rover with driver (6 wheel frames)
37 x 26 x 21
2,138
Astronaut (standing, saluting, 8 walk frames)
15 x 15 x 20
410 to 418
Flag
17 x 7 x 21
239
Rocks (6 sizes)
8 x 8 x 6 to 17 x 17 x 11
13 to 344
That's 24 models in all (one lander, one flag, 10 astronauts, 6 rovers and 6 rocks), 31,303 voxels together. The rocks are random ellipsoids with noise; the rest are written shape by shape. The same approach in Blender is a Blender Python script 3D model.
Eight of the lander's 44 slices, each a flat top-down pixel-art image: footpads, legs and engine bell, the gold descent stage, the cabin, the antenna.
To add a model of your own, ask your agent to build it in authoring/models.py from the kit's shapes, for example a small lunar seismometer on three legs with a gold foil body, about 20 x 20 x 16 voxels. Have it add the model to export.py and to the Scene picker, re-run the export and show you the new model turning with pch check.
3. Bake normals and shadowed corners into each voxel
Give every voxel a normal (the direction its surface faces) and an ambient occlusion value (how hidden it is), computed once when you export.
Pixel art usually has its light painted in. To light a model live as it turns (step 7), each voxel has to know which way it faces. Moon stack's finish step blurs the occupancy grid with a Gaussian of sigma 1.0 and takes the gradient: the normal points from full toward empty. For ambient occlusion it blurs the grid again (sigma 1.6), looks 1.6 voxels out along each voxel's normal and darkens the voxel by one shade if that spot is more than 30% full, two shades if more than 50%, so voxels in corners and crevices come out darker.
Two touches make the materials read at this size. The gold and amber foil get their normals jittered with random noise (0.28 for gold, 0.25 for amber), so the foil crinkles in the light. And painted surfaces get "big flat quilted panels instead of per-voxel speckle", in the code's words: each 4 x 4 x 3 block of voxels gets one shade nudge of -1, 0 or +1 by default.
4. Pick a palette of shade ramps
Give each material a short ramp of colors from dark to light, and store each pixel as a material and a level on its ramp, not as a color.
This is what keeps the result looking like pixel art. Lighting, shadows and outlines all work by moving a pixel up or down its ramp, so every pixel on screen is always one of the palette's colors. Moon stack has 21 materials with ramps of 1 to 10 levels: regolith has 10, gold has 6 and black ink has 1. At the end of each frame, one lookup table turns each (material, level) pair into its color.
The whole palette: 21 materials, 1 to 10 shades each. Every pixel the renderer draws is one of these. White swatches sit on the white ground.
The export packs every voxel, with its material, shade tweak, glow flag and normal, into one 375,888-byte binary file for all 24 models.
To add a material, ask your agent for a new ramp in authoring/palette.py, such as a 6-color copper from dark brown to pale orange, and to re-run export.py so src/palette.js is regenerated.
5. Draw each slice turned and lifted
Draw slice k turned to the camera's angle, with its ground plane squashed by the camera's pitch, and lifted k pixels up the screen.
A world point lands on screen in Moon stack by turning it around the camera, multiplying its y by a pitch factor f, and moving it up by its height: one pixel per slice at every pitch. At f = 1 the ground and the height have the same scale, which the README calls "classic top-down stacking". Lower f squashes the ground, so the model looks as if seen from lower down. Moon stack defaults to 0.7, and its Pitch control runs from 0.45 to 1. The Slice gap control adds 0 to 3 extra pixels between slices, which pulls the stack apart into its flat layers.
Slice gap 0 is the solid model. At 3 px the same 44 slices fan out into flat layers.
Pitch 0.45, 0.7 (the default) and 1. At 1 the ground plane is unsquashed; at 0.45 it's flattened and the model looks seen from lower.
The common way to draw a slice is as an image, with the canvas's rotate and drawImage. Moon stack instead works out the box each turned slice covers on screen, walks the pixels in that box and maps each one back into the slice to see which voxel it hits. That's the same rotation in reverse, and it gives the renderer each pixel's material and level, which it needs for everything after this.
Everything is drawn at art resolution: the landing site is about 240 x 135 pixels, scaled up by a whole number with hard edges. Turning a slice by a fraction of a degree moves whole pixels, never blurred ones.
6. Sort with a depth buffer on height
Keep a depth value for each screen pixel, set it to the slice's height when you draw it, and only draw over a pixel when the new slice is higher.
When several models stand near each other, painting order matters: whichever is drawn last covers the other. Emergent Mind's demo sorts sprites by their screen y. Moon stack's comment explains why it doesn't need to: "Along any screen pixel's ray the nearest surface is the one with the highest z, so a depth buffer on z is exact." Each pixel keeps the height of whatever was drawn there, and a new pixel only lands if its slice is higher. Models can then be drawn in any order and still overlap correctly.
Each frame runs in one order: clear, draw the ground, draw every model's shadow, darken the shadowed ground, draw every model, draw the dust, add outlines, then turn the levels into colors on the canvas.
The same buffer holds, for each pixel, its material, level and which object it belongs to. The shadows, outlines and dust in the steps below all read from it.
7. Light every voxel each frame, with the sun fixed
Before drawing a model, light each of its voxels from its normal, with the sun turned into the model's own frame, so it stays fixed in the world.
If the light is painted into the slices, it turns with the model. Moon stack's sun sits at an azimuth of 225 degrees and 48 degrees up. For each model each frame, it turns that sun direction back by the model's own rotation, takes each voxel's normal against it, and picks a level: ambient 0.3 plus 0.75 times the light, scaled to the length of the material's ramp, plus the voxel's baked tweak. Voxels marked as glowing take the top of their ramp. Since the lit level is a step on the palette ramps of step 4, the result stays pixel art.
As the lander turns, its lit side stays toward the sun, because the light is worked out per voxel every frame.
To change the light, ask your agent to move the sun in authoring/site.json and wherever src/demo.js and src/stack.js use it, for example to a low evening sun at azimuth 135 degrees and 25 degrees up, and to re-bake the ground with export.py so its crater shadows match.
8. Cast shadows from the slices
Draw each slice a second time onto the ground, shifted away from the sun by its height, and darken the ground wherever a slice lands.
A slice at height h casts its shadow h times the sun's slope away from its foot; with the sun 48 degrees up, that's about 0.9 pixels of shift for each pixel of height. Moon stack draws every slice of every model at that offset, marks the ground pixels it covers, then moves the regolith there four levels down its ramp. Shadows are applied before the models are drawn, so they only ever darken the ground. Because the shadow is computed from the slices, it follows the model's shape as it turns.
Cast shadow off and on: every slice drawn again on the ground, shifted away from the sun, four shades darker.
9. Outline the silhouettes from depth
Darken an object's pixels where it stands in front of something much farther away.
Small pixel art needs edges to separate an object from the ground. Moon stack compares each object pixel with its four neighbors, using its screen row plus its depth as a key. If the key jumps by more than 3 to a neighbor that is ground, the pixel is on an outer edge and goes two levels darker. Edges between two objects aren't darkened, and rocks (object ids 50 to 98) "keep their own shading", as the code puts it. The Outlines control turns it off, so you can compare.
Outlines off and on. On, the outer edges of the legs and the descent stage go two shades darker.
10. Bake a ground with craters and shadows
Bake the ground as one image of shade levels and draw it under everything, turned with the camera.
The ground isn't sprite-stacked: it's a 760 x 600 heightfield baked in Python. The kit adds noise and 87 craters (each a bowl, a rim and a spray of ejecta), and flattens a pad for the landing site. Then it lights the heightfield with ambient 0.22 and diffuse 0.78 from the same sun, marches toward the sun from every point to cast the craters' shadows, and adds grain and 2,600 pebbles. The result is saved as a gray PNG where each shade is a regolith level times 25 (174,628 bytes).
The baked ground, 760 x 600, shown through the regolith ramp. Each pixel is one of 9 shade levels of the 10-level ramp.
In the browser, each screen pixel is turned back into ground coordinates and reads its level from that image. Past the edge of the baked ground, a hashed noise keeps the plain going.
11. Animate the stack
Animate with baked frames for parts that move inside a model, and with per-slice offsets for effects.
The rover's wheels and the astronaut's legs change shape, so they're baked as separate models. The six rover frames together turn the tires by one tread chevron, and the renderer picks one from how far the rover has driven. The rover drives a 100-point lap around the site on its own; press WASD or the arrows and you drive it for 5 seconds before it takes over again, with the camera following. Its rear wheels stamp the ground two levels darker, and it kicks up dust.
The rover driven around the lander with the keys:, six baked wheel frames, tracks stamped into the ground.
The astronaut's eight walk frames play at 8 per second in the Astronaut scene; on the landing site the astronaut stands still in a salute.
The Astronaut scene: eight baked walk frames of 410 to 414 voxels, turning with the camera, shown at 0.75x speed.
Dust is single pixels drawn through the same depth buffer. The Stack button replays the intro: each slice falls from 70 pixels up over 0.22 seconds, bottom first, the lander's 44 slices over 1.25 seconds, and when the last one lands it throws 260 dust particles.
The Stack intro: 44 slices fall into place, bottom first.
To animate more of the site, ask your agent for a second astronaut who walks a slow loop around the flag on the eight walk frames at 8 per second, kicks up dust with each step and stamps boot prints one level darker into the ground, through the same depth buffer, shadows and outlines.
12. Publish it as a playable page
Put the controls in a recipe, build the release folder, and publish with pch.
Moon stack's controls (Scene, Slice gap, Turn, Pitch, Zoom, Speed, Outlines and the Stack button) are declared in its recipe.json as a scene picker, number fields, toggles and a button, and Patch shows them next to the game. node tools/build.mjs builds the dist folder that Patch publishes, with Patch's block script (fetched from patch.gg, so the build needs a network connection), and pch publish uploads it, tests it on your PC with Patch Desktop and follows Patch's review until it's live.
To ship your remix, ask your agent to run pch check, build dist with node tools/build.mjs and publish it with pch publish while Patch Desktop is open and signed in, and to tell you the address when it's live. The prompt under Try it below ends the same way.
The result
Moon stack is a renderer of 273 lines and a demo of 291 lines of plain JavaScript, plus 648 lines of Python that build 24 models of 31,303 voxels and the ground. You can turn the lander to any angle, pull its slices apart, and drive the rover around the site. @rehan_shei's post had 652 likes on 2026-11-20, and a later post of his shows a binary solar system made "with this 3D pixel art skill".
@rehan_shei's post on X. In the replies he suggests pointing your agent at the Patch page to drive the rover and make other 3D pixel objects.
More from @rehan_shei for browser games: AI game VFX in three.js, spells made from AI video.
prompt for your agent
In my Moon stack remix (if I don't have one yet, pch remix rehan/moon-stack and pch clone the remix), keep its renderer (src/stack.js) and authoring kit, and make a new sprite-stacked scene of your own: replace the moon landing with a small harbor, with a fishing boat, a lighthouse with a glowing lamp and a pier, each built from the kit's shapes and baked with export.py. Keep the per-voxel lighting with a fixed sun, the cast shadows, the depth buffer and the outlines, and let me turn the camera by dragging.
Run pch check and show me the screenshots, then run node tools/build.mjs and pch publish. Patch Desktop is open and signed in on this PC. Tell me the address when it's live.
Common questions
What is sprite stacking?
Sprite stacking is a pixel-art technique that draws a 3D object as a stack of flat 2D slices, each one layer of the object, drawn on top of the last and one pixel higher. Turning every slice by the same angle turns the whole object, so pixel art can be shown from any angle without drawing a sprite for each one.
Is sprite stacking the same as voxels?
Close. A sprite stack is a voxel model drawn one layer at a time, and Moon stack stores its models as voxels and draws them as slices. As @rehan_shei answered someone who asked: "its slices of images stacked up together". There's no 3D mesh and no WebGL, only 2D slices on a canvas.
Can you do sprite stacking in plain JavaScript without a game engine?
Yes. Moon stack is "one canvas, plain JavaScript, no dependencies": it computes every pixel itself into one image each frame and puts it on a 2D canvas. For a few simple sprites, drawing each slice with the canvas's rotate and drawImage is enough.
How do you light a sprite stack?
Store a normal per voxel and light it every frame against a sun that stays fixed in the world while the model turns. Moon stack uses ambient 0.3 plus 0.75 times the light, mapped onto each material's shade ramp, so the light stays in the palette.
What are the limits of sprite stacking?
The slices are flat and horizontal, so the camera can turn all the way around and tilt, but it can't roll (Pixor's guide makes the same point). Moon stack keeps its pitch between 0.45 and 1, so you never see a model from the side. Parts that change shape, like wheels and legs, need a baked model per frame.
Credits
Moon stack is by @rehan_shei, made for Patch with Claude. The idea came from @asynkimo's post "You can turn pixel art to any angle with sprite stacking"; nothing of theirs is copied. Sprite stacking itself is an old pixel-art trick. Built with JavaScript on a 2D canvas and Python with numpy, scipy and pillow.