Visualizations
152 / 500

152 · Simulation

Falling Sand

Sand, water, fire, lava and plants that all react with each other.

Every pixel is a cell holding a material, and each frame every cell follows a few local rules: sand falls and piles, water spreads sideways, fire rises and burns wood and plants, lava melts sand into glass and boils water into steam, and plants grow into water. The grid lives in a typed array, gets updated bottom-up in a randomized order so nothing drifts to one side, and is written straight into ImageData.

Try it. Pick a material from the palette (or press 1 to 0) and drag to pour it. Right-click or Shift-drag to erase. Press R for a new scene.

  • Cellular automata
  • Typed arrays
  • Per-pixel ImageData rendering

View the source · one module, plus a small shared runtime for sizing, the animation loop and input

Build your own

Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.

Build a falling sand game with JavaScript and the HTML canvas element. Put everything in a single index.html file with no libraries or build step, so I can open it directly in a browser.

Start simple:
- Make a canvas that fills the window and stays sharp on high-DPI screens. Simulate on a small grid (around 300 by 200 cells) and scale it up with image smoothing turned off, so each cell is a crisp square.
- Store the grid as a Uint8Array of material ids: empty, sand, water and stone. Draw it each frame by writing colors into an ImageData buffer with a Uint32Array view and calling putImageData once.
- Update the grid bottom-up. Sand falls into the cell below if it is empty, otherwise slides diagonally. Water does the same, then also flows a few cells sideways. Sand sinks through water by swapping places with it. Stone never moves.
- Alternate the direction you scan each row (left to right, then right to left) so nothing drifts to one side, and stamp moved cells so a grain cannot move twice in one frame.
- Let me pick a material with the number keys and drag the mouse to pour it.

Once that works, make it beautiful and alive:
- Give every grain a small random color variation when it is created, so piles look like real sand.
- Add fire (rises, flickers through a color ramp, dies into smoke), wood (catches fire), and lava (flows slowly, glows, turns water into steam and itself into stone).
- Add glow: count fire and lava cells into a coarse quarter-size light map and draw it scaled up on top with "lighter" blending.
- Draw a small material palette inside the canvas so it works without a keyboard.

Explain the key ideas in short code comments. When you're done, tell me how to open it and suggest three directions I could take it next, such as plants that grow into water, acid that eats through everything but glass, or an autopilot that pours and erupts on its own.
PreviousSnowflake CutterFold paper into a 12-layer wedge, snip it, and unfold a six-fold snowflake. NextIron FilingsMagnets on paper dusted with iron filings, drawn by line integral convolution.

Related visualizations

  • Turing PatternsEmergence Two chemicals react and diffuse into coral, spots and stripes.
  • Snow and SlushPhysics Snowballs, jelly, sand and water smash and tumble down a slope, simulated with MLS-MPM.
  • LeniaEmergence Smooth artificial life: glowing creatures that glide, spin and divide.
  • Lava IslandNature A volcanic island erupts at dusk, and every lava flow cools into new land.
  • Glowing SurfFluids Moonlit waves break on a beach and the plankton in them flash electric blue.
  • Julia SetFractals A fractal that reshapes itself as its complex constant wanders.
  • Growing Neural CellsMachine learning Cells running one tiny learned rule grow a gecko from a single seed and heal its wounds.
  • Ant FarmEmergence A colony digs its nest grain by grain behind glass, with no plan but local rules.
  • Chladni PlateSound Sand dances into Chladni figures on a brass plate whose modes come from the Ritz method.

Use ← and → to move between demos. While the canvas has focus, keys go to the demo instead.

← More from Emergent Mind Labs