forked from infusion/HTML5-Experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
catch-it.html
284 lines (198 loc) · 8.5 KB
/
catch-it.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Catch it!</title>
<style>
*{
font-family:Arial;
}
canvas{
margin:0px;
padding:0px;
border-right:#333 dashed;
border-bottom:#333 dashed;
cursor: none;
}
body{
margin:0px;
background:#fc0;
}
.label {
font-weight:bold;
}
.label span {
font-weight:normal;
}
img {
position:fixed;
right:20px;
bottom:20px;
z-index:1000;
border:0px;
}
</style>
</head>
<body>
<canvas id="canvas" height="1000" width="1000">how can you use the internet?</canvas>
<div class="label">Score: <span id="score">0</span></div>
<div class="label">Max-Score: <span id="max_score">0</span></div>
<div class="label">Tries: <span id="tries">1</span></div>
<div style="padding-top:50px;">Read the <a href="/2010/02/my-very-first-chrome-experiment/">detailed description</a></div>
<a href="http://www.xarg.org/projects/"><img src="/image/xarg-logo.png" alt="xarg open projects" /></a>
<script>
/**
* @license HTML5 experiment Bubble Evolution
* http://www.xarg.org/project/chrome-experiment/
*
* Copyright (c) 2011, Robert Eisele ([email protected])
* Dual licensed under the MIT or GPL Version 2 licenses.
**/
(function(window) {
var XSIZE=990;
var YSIZE=990;
var EPSILON=5;
var ctx;
var document = window.document;
// our ball object holder
var balls = new Array();
// mouse position cache
var mouse = {x: -100, y: -100};
// our prey we want to hunt
var prey = {x: Math.random() * XSIZE, y: Math.random() * YSIZE};
// cache 2*PI for arc()
var pi2 = Math.PI * 2;
var tries = 1;
var score = 0;
var maxScore = 0;
var currentColor = '#333';
var activeDecay = -1;
var lastCatch = Date.now();
function $(id) {
return document.getElementById(id);
}
function updateStat() {
$('tries').innerHTML = tries;
$('score').innerHTML = score;
$('max_score').innerHTML = maxScore;
}
function Ball(x, y, xsee, ysee) {
this.x = x;
this.y = y;
this.xsee = xsee;
this.ysee = ysee;
this.type = Math.random() * 1.05 | 0;
this.move = function() {
if (this.x > XSIZE+EPSILON) {
this.x = XSIZE+EPSILON;
this.xsee = -this.xsee;
} else if (this.x < EPSILON) {
this.x = EPSILON;
this.xsee = -this.xsee;
}
if (this.y > YSIZE+EPSILON) {
this.y = YSIZE+EPSILON;
this.ysee = -this.ysee;
} else if (this.y < 5) {
this.y = 5;
this.ysee = -this.ysee;
}
this.x += this.xsee;
this.y += this.ysee;
if (this.type === 0) {
ctx.fillStyle = currentColor;
} else {
ctx.fillStyle = '#fff';
}
ctx.beginPath();
ctx.arc(this.x, this.y, 5, 0, pi2, true);
ctx.closePath();
ctx.fill();
};
}
function createBall() {
var x, y;
do {
x = Math.random() * (XSIZE+EPSILON);
y = Math.random() * (YSIZE+EPSILON);
} while (prey.x <= x + 35 && x <= prey.x + 55 && prey.y <= y + 35 && y <= prey.y + 55);
balls.push(new Ball(x, y, Math.random() * 5.5 - 2.75, Math.random() * 5.5 - 2.75));
}
ctx = $('canvas').getContext('2d');
window.setInterval(clock, 1000 / 60);
function clock() {
// global clear is faster for many balls
ctx.clearRect(0, 0, XSIZE+2*EPSILON, YSIZE+2*EPSILON);
ctx.fillStyle = "#c00";
ctx.fillRect(prey.x, prey.y, 20, 20);
ctx.fillStyle = "#c00";
ctx.beginPath();
ctx.arc(mouse.x, mouse.y, 10, 0, pi2, true);
ctx.closePath();
ctx.fill();
if (activeDecay !== -1) {
var now = Date.now();
var pct = (now - activeDecay) / 3000;
if (pct >= 1) {
activeDecay = -1;
pct = 1;
}
currentColor = 'rgba(51,51,51,'+(1 - 2 * pct)*(1 - 2 * pct)+')';
}
for (var i = 0; i < balls.length; i++) {
balls[i].move();
if (
balls[i].x <= mouse.x + 15 && mouse.x <= balls[i].x + 15 &&
balls[i].y <= mouse.y + 15 && mouse.y <= balls[i].y + 15 &&
((mouse.x - balls[i].x) * (mouse.x - balls[i].x) + (mouse.y - balls[i].y) * (mouse.y - balls[i].y)) <= 225) {
if (balls[i].type === 0) {
balls = new Array();
if (maxScore < score) {
maxScore = score;
}
score = 0;
tries++;
updateStat();
activeDecay = -1;
currentColor = '#333';
} else {
activeDecay = Date.now();
}
}
}
}
document.onclick = function(ev) {
var x = ev.pageX;
var y = ev.pageY;
for (var i = 0; i < balls.length; i++) {
var t = balls[i];
var d = ((t.x - x) * (t.x - x) + (t.y - y) * (t.y - y));
t.xsee += (t.x - x) / d * 180;
t.ysee += (t.y - y) / d * 180;
}
};
document.ontouchstart = function(e) {
};
document.ontouchmove = document.onmousemove = function(e) {
e.preventDefault();
mouse.x = e.pageX;
mouse.y = e.pageY;
ctx.fillStyle = "#c00";
ctx.beginPath();
ctx.arc(mouse.x, mouse.y, 10, 0, pi2, true);
ctx.closePath();
ctx.fill();
/////////////
if (prey.x <= mouse.x + 10 && mouse.x <= prey.x + 30 && prey.y <= mouse.y + 10 && mouse.y <= prey.y + 30) {
prey = {x: Math.random() * (XSIZE-2*EPSILON), y: Math.random() * (YSIZE-2*EPSILON)};
createBall();
var now = Date.now();
score+= Math.round(Math.max(5, 30 * Math.exp((now - lastCatch) * 0.000089)) * (activeDecay === -1 ? 1 : 2));
lastCatch = now;
updateStat();
}
};
})(this);
</script>
</body>
</html>