Skip to content

Instantly share code, notes, and snippets.

@YonatanKra
Created September 28, 2021 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YonatanKra/3a3b6c2862d3302cd4c626ccbe3c3feb to your computer and use it in GitHub Desktop.
Save YonatanKra/3a3b6c2862d3302cd4c626ccbe3c3feb to your computer and use it in GitHub Desktop.
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