-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticle.js
More file actions
77 lines (66 loc) · 2 KB
/
Particle.js
File metadata and controls
77 lines (66 loc) · 2 KB
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
class Particle {
constructor(lifeloss) {
this.acc = createVector(0, 0);
this.done = false;
this.lifespan = 255;
this.lifeloss = lifeloss || 0;
}
applyForce(force) {
this.acc.add(force);
}
update() {
if (!this.done) {
this.vel.add(this.acc);
this.pos.add(this.vel);
this.acc.mult(0);
if (this.lifespan < 0)
this.done = true;
this.lifespan -= this.lifeloss;
}
}
}
class Sparkle extends Particle {
constructor(x, y, color, shape, lifeloss) {
super(lifeloss);
this.pos = createVector(x, y);
this.vel = p5.Vector.random2D();
if (shape.name == "NORMAL") {
this.vel.mult(random(2, 5));
} else if (shape.name == "HEART") {
if (this.vel.y > 0) {
if (this.vel.x < 0) {
this.vel.y = -0.4*pow(this.vel.x, 2) + 0.6*this.vel.x + 1;
} else {
this.vel.y = -0.4*pow(this.vel.x, 2) - 0.6*this.vel.x + 1;
}
} else {
if (this.vel.x < 0) {
this.vel.y = 2*pow(this.vel.x, 2) + 2*this.vel.x;
} else {
this.vel.y = 2*pow(this.vel.x, 2) - 2*this.vel.x;
}
}
this.vel.mult(shape.size*random(0.8, 1.2));
} else if (shape.name == "CIRCLE") {
this.vel.mult(shape.size*random(0.8, 1.2));
}
this.color = color;
}
show() {
strokeWeight(4);
stroke(this.color[0], this.color[1], this.color[2], this.lifespan);
point(this.pos.x, this.pos.y);
}
}
class Base extends Particle {
constructor(x) {
super();
this.pos = createVector(x, height);
this.vel = createVector(0, random(-8, -10));
}
show() {
strokeWeight(8);
stroke(255, random(153, 255), 0);
point(this.pos.x, this.pos.y);
}
}