Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function drawBackground(mazePaths) {
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, CANVAS_HEIGHT, CANVAS_WIDTH);
ctx.fillStyle = COLORS[BLACK];
ctx.fill(mazePaths.roadsPath);
ctx.fillStyle = COLORS[WHITE];
ctx.fill(mazePaths.wallsPath);
return mazePaths;
}
function generateBackground() {
const matrices = {
last: null,
current: null,
};
matrices.current = new Array(MATRIX_DIMENSIONS.height)
.fill(0)
.map(() => {
return generateWhiteNoise(MATRIX_DIMENSIONS.width, WHITE_LEVEL);
});
let count = 0;
const ITERATIONS_LIMIT = 100;
while (
areMatricesDifferent(matrices.current, matrices.last) ||
count > ITERATIONS_LIMIT
) {
matrices.last = matrices.current;
matrices.current = cellularAutomaton(matrices.last);
}
const backgroundPaths = generateBackgroundPaths(matrices.current);
return drawBackground(backgroundPaths);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment