Visualizations
214 / 500

214 · Algorithms

Wave Function Collapse

A tile-based world that assembles itself one consistent cell at a time.

Every cell starts out able to be any tile. The algorithm repeatedly picks the cell with the fewest remaining options, collapses it to one tile at random, and propagates the consequences: neighbors lose every option whose edges would no longer match. The result is a coherent landscape of coasts, rivers, roads and forests that nobody designed, built one cell at a time, with the remaining uncertainty shown as a shimmer of possibilities.

Try it. Click to generate a new world. Hover over the map, or press and hold, to speed it up.

  • Constraint propagation
  • Minimum entropy heuristic
  • Procedural tilesets

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 an animated Wave Function Collapse map generator 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, stays sharp on high-DPI screens (scale by devicePixelRatio), and resizes with the window. Use a dark background.
- Define a tileset in code, no image files: each tile is the terrain at its four corners, using water, sand and grass, with corners at most one step apart. Generate every valid combination and give each a weight.
- Two tiles fit side by side when the two corners along their shared edge match.
- Make a grid of about 30 by 20 cells, each starting with every tile possible.
- Each step, find the uncollapsed cell with the lowest entropy (fewest weighted options, with a tiny random tie-breaker), pick one of its tiles at random by weight, then propagate: repeatedly remove tiles from neighbors that no longer have a compatible tile next to them.
- Run a few steps per frame. Draw collapsed cells from their corner terrains, and uncollapsed cells as the average color of their remaining options, dimmer when more remain.
- If a cell ever runs out of options, start over.

Once that works, make it beautiful:
- Prerender every tile once into a small offscreen canvas: blend the corner heights bilinearly, add noise that fades to zero at the tile edges so coastlines wiggle but still line up, and add foam along the shore.
- Add forests and mountains as more terrain levels, with little trees and peaks drawn on top.
- When the map is done, hold it for a few seconds, then generate a new one. Click to start over right away.

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 roads and rivers as edge sockets, backtracking instead of restarting, or biasing the weights with a noise map so the world forms continents.
PreviousFaraday WavesA shaken dish of liquid locks into stripes, squares, hexagons and a 12-fold quasicrystal. NextSLE CurvesRandom fractal curves grown tip first from Loewner's equation, kappa by kappa.

Related visualizations

  • Nonogram ForgeAlgorithms Paints a pixel picture, proves its clues have one answer, then solves it line by line.
  • Multiscale TruchetGenerative art Truchet tiles at five scales lock together seamlessly, flipping color at each level.
  • Self-Replicating LoopsEmergence Langton's 1984 loops copy their own genome and tile the plane with a glass colony.
  • Tessellation MorphMath Drag one edge of an Escher-style creature and every copy reshapes while still tiling.
  • Invented AtlasGenerative art A parchment map of a world that never was, with rivers, realms and its own language.
  • CooperationEmergence A few defectors in a sea of cooperators unfold into an endless symmetric kaleidoscope.
  • Basalt ColumnsNature Cooling lava cracks into columns that drift toward hexagons, on an isometric causeway.
  • Lights Out AlgebraMath The toggle-tile puzzle solved instantly at any size by linear algebra over GF(2).
  • Constraint SolverAlgorithms A sudoku solver reasons in pencil, guesses in ink, and erases when it is wrong.

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

← More from Emergent Mind Labs