---
title: "Wave Function Collapse (wfc) ⁞ Arthur Erlich"
canonical: "https://me.arthurerlich.de/project/article/wave-function-collapse/"
---

project·06.04.2026·Updated25.10.2026

# Wave Function Collapse (wfc)

An experiment implementing the Wave Function Collapse algorithm for procedural generation in the browser.

* Typescrypt

During my studies at HFU in Furtwangen, I started this experiment with the Wave Function Collapse algorithm. I finished the project in winter 2024 but didn’t have time to make it look neat. In 2025 I came back to it, reworked the generation completely and cleaned up the webpage. This is a fascinating way to solve a puzzle “Sudoku-like.” It would be nice to try this for level generation.

## The idea

The algorithm I followed is from [Robert Heaton](https://robertheaton.com/2018/12/17/wavefunction-collapse-algorithm/). The basic idea is simple: You have a set of tiles, and every tile knows which tiles fit on its four sides. The grid starts with every cell holding all possible tiles. Then the algorithm picks one cell, collapses it to a single random tile, and removes every option from the neighboring cells that no longer fits. Step by step, the whole grid fills in with a valid, non-contradicting result.

## How my version works

* One grid of 10 by 10 cells, driven by a small config file (grid size, canvas size, delay).
* Seven tiles in total: one blank tile, a straight line in two directions (the same image, rotated 90°), and a corner tile in all four rotations.
* Compatibility is hard-coded as a 3-digit code per side. `010` means the line continues through the middle of that side, `000` means nothing.
* No canvas or WebGL: every cell is a custom DOM element that gets a background image and a rotation, so the whole run is just the browser re-painting divs.
* The loop: seed one random cell, prune the options of every cell against its neighbors, then collapse the cell with the least options left. Repeat with a small delay in between, so you can watch the grid fill up.
* No backtracking: if a cell ever runs out of options, the run just stops. It is rare with this tileset, because the blank tile fits on every side.

## The result

Every run produces a different network of lines and corners on the blank background. Some look like circuit traces, some like a tiny city map. Because the collapse order is random, two runs are never the same.

## The rework

The first commit in the repository is from March 2023, right at the start of the course. After I finished it in winter 2024, it sat around for a while. In April 2025 I did a complete rework of the generation, and in July I made it presentable: moved the code into a clean folder structure, removed the dev dependencies and fixed the paths so it could be deployed as a proper page.

Have a look and play around: [wfc.arthurerlich.de](https://wfc.arthurerlich.de/) and the [source code on GitHub](https://github.com/ArthurErlich/WaveFunctionColapsWeb).
