-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsketch-004.js
64 lines (55 loc) · 1.67 KB
/
sketch-004.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
const canvasSketch = require("canvas-sketch");
const { lerp } = require("canvas-sketch-util/math");
const random = require("canvas-sketch-util/random");
const palettes = require("nice-color-palettes");
const settings = {
dimensions: [2048, 2048]
};
const count = 20;
const sketch = () => {
const createGrid = () => {
const points = [];
for (let x = 0; x < count; x++) {
for (let y = 0; y < count; y++) {
const u = count <= 1 ? 0.5 : x / (count - 1);
const v = count <= 1 ? 0.5 : y / (count - 1);
points.push({
radius: 5,
position: [u, v],
alpha: random.value(1)
});
}
}
return points;
};
const points = createGrid();
const margin = 300;
return ({ context, width, height }) => {
context.fillStyle = "black";
context.fillRect(0, 0, width, height);
points.forEach(point => {
const { radius, position, alpha } = point;
const [u, v] = position;
const x = lerp(margin, width - margin, u);
const y = lerp(margin, height - margin, v);
const dist = (width - 600) / (count - 1);
const siblingDots = [
{ x, y },
{ x: x + dist, y: y },
{ x: x + dist, y: y + dist },
{ x: x, y: y + dist }
];
const randomDot = random.pick(siblingDots);
context.lineCap = "round";
context.beginPath();
context.moveTo(x, y);
context.globalAlpha = alpha;
context.strokeStyle = random.pick(random.pick(palettes));
context.lineWidth = 10;
// draw line to random sibiling dots
context.lineTo(randomDot.x, randomDot.y);
context.stroke();
});
};
};
canvasSketch(sketch, settings);