npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@mobiuscode/wave-collapse

v1.0.2

Published

A simple wave collapse algorithm

Readme

Wave Collapse

A simple wave collapse algorithm implementation that is used to populate a grid of a defined size with tiles that have certain connection rules. This is useful for generating a random pattern that follows certain rules.

Wavefunction Collapse and how it works: https://robertheaton.com/2018/12/17/wavefunction-collapse-algorithm/

How-to use?

First you have to define the tiles that make up your grid. For example:

const myTiles = [
    new Tile(0, new Map())
];

The map here contains the "connectors" that the tile is compatible with. Normally you want to define an enum to specify what kind of connectors you want to have:

const ConnectorType = Object.freeze({
    SINGLE_LINE: 0,
    // ...
});

So, for example, if we have three tiles that look like this:

    ┌──────┐  ┌──────┐  ┌───┬──┐
    │      │  │      │  │   │  │
    │      │  ├──────┤  ├───┘  │
    │      │  │      │  │      │
    └──────┘  └──────┘  └──────┘
id   0         1         2

They would be defined like this:

const myTiles = [
    // no connections possible from this tile, so this tile can only
    // connect to some other tile that also has no connection pointing
    // to its neighbor
    new Tile(0, new Map()),
    
    // has one connection to the left, and one to the right, each
    // as the single line connector type
    new Tile(1, new Map(
        [Direction.LEFT, new Set([ConnectorType.SINGLE_LINE])],
        [Direction.RIGHT, new Set([ConnectorType.SINGLE_LINE])],
    )),
    
    // has one connection to the left, and one pointing upwards, each
    // as the single line connector type
    new Tile(1, new Map(
        [Direction.LEFT, new Set([ConnectorType.SINGLE_LINE])],
        [Direction.UP, new Set([ConnectorType.SINGLE_LINE])],
    )),    
];

Now you can use the wave collapse algorithm to get a solution like this:

// for a consistent result you can initialize the random number generator with
// a fixed seed. Comment this out in case you want a new solution everytime you
// run the script
random.use(12345);

// create all rotation variants of my tiles (note that this generates rotation
// variants even for when it is not really required, like with tile 0
const myTilesRotated = generateRotatedTiles(myTiles);

// create a 10x10 grid of our possible tiles (all variants are possible in all grid positions, a.k.a. superposition)
const grid = generateGrid(myTilesRotated, 10, 10);

// tries to find a solution and returns that solution if found (otherwise null is returned)
const solution = collapseWave(grid);

if (solution !== null) {
    console.log("solution found")
    console.log(gridToString(solution))
} else {
    console.log("no solution found :(")
}