Home

Awesome

wavefunctioncollapse

Javascript port of https://github.com/mxgmn/WaveFunctionCollapse

Installing

With npm do:

npm install wavefunctioncollapse --production

Public API

OverlappingModel Constructor

new OverlappingModel(data, dataWidth, dataHeight, N, width, height, periodicInput, periodicOutput, symmetry[, ground])

var wfc = require('wavefunctioncollapse');

var imgData = ... // let's pretend this is an ImageData retrieved from a canvas context in the browser

var model = new wfc.OverlappingModel(imgData.data, imgData.width, imgData.height, 3, 48, 48, true, true, 4);

OverlappingModel Methods

model.graphics([array])

Retrieve the RGBA data of the generation.

// create a blank ImageData
var imgData = canvasContext.createImageData(48, 48);

// write the RGBA data directly in the ImageData
model.graphics(imgData.data);

// print the ImageData in the canvas
canvasContext.putImageData(imgData, 0, 0);

SimpleTiledModel Constructor

new SimpleTiledModel(data, subsetName, width, height, periodicOutput)

var wfc = require('wavefunctioncollapse');

var data = ... // object with tiles, subsets and constraints definitions

var model = new wfc.SimpleTiledModel(data, null, 48, 48, false);

SimpleTiledModel Methods

model.graphics([array[, defaultColor]])

Retrieve the RGBA data of the generation.

// create a blank ImageData
var imgData = canvasContext.createImageData(48, 48);

// write the RGBA data directly in the ImageData, use an opaque blue as the default color
model.graphics(imgData.data, [0, 0, 255, 255]);

// print the ImageData in the canvas
canvasContext.putImageData(imgData, 0, 0);

Common Methods

model.generate([rng])

Execute a complete new generation. Returns whether the generation was successful.

model.generate(Math.random); // return true or false

model.iterate(iterations[, rng])

Execute a fixed number of iterations. Stop when the generation is successful or reaches a contradiction. Returns whether the iterations ran without reaching a contradiction.

model.iterate(30, Math.random); // return true or false

model.isGenerationComplete()

Returns whether the previous generation completed successfully.

model.clear()

Clear the internal state to start a new generation.

Changelog

2.1.0 (2021-03-27)

2.0.0 (2019-07-06)

1.0.0 (2016-10-14)

License

MIT