-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
393 lines (340 loc) · 13.4 KB
/
app.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// === CONFIGURATION ===
// Base firework acceleration.
// 1.0 causes fireworks to travel at a constant speed.
// Higher number increases rate firework accelerates over time.
const FIREWORK_ACCELERATION = 1.05;
// Minimum firework brightness.
const FIREWORK_BRIGHTNESS_MIN = 50;
// Maximum firework brightness.
const FIREWORK_BRIGHTNESS_MAX = 70;
// Base speed of fireworks.
const FIREWORK_SPEED = 5;
// Base length of firework trails.
const FIREWORK_TRAIL_LENGTH = 3;
// Determine if target position indicator is enabled.
const FIREWORK_TARGET_INDICATOR_ENABLED = true;
// Minimum particle brightness.
const PARTICLE_BRIGHTNESS_MIN = 50;
// Maximum particle brightness.
const PARTICLE_BRIGHTNESS_MAX = 80;
// Base particle count per firework.
const PARTICLE_COUNT = 80;
// Minimum particle decay rate.
const PARTICLE_DECAY_MIN = 0.015;
// Maximum particle decay rate.
const PARTICLE_DECAY_MAX = 0.03;
// Base particle friction.
// Slows the speed of particles over time.
const PARTICLE_FRICTION = 0.95;
// Base particle gravity.
// How quickly particles move toward a downward trajectory.
const PARTICLE_GRAVITY = 0.7;
// Variance in particle coloration.
const PARTICLE_HUE_VARIANCE = 20;
// Base particle transparency.
const PARTICLE_TRANSPARENCY = 1;
// Minimum particle speed.
const PARTICLE_SPEED_MIN = 1;
// Maximum particle speed.
const PARTICLE_SPEED_MAX = 10;
// Base length of explosion particle trails.
const PARTICLE_TRAIL_LENGTH = 5;
// Alpha level that canvas cleanup iteration removes existing trails.
// Lower value increases trail duration.
const CANVAS_CLEANUP_ALPHA = 0.3;
// Hue change per loop, used to rotate through different firework colors.
const HUE_STEP_INCREASE = 0.5;
// Minimum number of ticks per manual firework launch.
const TICKS_PER_FIREWORK_MIN = 5;
// Minimum number of ticks between each automatic firework launch.
const TICKS_PER_FIREWORK_AUTOMATED_MIN = 20;
// Maximum number of ticks between each automatic firework launch.
const TICKS_PER_FIREWORK_AUTOMATED_MAX = 80;
// === END CONFIGURATION ===
// === LOCAL VARS ===
let canvas = document.getElementById('canvas');
// Set canvas dimensions.
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Set the context, 2d in this case.
let context = canvas.getContext('2d');
// Firework and particles collections.
let fireworks = [], particles = [];
// Mouse coordinates.
let mouseX, mouseY;
// Variable to check if mouse is down.
let isMouseDown = false;
// Initial hue.
let hue = 120;
// Track number of ticks since automated firework.
let ticksSinceFireworkAutomated = 0;
// Track number of ticks since manual firework.
let ticksSinceFirework = 0;
// === END LOCAL VARS ===
// === HELPERS ===
// Use requestAnimationFrame to maintain smooth animation loops.
// Fall back on setTimeout() if browser support isn't available.
window.requestAnimFrame = (() => {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
// Get a random number within the specified range.
function random(min, max) {
return Math.random() * (max - min) + min;
}
// Calculate the distance between two points.
function calculateDistance(aX, aY, bX, bY) {
let xDistance = aX - bX;
let yDistance = aY - bY;
return Math.sqrt(Math.pow(xDistance, 2) + Math.pow(yDistance, 2));
}
// === END HELPERS ===
// === PROTOTYPING ===
// Creates a new firework.
// Path begins at 'start' point and ends and 'end' point.
function Firework(startX, startY, endX, endY) {
// Set current coordinates.
this.x = startX;
this.y = startY;
// Set starting coordinates.
this.startX = startX;
this.startY = startY;
// Set end coordinates.
this.endX = endX;
this.endY = endY;
// Get the distance to the end point.
this.distanceToEnd = calculateDistance(startX, startY, endX, endY);
this.distanceTraveled = 0;
// Create an array to track current trail particles.
this.trail = [];
// Trail length determines how many trailing particles are active at once.
this.trailLength = FIREWORK_TRAIL_LENGTH;
// While the trail length remains, add current point to trail list.
while(this.trailLength--) {
this.trail.push([this.x, this.y]);
}
// Calculate the angle to travel from start to end point.
this.angle = Math.atan2(endY - startY, endX - startX);
// Set the speed.
this.speed = FIREWORK_SPEED;
// Set the acceleration.
this.acceleration = FIREWORK_ACCELERATION;
// Set the brightness.
this.brightness = random(FIREWORK_BRIGHTNESS_MIN, FIREWORK_BRIGHTNESS_MAX);
// Set the radius of click-target location.
this.targetRadius = 2.5;
}
// Update a firework prototype.
// 'index' parameter is index in 'fireworks' array to remove, if journey is complete.
Firework.prototype.update = function(index) {
// Remove the oldest trail particle.
this.trail.pop();
// Add the current position to the start of trail.
this.trail.unshift([this.x, this.y]);
// Animate the target radius indicator.
if (FIREWORK_TARGET_INDICATOR_ENABLED) {
if(this.targetRadius < 8) {
this.targetRadius += 0.3;
} else {
this.targetRadius = 1;
}
}
// Increase speed based on acceleration rate.
this.speed *= this.acceleration;
// Calculate current velocity for both x and y axes.
let xVelocity = Math.cos(this.angle) * this.speed;
let yVelocity = Math.sin(this.angle) * this.speed;
// Calculate the current distance travelled based on starting position, current position, and velocity.
// This can be used to determine if firework has reached final position.
this.distanceTraveled = calculateDistance(this.startX, this.startY, this.x + xVelocity, this.y + yVelocity);
// Check if final position has been reached (or exceeded).
if(this.distanceTraveled >= this.distanceToEnd) {
// Destroy firework by removing it from collection.
fireworks.splice(index, 1);
// Create particle explosion at end point. Important not to use this.x and this.y,
// since that position is always one animation loop behind.
createParticles(this.endX, this.endY);
} else {
// End position hasn't been reached, so continue along current trajectory by updating current coordinates.
this.x += xVelocity;
this.y += yVelocity;
}
}
// Draw a firework.
// Use CanvasRenderingContext2D methods to create strokes as firework paths.
Firework.prototype.draw = function() {
// Begin a new path for firework trail.
context.beginPath();
// Get the coordinates for the oldest trail position.
let trailEndX = this.trail[this.trail.length - 1][0];
let trailEndY = this.trail[this.trail.length - 1][1];
// Create a trail stroke from trail end position to current firework position.
context.moveTo(trailEndX, trailEndY);
context.lineTo(this.x, this.y);
// Set stroke coloration and style.
// Use hue, saturation, and light values instead of RGB.
context.strokeStyle = `hsl(${hue}, 100%, ${this.brightness}%)`;
// Draw stroke.
context.stroke();
if (FIREWORK_TARGET_INDICATOR_ENABLED) {
// Begin a new path for end position animation.
context.beginPath();
// Create an pulsing circle at the end point with targetRadius.
context.arc(this.endX, this.endY, this.targetRadius, 0, Math.PI * 2);
// Draw stroke.
context.stroke();
}
}
// Creates a new particle at provided 'x' and 'y' coordinates.
function Particle(x, y) {
// Set current position.
this.x = x;
this.y = y;
// To better simulate a firework, set the angle of travel to random value in any direction.
this.angle = random(0, Math.PI * 2);
// Set friction.
this.friction = PARTICLE_FRICTION;
// Set gravity.
this.gravity = PARTICLE_GRAVITY;
// Set the hue to somewhat randomized number.
// This gives the particles within a firework explosion an appealing variance.
this.hue = random(hue - PARTICLE_HUE_VARIANCE, hue + PARTICLE_HUE_VARIANCE);
// Set brightness.
this.brightness = random(PARTICLE_BRIGHTNESS_MIN, PARTICLE_BRIGHTNESS_MAX);
// Set decay.
this.decay = random(PARTICLE_DECAY_MIN, PARTICLE_DECAY_MAX);
// Set speed.
this.speed = random(PARTICLE_SPEED_MIN, PARTICLE_SPEED_MAX);
// Create an array to track current trail particles.
this.trail = [];
// Trail length determines how many trailing particles are active at once.
this.trailLength = PARTICLE_TRAIL_LENGTH;
// While the trail length remains, add current point to trail list.
while(this.trailLength--) {
this.trail.push([this.x, this.y]);
}
// Set transparency.
this.transparency = PARTICLE_TRANSPARENCY;
}
// Update a particle prototype.
// 'index' parameter is index in 'particles' array to remove, if journey is complete.
Particle.prototype.update = function(index) {
// Remove the oldest trail particle.
this.trail.pop();
// Add the current position to the start of trail.
this.trail.unshift([this.x, this.y]);
// Decrease speed based on friction rate.
this.speed *= this.friction;
// Calculate current position based on angle, speed, and gravity (for y-axis only).
this.x += Math.cos(this.angle) * this.speed;
this.y += Math.sin(this.angle) * this.speed + this.gravity;
// Apply transparency based on decay.
this.transparency -= this.decay;
// Use decay rate to determine if particle should be destroyed.
if(this.transparency <= this.decay) {
// Destroy particle once transparency level is below decay.
particles.splice(index, 1);
}
}
// Draw a particle.
// Use CanvasRenderingContext2D methods to create strokes as particle paths.
Particle.prototype.draw = function() {
// Begin a new path for particle trail.
context.beginPath();
// Get the coordinates for the oldest trail position.
let trailEndX = this.trail[this.trail.length - 1][0];
let trailEndY = this.trail[this.trail.length - 1][1];
// Create a trail stroke from trail end position to current particle position.
context.moveTo(trailEndX, trailEndY);
context.lineTo(this.x, this.y);
// Set stroke coloration and style.
// Use hue, brightness, and transparency instead of RGBA.
context.strokeStyle = `hsla(${this.hue}, 100%, ${this.brightness}%, ${this.transparency})`;
context.stroke();
}
// === END PROTOTYPING ===
// Cleans up the canvas by removing older trails.
// ...
function cleanCanvas() {
// Set 'destination-out' composite mode, so additional fill doesn't remove non-overlapping content.
context.globalCompositeOperation = 'destination-out';
// Set alpha level of content to remove.
// Lower value means trails remain on screen longer.
context.fillStyle = `rgba(0, 0, 0, ${CANVAS_CLEANUP_ALPHA})`;
// Fill entire canvas.
context.fillRect(0, 0, canvas.width, canvas.height);
// Reset composite mode to 'lighter', so overlapping particles brighten each other.
context.globalCompositeOperation = 'lighter';
}
// Create particle explosion at 'x' and 'y' coordinates.
function createParticles(x, y) {
// Set particle count.
// Higher numbers may reduce performance.
let particleCount = PARTICLE_COUNT;
while(particleCount--) {
// Create a new particle and add it to particles collection.
particles.push(new Particle(x, y));
}
}
// Launch fireworks automatically.
function launchAutomatedFirework() {
// Determine if ticks since last automated launch is greater than random min/max values.
if(ticksSinceFireworkAutomated >= random(TICKS_PER_FIREWORK_AUTOMATED_MIN, TICKS_PER_FIREWORK_AUTOMATED_MAX)) {
// Check if mouse is not currently clicked.
if(!isMouseDown) {
// Set start position to bottom center.
let startX = canvas.width / 2;
let startY = canvas.height;
// Set end position to random position, somewhere in the top half of screen.
let endX = random(0, canvas.width);
let endY = random(0, canvas.height / 2);
// Create new firework and add to collection.
fireworks.push(new Firework(startX, startY, endX, endY));
// Reset tick counter.
ticksSinceFireworkAutomated = 0;
}
} else {
// Increment counter.
ticksSinceFireworkAutomated++;
}
}
// Update all active fireworks.
function updateFireworks() {
// Loop backwards through all fireworks, drawing and updating each.
for (let i = fireworks.length - 1; i >= 0; --i) {
fireworks[i].draw();
fireworks[i].update(i);
}
}
// Update all active particles.
function updateParticles() {
// Loop backwards through all particles, drawing and updating each.
for (let i = particles.length - 1; i >= 0; --i) {
particles[i].draw();
particles[i].update(i);
}
}
// === END APP HELPERS ===
// Primary loop.
function loop() {
// Smoothly request animation frame for each loop iteration.
requestAnimFrame(loop);
// Adjusts coloration of fireworks over time.
hue += HUE_STEP_INCREASE;
// Clean the canvas.
cleanCanvas();
// Update fireworks.
updateFireworks();
// Update particles.
updateParticles();
// Launch automated fireworks.
launchAutomatedFirework();
// Launch manual fireworks.
// launchManualFirework();
}
// Initiate loop after window loads.
window.onload = loop;