-
Notifications
You must be signed in to change notification settings - Fork 0
/
particle.js
62 lines (51 loc) · 1.53 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
let noiseScale=12
class Particle {
constructor(x, y) {
this.redGrad = map(y, height, 0, 142, 70);
this.greenGrad = map(y, height, 0, 0, 38);
this.blueGrad = map(y, height, 0, 51, 253);
this.trailLength = 20;
this.pos = createVector(x, y);
this.vel = createVector(0, 0);
this.acc = createVector(0, 0);
this.maxSpeed = 4;
this.size = map(y, height, 0, 10, 2);
this.color = color(this.redGrad, this.greenGrad, this.blueGrad);
this.lifespan = 200;
this.trail = [];
}
update() {
// Perlin noise movement
let angle = noise(this.pos.x * noiseScale, this.pos.y * noiseScale) * TWO_PI * 4;
this.acc.set(cos(angle), sin(angle));
this.acc.mult(0.1);
this.vel.add(this.acc);
this.vel.limit(this.maxSpeed);
this.pos.add(this.vel);
// Add current position to trail
this.trail.push(this.pos.copy());
// Remove old trail points
if (this.trail.length > this.trailLength) {
this.trail.shift();
}
// Decrease lifespan
this.lifespan -= 1;
}
display() {
// Draw trail
push();
noFill();
beginShape();
for (let i = 0; i < this.trail.length; i++) {
let alpha = map(i, 0, this.trail.length, 0, this.lifespan);
stroke(this.redGrad, this.greenGrad, this.blueGrad, alpha);
strokeWeight(this.size);
vertex(this.trail[i].x, this.trail[i].y);
}
endShape();
pop();
}
isDead() {
return this.lifespan < 0;
}
}