forked from mattdesl/canvas-sketch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas-10-print.js
61 lines (54 loc) · 1.38 KB
/
canvas-10-print.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
/**
* A Canvas2D example of "10 PRINT" algorithm.
* @author Guido Schmidt (@guidoschmidt)
*/
const createSketch = require('canvas-sketch');
const settings = {
dimensions: [2048, 2048]
};
const sketch = props => {
const tileCount = 30;
const tiles = Array(tileCount)
.fill(0)
.map((_, i) => {
return Array(tileCount)
.fill(0)
.map((_, j) => {
const [width, height] = [
props.width / tileCount,
props.height / tileCount
];
const randomNumber = Math.random();
if (randomNumber > 0.5) {
return {
x: i * width,
y: j * height,
toX: i * width + width,
toY: j * height + height
};
} else {
return {
x: i * width,
y: j * height + height,
toX: i * width + width,
toY: j * height
};
}
});
});
return ({ context, width, height }) => {
context.fillStyle = 'black';
context.fillRect(0, 0, width, height);
context.lineWidth = '4';
context.strokeStyle = 'white';
tiles.map(row => {
row.map(rect => {
context.beginPath();
context.moveTo(rect.x, rect.y);
context.lineTo(rect.toX, rect.toY);
context.stroke();
});
});
};
};
createSketch(sketch, settings);