-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
97 lines (85 loc) · 2.25 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
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
86
87
88
89
90
91
92
93
94
95
96
97
const canvasSketch = require("canvas-sketch");
const random = require("random");
const size = 620;
const settings = {
animate: true,
dimensions: [size, size],
loop: false,
totalFrames: 1000
};
const numPoints = 30;
const margin = 10;
const sketch = ({ context, width, height }) => {
context.fillStyle = "white";
context.fillRect(0, 0, width, height);
const cx = size / 2;
const cy = cx;
const r = size / 4;
const gap = (2 * Math.PI) / numPoints;
const off = random.normal(0, r / 6 / 3);
const points = [];
for (let i = 0; i < numPoints; i += 1) {
const angle = i * gap;
const x = r * Math.cos(angle) + cx;
const y = r * Math.sin(angle) + cy;
points.push({
_angle: angle,
x,
y
});
}
function angle(x, y) {
const a = Math.atan2(y - cy, x - cx);
return mod(a + 2 * Math.PI, 2 * Math.PI);
}
function render() {
context.beginPath();
context.strokeStyle = "white";
context.lineWidth = margin;
context.strokeRect(0, 0, size, size);
//context.save();
//context.translate(margin, margin);
context.beginPath();
context.moveTo(points[0].x, points[0].y);
for (let i = 1; i < numPoints - 1; i += 1) {
context.quadraticCurveTo(
points[i].x,
points[i].y,
(points[i].x + points[i + 1].x) / 2,
(points[i].y + points[i + 1].y) / 2
);
}
context.quadraticCurveTo(
points[numPoints - 1].x,
points[numPoints - 1].y,
points[0].x,
points[0].y
);
context.strokeStyle = "rgba(50, 50, 50, 0.04)";
context.lineWidth = 1;
context.stroke();
//context.restore();
for (let i = 0; i < numPoints; i += 1) {
const point = points[i];
if (Math.random() < 0.1) {
const xNew = point.x + off();
const yNew = point.y + off();
const a = angle(xNew, yNew);
let s = mod(point._angle - gap / 3, 2 * Math.PI);
let e = mod(point._angle + gap / 3, 2 * Math.PI);
if (i == 0 && (a > s || a < e)) {
point.x = xNew;
point.y = yNew;
} else if (a > s && a < e) {
point.x = xNew;
point.y = yNew;
}
}
}
}
return render;
};
canvasSketch(sketch, settings);
function mod(n, m) {
return ((n % m) + m) % m;
}