-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsketch-008.js
74 lines (65 loc) · 1.96 KB
/
sketch-008.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
const canvasSketch = require("canvas-sketch");
const random = require("canvas-sketch-util/random");
const settings = {
dimensions: [500, 500]
};
const sketch = () => {
let offset = -25;
const createLines = () => {
const lines_array = [];
const count = 10;
const gap = 500 / count;
for (let x = 1; x <= count; x++) {
const line = x * gap;
lines_array.push(line + offset);
}
return lines_array;
};
const lines = createLines();
return ({ context, width, height }) => {
// draw canvas
context.fillStyle = "#000";
context.fillRect(0, 0, width, height);
const grd = context.createLinearGradient(0, 0, 0, 170);
grd.addColorStop(0, "black");
grd.addColorStop(0.5, "#fff1cf");
grd.addColorStop(1, "white");
context.strokeStyle = grd;
context.lineWidth = 2;
// draw straight lines
lines.forEach(line => {
context.beginPath();
context.moveTo(line, 0);
context.lineTo(line, 500);
context.stroke();
});
// draw cross
lines.forEach(line => {
let offset = -50;
const position = random.range(50, 450);
const path_visible = new Path2D(
`M ${line + offset},${position} L ${line + 50 + offset},${position +
25} M ${line + 50 + offset},${position} L ${line +
offset},${position + 25}`
);
const path_invisible_l = new Path2D(
`M ${line + offset}, ${position} L ${line + offset}, ${position + 25}`
);
const path_invisible_r = new Path2D(
`M ${line + 50 + offset}, ${position} L ${line +
50 +
offset}, ${position + 25}`
);
// draw left line
context.lineCap = "round";
context.strokeStyle = "#000";
context.stroke(path_invisible_l);
context.strokeStyle = "#000";
context.stroke(path_invisible_r);
// draw the cross
context.strokeStyle = "#fff";
context.stroke(path_visible);
});
};
};
canvasSketch(sketch, settings);