-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparticles.js
More file actions
112 lines (107 loc) · 2.38 KB
/
particles.js
File metadata and controls
112 lines (107 loc) · 2.38 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
let partColors = ["#812", "#26C", "#262"];
Particles = {
parts: [],
update: function(){
let len = this.parts.length;
for (let i = 0; i < len; i++) {
if (this.parts[i].update) {
this.parts[i].update();
}
else {
this.parts.splice(i--, 1);
len--;
}
}
},
draw: function(ctx, camerax){
for (let p of this.parts) {
if (p.draw) p.draw(ctx, camerax);
}
},
addPart: function(x, y, color){
this.parts.push(newPart(x,y, color));
},
addSpiralPart: function(x, y){
this.parts.push(newSpiralPart(x,y, color));
},
explode: function(x, y, color, t=20, rscale=10) {
for (let i = 0; i < t; i++){
this.parts.push(newPart(x, y, color, rscale));
}
},
spiral: function(x, y, color, t=20, rscale=10) {
for (let i = 0; i < t; i++){
this.parts.push(newSpiralPart(x,y, color, rscale));
}
}
}
function newPart(x, y, color, rscale) {
return {
ox: x,
oy: y,
x: x,
y: y,
r: 1 + rscale*Math.random(),
v: .2,
color: color,
theta: Math.random() * TAU,
draw: function(ctx, camerax){
ctx.beginPath();
let oldW = ctx.lineWidth;
ctx.lineWidth = 1;
ctx.fillStyle = this.color;
ctx.arc(this.x - camerax, this.y, this.r, 0, TAU);
ctx.fill();
ctx.strokeStyle = "black";
ctx.stroke();
ctx.closePath();
ctx.lineWidth = oldW;
},
update: function(){
this.x += this.v * this.r * Math.cos(this.theta);
this.y += this.v * this.r * Math.sin(this.theta);
this.r -= .1;
if (this.r <= 0) {
this.update = null;
this.draw = () => {};
return;
}
this.theta += .3-.6*Math.random();
},
}
}
function newSpiralPart(x, y, color, rscale) {
return {
ox: x,
oy: y,
x: x,
y: y,
r: 2 + rscale*Math.random(),
v: 5,
color: Math.random() > .7 ? "white" : color,
theta: Math.random() * TAU,
draw: function(ctx, camerax){
ctx.beginPath();
let oldW = ctx.lineWidth;
ctx.lineWidth = 1;
ctx.fillStyle = this.color;
ctx.arc(this.x - camerax, this.y, this.r, 0, TAU);
ctx.fill();
ctx.strokeStyle = "black";
ctx.stroke();
ctx.closePath();
ctx.lineWidth = oldW;
},
update: function(){
this.x += this.r * Math.cos(this.theta);
this.y += this.r * Math.sin(this.theta);
this.r -= .1;
if (this.r <= 0) {
this.update = null;
this.draw = () => {};
return;
}
this.theta += .05;
},
}
}