-
Notifications
You must be signed in to change notification settings - Fork 0
/
particle.js
34 lines (32 loc) · 837 Bytes
/
particle.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
export class Particle {
constructor(x, y, context) {
this.x = x;
this.y = y;
this.context = context;
this.radius = Math.random() + 2;
this.color = "#920101";
this.velocity = {
x: (Math.random() - 0.5) * Math.random() * 5,
y: (Math.random() - 0.5) * Math.random() * 5,
};
this.alpha = 1;
this.friction = 0.99;
}
draw() {
this.context.save();
this.context.globalAlpha = this.alpha;
this.context.beginPath();
this.context.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
this.context.fillStyle = this.color;
this.context.fill();
this.context.restore();
}
update() {
this.draw();
this.velocity.x *= this.friction;
this.velocity.y *= this.friction;
this.x += this.velocity.x;
this.y += this.velocity.y;
this.alpha -= 0.04;
}
}