forked from andrewdcampbell/jsfireworks
-
Notifications
You must be signed in to change notification settings - Fork 3
/
sketch.js
275 lines (245 loc) · 7 KB
/
sketch.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
const GRAVITY = 0.2;
const SHELLTYPES = [
"simple",
"split",
"burst",
"double",
"mega",
"writer",
"pent",
"comet",
];
let PAUSED = false;
let MUTE = false;
var shells = [];
var sparks = [];
var sounds = [];
function setup() {
createCanvas(windowWidth, windowHeight);
background(0);
strokeWeight(1);
colorMode(HSB);
for (let i = 0; i < 3; i++) {
sounds.push(loadSound("sounds/explosion" + i + ".mp3"));
}
}
/*
From p5.js docs: Called directly after setup(), the draw() function continuously
executes the lines of code contained inside its block until the program is
stopped or noLoop() is called.
*/
function draw() {
translate(width / 2, height);
background("rgba(0, 0, 0, 0.2)");
/* Remove the exploded shells and burnt out sparks */
shells = shells.filter((shell) => !shell.exploded);
sparks = sparks.filter((spark) => spark.brt > 0);
/* Draw the shells and sparks */
for (let shell of shells) shell.draw();
for (let spark of sparks) spark.draw();
/* Generate new shell with small probability */
if (random() < 0.03) {
let s = new Shell();
shells.push(s);
}
}
function touchMoved() {
touchStarted();
return false;
}
function touchStarted() {
let speed = createVector(0, 0);
let pos = createVector(mouseX - width / 2, mouseY - height);
let s = new Shell(pos, speed);
s.explode();
return false;
}
function keyPressed() {
// Space bar
if (keyCode == 32) {
if (!PAUSED) {
PAUSED = true;
// Draw a pause symbol in top right corner
strokeWeight(1);
fill(255);
rect(width / 2 - 30, -height + 20, 10, 30);
rect(width / 2 - 50, -height + 20, 10, 30);
// Stop the draw loop
noLoop();
} else {
PAUSED = false;
loop();
}
return false;
}
// 's' for sound effects
if (keyCode == 83) {
MUTE = !MUTE;
}
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}
function Shell(pos, vel, type, sparkTrail) {
this.pos = pos || createVector(int(random(-width / 4, width / 4)), 0);
this.vel = vel || createVector(random(-2, 2), -random(11, 16));
this.sparkTrail = sparkTrail || random() < 0.5;
this.fuse = random(-3, -1);
this.hue = round(random(0, 360));
this.type = type;
this.exploded = false;
// Get random type, if not set
if (this.type == undefined) {
let randIndex = floor(random(0, SHELLTYPES.length));
this.type = SHELLTYPES[randIndex];
}
this.draw = () => {
// Explode if at the apex (i.e., vel )
if (this.fuse < this.vel.y) {
this.explode();
return;
}
// Spark trail
if (this.sparkTrail) {
// Random spark direction between 0 and 2 pi
let sparkDir = random(0, TWO_PI);
// Random velocity between 0 and 1
let sparkVel = random(0, 1);
// Random speed? - maybe the jitter?
let sparkSpd = createVector(
sparkVel * cos(sparkDir),
sparkVel * sin(sparkDir)
);
// Spark position
let sparkPos = createVector(
this.pos.x + sparkSpd.x,
this.pos.y + sparkSpd.y
);
let s = new Spark(
sparkPos,
sparkSpd,
random(50, 75),
floor(random(20, 40)),
floor(random(0, 30))
);
sparks.push(s);
}
// Color shell ±10 hue and 0-20 saturation, fixed brightness
stroke(this.hue + round(random(-10, 10)), random(0, 20), 90);
point(this.pos.x, this.pos.y);
// Update shell position
this.pos.add(this.vel);
this.vel.y = this.vel.y + GRAVITY;
};
this.drawSparks = (
numSparks,
velMin,
velMax,
fadeMin,
fadeMax,
type,
baseDir,
angle
) => {
for (let i = 0; i < numSparks; i++) {
let dir = random(0, TWO_PI);
if (baseDir != undefined) dir = baseDir + random(0, PI / angle);
let vel = random(velMin, velMax);
let sparkSpd = createVector(
this.vel.x + vel * cos(dir),
this.vel.y + vel * sin(dir)
);
let hue = this.hue + round(random(-10, 10));
let sat = round(random(0, 40));
let fade = random(fadeMin, fadeMax);
let spark = new Spark(this.pos.copy(), sparkSpd, fade, hue, sat, type);
sparks.push(spark);
}
};
this.explode = () => {
if (this.type == "split") {
this.drawSparks(30, 3, 5, 3, 8, "writer");
this.drawSparks(10, 3, 5, 3, 6, "sparkr");
} else if (this.type == "burst") {
this.drawSparks(60, 0, 6, 3, 8, "sparkr");
} else if (this.type == "double") {
this.drawSparks(90, 3, 5, 2, 4);
this.drawSparks(90, 0.5, 2, 4, 6, "writer");
} else if (this.type == "mega") {
this.drawSparks(600, 0, 8, 3, 8);
} else if (this.type == "writer") {
this.drawSparks(100, 0, 5, 1, 3, "writer");
} else if (this.type == "simple") {
this.drawSparks(100, 0, 5, 1, 3);
} else if (this.type == "pent") {
let baseDir = random(0, TWO_PI);
this.drawSparks(20, 3, 5, 3, 8, "writer", baseDir + (2 / 5) * PI, 6);
this.drawSparks(20, 3, 5, 3, 8, "writer", baseDir + (4 / 5) * PI, 6);
this.drawSparks(20, 3, 5, 3, 8, "writer", baseDir + (6 / 5) * PI, 6);
this.drawSparks(20, 3, 5, 3, 8, "writer", baseDir + (8 / 5) * PI, 6);
this.drawSparks(20, 3, 5, 3, 8, "writer", baseDir + (10 / 5) * PI, 6);
} else if (this.type == "comet") {
let baseDir = random(0, TWO_PI);
this.drawSparks(10, 3, 7, 3, 8, "sparkr", baseDir + (2 / 3) * PI, 128);
this.drawSparks(10, 3, 7, 3, 8, "sparkr", baseDir + (4 / 3) * PI, 128);
this.drawSparks(10, 3, 7, 3, 8, "sparkr", baseDir + (6 / 3) * PI, 128);
this.drawSparks(200, 0, 8, 3, 8, "writer");
}
this.exploded = true;
if (!MUTE) {
let randIndex = floor(random(0, sounds.length));
sounds[randIndex].play();
}
};
}
function Spark(pos, vel, fade, hue, sat, type = "default") {
// Position
this.pos = pos;
// Velocity
this.vel = vel;
this.fade = fade;
this.hue = hue;
this.sat = sat;
this.type = type;
// Brightness
this.brt = 255;
this.burntime = 0;
this.draw = () => {
// Color
stroke(this.hue, this.sat, this.brt);
// Trail
// NB: Confusing
let newXPos = this.pos.x + log(this.burntime) * 8 * this.vel.x;
let newYPos =
this.pos.y +
log(this.burntime) * 8 * this.vel.y +
this.burntime * GRAVITY;
point(newXPos, newYPos);
if (this.type == "writer" && this.burntime > 1) {
line(
newXPos,
newYPos,
this.pos.x + log(this.burntime - 2) * 8 * this.vel.x,
this.pos.y +
log(this.burntime - 2) * 8 * this.vel.y +
this.burntime * GRAVITY
);
}
if (this.type == "sparkr") {
let dir = random(0, TWO_PI);
let vel = random(0, 1);
let sparkSpd = createVector(vel * cos(dir), vel * sin(dir));
let spark = new Spark(
createVector(newXPos + sparkSpd.x, newYPos + sparkSpd.y),
sparkSpd,
random(50, 75),
round(random(20, 40)),
round(random(0, 30))
);
sparks.push(spark);
}
// Fade
this.brt -= this.fade;
this.burntime++;
};
}