-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparticle.js
More file actions
42 lines (36 loc) · 930 Bytes
/
particle.js
File metadata and controls
42 lines (36 loc) · 930 Bytes
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
const TOTAL = 12;
export class Particle {
constructor(pos, color, ctx) {
this.color = color;
const ranMax = 20;
this.points = [{
x: pos.x,
y: pos.y,
}];
for (let i = 1; i < TOTAL; i++) {
const prev = this.points[i - 1];
this.points.push(this.setRandom(prev, ranMax));
}
this.draw(ctx);
}
draw(ctx) {
ctx.beginPath();
ctx.lineWidth = 0.3;
ctx.strokeStyle = this.color;
ctx.moveTo(this.points[0].x, this.points[0].y);
for (let i = 1; i < this.points.length; i++) {
const prev = this.points[i - 1];
const cur = this.points[i];
const cx = (prev.x + cur.x) / 2;
const cy = (prev.y + cur.y) / 2;
ctx.quadraticCurveTo(prev.x, prev.y, cx, cy);
}
ctx.stroke();
}
setRandom(pos, gap) {
return {
x: pos.x + Math.random() * (gap + gap) - gap,
y: pos.y + Math.random() * (gap + gap) - gap,
}
}
}