-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
81 lines (69 loc) · 3.29 KB
/
index.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Skyrocket.js | example</title>
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/styles.css">
</head>
<body onresize="resizeCanvas()" onload="resizeCanvas(); addFirework();">
<div class="message">
<h3>Merry new year!</h3>
<p>Skyrocket.js</p>
<span class="small">Click anywhere to launch another skyrocket</span>
</div>
<figure id="main">
<div class="backdrop"></div>
</figure>
<canvas id="canvas" width="500" height="500">
Canvas not supported, please try a different web browser.
</canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.20.2/TweenMax.min.js"></script>
<script src="js/backdrop.js"></script>
<script src="skyrockets/colors.js"></script>
<script src="skyrockets/explosions.js"></script>
<script src="js/skyrocket.js"></script>
<script>
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
function addFirework()
{
// Check if the window has focus, only if yes then add more skyrockets. This is to prevent 100s of rockets
// firing off if the browser tab has been in the background for some time.
if (document.hasFocus()) {
// Choose a random explosion and firework colour from those defined in the /skyrockets directory.
var randExplosion = Math.floor(Math.random() * explosions.length);
var randColor = Math.floor(Math.random() * colors.length);
// Create a new firework and launch it straight away.
//++ @TODO it might be nice if the firework added itelf to the array and incremented the fireCount in
//++ its constructor rather than the developer having to worry about this.
fireworks[fireCount] = new Firework(fireCount, explosions[randExplosion], colors[randColor]);
fireworks[fireCount].launch();
// Increment this for next time.
fireCount ++;
}
// Set random timeout to call this function again at a random interval.
var randTimeout = Math.floor((Math.random() * 2500) + 300);
setTimeout(addFirework, randTimeout);
}
// Add click event handler to the canvas which adds a new firework and makes it travel to the x,y of the mouse pointer.
canvas.onclick = function (e)
{
// Choose a random skyrocket.
var randExplosion = Math.floor(Math.random() * explosions.length);
var randColor = Math.floor(Math.random() * colors.length);
// Create a new firework.
var rocket = new Firework(fireCount, explosions[randExplosion], colors[randColor]);
// Set target x and y to mouse poistion.
rocket.targetX = e.clientX;
rocket.targetY = e.clientY;
// Add it to the array for drawing.
fireworks[fireCount] = rocket;
// Launch the rocket.
rocket.launch();
// Increment this for next time.
fireCount ++;
}
</script>
</body>
</html>