-
Notifications
You must be signed in to change notification settings - Fork 0
/
Particle.js
48 lines (40 loc) · 1.21 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use strict';
/* A general particle class. Useful for all sorts of things like explosions,
* smoke, or other cool special effects. */
class Particle {
constructor(x, y, w = 10, h = 10) {
this.reset(x, y);
this.width = w;
this.height = h;
}
/* Instead of re-creating a particle, you can optionally reset it's values
* like position or life. */
reset(x, y) {
this.position = createVector(x, y);
this.velocity = createVector(randomGaussian() * 0.1,
randomGaussian() * 0.1 - 1);
this.acceleration = createVector();
this.life = 100;
}
dead() {
return this.life <= 0;
}
applyForce(force) {
this.acceleration.add(force);
}
/* */
update() {
this.velocity.add(this.acceleration);
this.position.add(this.velocity);
this.life -= 1;
this.acceleration.mult(0);
}
/* Draw the particle. Transparency is linked to the life left of the
* particle. */
draw() {
let transparency = map(this.life, 0, 100, 0, 255);
noStroke();
fill(220, 220, 220, transparency);
ellipse(this.position.x, this.position.y, this.width, this.height);
}
}