-
Notifications
You must be signed in to change notification settings - Fork 0
/
nestedSquares.js
85 lines (72 loc) · 2.17 KB
/
nestedSquares.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { createGrid } from "./grid";
import gaussianRandom from "./gaussianRandom";
const CANVAS_DIMENSION = 800;
const LINES_PER_DIMENSION = 5;
export default function nestedSquares() {
const canvas = document.querySelector("canvas");
canvas.width = CANVAS_DIMENSION;
canvas.height = CANVAS_DIMENSION;
const ctx = canvas.getContext("2d");
ctx.strokeStyle = "black";
ctx.strokeWidth = 0.5;
ctx.fillStyle = "rgba(0, 0, 0, 0.01)";
const grid = createGrid(
CANVAS_DIMENSION,
CANVAS_DIMENSION,
CANVAS_DIMENSION / LINES_PER_DIMENSION
);
ctx.clearRect(0, 0, CANVAS_DIMENSION, CANVAS_DIMENSION);
const interval = setInterval(() => {
ctx.clearRect(0, 0, CANVAS_DIMENSION, CANVAS_DIMENSION);
drawSquaresIntoGrid(grid, ctx);
}, 100);
return {
teardown() {
ctx.clearRect(0, 0, CANVAS_DIMENSION, CANVAS_DIMENSION);
clearInterval(interval);
}
};
}
function drawSquareWithinBounds(ctx, numSquares, x1, y1, x2, y2) {
const dx = x2 - x1;
const dy = y2 - y1;
// fill or stroke?
// ctx.strokeRect(x1, y1, dx, dy);
ctx.fillRect(x1, y1, dx, dy);
if (numSquares > 1) {
const center = [(x1 + x2) / 2, (y1 + y2) / 2];
const displacement = [
(0.5 - gaussianRandom()) * dx,
(0.5 - gaussianRandom()) * dy
];
const newCenter = center.map((coord, index) => coord + displacement[index]);
const shrinkage = gaussianRandom() * 2;
const newDx = dx * shrinkage;
const newDy = dy * shrinkage;
drawSquareWithinBounds(
ctx,
numSquares - 1,
newCenter[0] - newDx / 2,
newCenter[1] - newDy / 2,
newCenter[0] + newDx / 2,
newCenter[1] + newDy / 2
);
}
}
let margin = 10;
function drawSquaresIntoGrid(grid, ctx) {
for (let columnIndex = 0; columnIndex < grid.length - 1; columnIndex += 1) {
for (let rowIndex = 0; rowIndex < grid[0].length - 1; rowIndex += 1) {
const { x: startX, y: startY } = grid[rowIndex][columnIndex];
const { x: endX, y: endY } = grid[rowIndex + 1][columnIndex + 1];
drawSquareWithinBounds(
ctx,
1000,
startX + margin,
startY + margin,
endX - margin,
endY - margin
);
}
}
}