-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
53 lines (44 loc) · 1.38 KB
/
sketch.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
const canvasSketch = require("canvas-sketch");
const size = 620;
const settings = {
dimensions: [size, size]
};
const numRows = 10;
const numCols = numRows;
const margin = 20;
const sizeInner = size - 2 * margin;
const widthCell = sizeInner / numCols;
const heightCell = sizeInner / numRows;
const sketch = ({ canvas, context, width, height }) => {
canvas.addEventListener("click", render);
function render() {
context.fillStyle = "white";
context.fillRect(0, 0, width, height);
context.save();
context.translate(margin, margin);
context.strokeStyle = "black";
context.beginPath();
for (let i = 0; i < numRows; i += 1) {
for (let j = 0; j < numCols; j += 1) {
context.save();
context.translate(j * widthCell, i * heightCell);
const off = (i + 1) / numRows;
context.translate(
widthCell / 2 + randomSign(Math.random() * (widthCell / 8) * off),
heightCell / 2 + randomSign(Math.random() * (widthCell / 8) * off)
);
context.rotate(randomSign(Math.random() * (Math.PI / 10)) * off);
context.translate(-widthCell / 2, -heightCell / 2);
context.rect(0, 0, widthCell, heightCell);
context.restore();
}
}
context.stroke();
context.restore();
}
return render;
};
canvasSketch(sketch, settings);
function randomSign(x) {
return Math.random() < 0.5 ? x : -x;
}