This repository has been archived by the owner on Sep 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexplodetest.html
251 lines (196 loc) · 5.16 KB
/
explodetest.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<!-- Always force latest IE rendering engine (even in intranet) & Chrome Frame
Remove this if you use the .htaccess -->
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>explodetest</title>
<meta name="description" content="">
<meta name="author" content="Sal">
<meta name="viewport" content="width=device-width; initial-scale=1.0">
<!-- Replace favicon.ico & apple-touch-icon.png in the root of your domain and delete these references -->
<link rel="shortcut icon" href="/favicon.ico">
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
</head>
<body>
<div>
<header>
<h1>explodetest</h1>
</header>
<nav>
<p>
<a href="/">Home</a>
</p>
<p>
<a href="/contact">Contact</a>
</p>
</nav>
<div>
<!DOCTYPE html>
<html>
<head>
<title>Explosion Effect</title>
<script src="//ajax.googleapis.com/ajax/libs/mootools/1.4.5/mootools-yui-compressed.js">
</script>
<script>
var canvas;
var context2D;
var particles = [];
function randomFloat (min, max)
{
return min + Math.random()*(max-min);
}
/*
* A single explosion particle
*/
function Particle ()
{
this.scale = 1.0;
this.x = 0;
this.y = 0;
this.radius = 20;
this.color = "#000";
this.velocityX = 0;
this.velocityY = 0;
this.scaleSpeed = 0.5;
this.update = function(ms)
{
// shrinking
this.scale -= this.scaleSpeed * ms / 1000.0;
if (this.scale <= 0)
{
this.scale = 0;
}
// moving away from explosion center
this.x += this.velocityX * ms/1000.0;
this.y += this.velocityY * ms/1000.0;
};
this.draw = function(context2D)
{
// translating the 2D context to the particle coordinates
context2D.save();
context2D.translate(this.x, this.y);
context2D.scale(this.scale, this.scale);
// drawing a filled circle in the particle's local space
context2D.beginPath();
context2D.arc(0, 0, this.radius, 0, Math.PI*2, true);
context2D.closePath();
context2D.fillStyle = this.color;
context2D.fill();
context2D.restore();
};
}
/*
* Basic Explosion, all particles move and shrink at the same speed.
*
* Parameter : explosion center
*/
function createBasicExplosion(x, y)
{
// creating 4 particles that scatter at 0, 90, 180 and 270 degrees
for (var angle=0; angle<360; angle+=90)
{
var particle = new Particle();
// particle will start at explosion center
particle.x = x;
particle.y = y;
particle.color = "#FF0000";
var speed = 50.0;
// velocity is rotated by "angle"
particle.velocityX = speed * Math.cos(angle * Math.PI / 180.0);
particle.velocityY = speed * Math.sin(angle * Math.PI / 180.0);
// adding the newly created particle to the "particles" array
particles.push(particle);
}
}
/*
* Advanced Explosion effect
* Each particle has a different size, move speed and scale speed.
*
* Parameters:
* x, y - explosion center
* color - particles' color
*/
function createExplosion(x, y, color)
{
var minSize = 10;
var maxSize = 30;
var count = 10;
var minSpeed = 60.0;
var maxSpeed = 200.0;
var minScaleSpeed = 1.0;
var maxScaleSpeed = 4.0;
for (var angle=0; angle<360; angle += Math.round(360/count))
{
var particle = new Particle();
particle.x = x;
particle.y = y;
particle.radius = randomFloat(minSize, maxSize);
particle.color = color;
particle.scaleSpeed = randomFloat(minScaleSpeed, maxScaleSpeed);
var speed = randomFloat(minSpeed, maxSpeed);
particle.velocityX = speed * Math.cos(angle * Math.PI / 180.0);
particle.velocityY = speed * Math.sin(angle * Math.PI / 180.0);
particles.push(particle);
}
}
function update (frameDelay)
{
// draw a white background to clear canvas
context2D.fillStyle = "#FFF";
context2D.fillRect(0, 0, context2D.canvas.width, context2D.canvas.height);
// update and draw particles
for (var i=0; i<particles.length; i++)
{
var particle = particles[i];
particle.update(frameDelay);
particle.draw(context2D);
}
}
window.addEvent("load", function()
{
// canvas and 2D context initialization
canvas = document.getElementById("canvas");
context2D = canvas.getContext("2d");
// Button click : BOOM !
document.id("explosion").addEvent("click", function()
{
var x = randomFloat(100, 400);
var y = randomFloat(100, 400);
createExplosion(x, y, "#525252");
createExplosion(x, y, "#FFA318");
});
// Button click : basic effect
document.id("basic_explosion").addEvent("click", function()
{
createBasicExplosion(250, 200);
});
// starting the game loop at 60 frames per second
var frameRate = 60.0;
var frameDelay = 1000.0/frameRate;
setInterval(function()
{
update(frameDelay);
}, frameDelay);
});
</script>
</head>
<body>
<div>
<button id="explosion">BOOM !</button>
<button id="basic_explosion">Basic Explosion</button>
</div>
<canvas id="canvas" width="600" height="600">
</canvas>
</body>
</html>
</div>
<footer>
<p>
© Copyright by Sal
</p>
</footer>
</div>
</body>
</html>