Wave Function Collapse JavaScript: Procedural Tile Maps for Game Levels
wavefunctioncollapse is an npm package that produces unique tilemaps and textures based on the Wave Function Collapse algorithm. It’s easy to install into your JS projects and let your agent generate endless dungeon floorplans, city layouts, or tiling textures from any image sample.
You’ve probably thought about giving your agent the ability to create a tile-based world. Great, he made you a dungeon you could play in. Awesome. Wait…isn’t that dungeon the same one from last time? Exactly the same rooms in the exact same order? You can’t unsee it now.
WFC (Wave Function Collapse) is a scientific algorithm developed by Maxim Gumin in C# originally. Since then, it has been ported to virtually every language. There’s a longstanding JS port by Kevin Chapelier available on npm for years, and it’s been quietly working away under the radar, licensed under the MIT license. I had need of random maps for a browser game and found it to be a perfect solution.
Source
kchapelier/wavefunctioncollapse
★ 524JavaScript
This package provides you with two models. OverlappingModel allows you to train on a sample image to learn local patterns that are then translated across the generated output, placing similarities at different points. SimpleTiledModel lets you set up tiles and define which edges can be connected to which other edges. The result is a map that has tiles fitting within the constraints you provide. SimpleTiledModel is good for dungeon floorplans and room-based maps.
How to add it to your project
It’s as simple as running a single command, creating an ImageData on a canvas, feeding that into the model and calling generate() to get the pixel data back out for drawing on a canvas.
npm install wavefunctioncollapse --production
How OverlappingModel works
Feed it some sample image data, like a portion of a tilemap or a hand drawn map. It traverses through the NxN block, learning what combinations can go together and fills an empty canvas by collapsing down the options on a cell by cell basis, much like sudoku. In the animated demo here, you can watch it build out the maps in real time. It’s actually hypnotic.
Using SimpleTiledModel for dungeon maps
For SimpleTiledModel, you provide a JSON object detailing the tiles you want to use and their edge restrictions. You tell the algorithm which sides can be connected to which other sides, and it lays out tiles so they adhere to the rules you defined. Nothing in the middle of the wall, no dead ends on the road, etc. Constraints are constraints.
What to tell your agent
If you want, you can simply tell your agent what package name to use and the kind of model you’re looking for. If your agent is familiar with WFC, it may be able to access wavefunctioncollapse right on npm and get started. Check out one of the 3 interactive demos on the project site to get a feel for what’s possible and communicate that to your agent.
"I want procedural dungeon levels in my JavaScript game. Use the wavefunctioncollapse npm package. Load my sample floor-plan as a canvas ImageData, run OverlappingModel with N=3, output size 48x48, periodicOutput false. On each new game generate a fresh map with Math.random and draw it to a canvas element. Show me the code." "Use wavefunctioncollapse SimpleTiledModel. My tiles: floor, wall, door. Floor connects to floor and door on all sides, wall to wall only, door to floor and wall. Generate a 20x15 tile map and return it as a 2D array of tile names."
An npm package that generates infinite unique tile maps — dungeons, city blocks, seamless textures — from a single sample input. MIT license, works in the browser, no build step beyond npm install. Give your agent the package name plus either a sample image or a set of tile rules, and describe the output you want. Check the three live demos on the project site first to see what WFC output actually looks like.
Source: github.com
Free course
Stop reading about AI — start building with it
The free Claude Code course: your first site, tool or game — no coding. No upsells, no cross-sells — nothing to buy here.
Start free →
Author
Evgenii Arsentev
PhD · Chief Executive Officer, digital health
Articles · Latest articles