-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
615 lines (506 loc) Β· 16.8 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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Circle game</title>
<meta name="description" content="Try and catch it" />
<meta
name="viewport"
content="width=device-width, height=device-height, initial-scale=1, maximum-scale=1.0, user-scalable=no"
/>
<link rel="stylesheet" type="text/css" href="reset.css" />
<link rel="icon" type="image/png" href="favicon.png" />
<link rel="apple-touch-icon" href="favicon.png" />
<!-- Global site tag (gtag.js) - Google Analytics -->
<script
async
src="https://www.googletagmanager.com/gtag/js?id=UA-177518266-1"
></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag("js", new Date());
gtag("config", "UA-177518266-1");
</script>
<style>
* {
/* Disable double tab to zoom on android */
touch-action: manipulation;
}
html {
background-color: black;
user-select: none;
-moz-user-select: none;
-khtml-user-select: none;
-webkit-user-select: none;
-o-user-select: none;
overflow: hidden;
}
#hand-container {
position: relative;
height: 100%;
}
#image-wrapper {
position: relative;
display: inline-block;
left: -150%;
opacity: 0;
/* Debug code */
/* left: 50%; */
/* opacity: 1; */
top: 54%;
}
#neutralize-circle {
position: absolute;
width: 16%;
height: 21%;
left: 45%;
top: 38%;
transform: rotate(20deg);
/* Debug code */
/* border: 1px solid white; */
}
.celebration-container {
display: none;
color: white;
width: 100%;
height: 100%;
z-index: 4;
position: absolute;
align-items: center;
justify-content: center;
flex-direction: row;
}
#me-celebration-title {
font-size: 6vw;
}
#you-celebration-title {
font-size: 9vw;
}
#vs {
margin-right: 15px;
margin-left: 15px;
color: white;
}
#score-container {
display: flex;
justify-content: space-evenly;
align-items: baseline;
}
@media only screen and (max-width: 600px) {
#score-container {
flex-direction: column;
align-items: center;
}
}
.player-container {
color: white;
margin-top: 10px;
display: flex;
align-items: center;
font-size: 22px;
}
#you-score {
display: inline-block;
}
#me-score {
display: inline-block;
}
#dot {
position: fixed;
display: none;
z-index: 2;
width: 10px;
height: 10px;
background-color: white;
border-radius: 50%;
opacity: 0.7;
transform: rotate(20deg);
}
.fade {
-webkit-transition: opacity 500ms ease-in-out;
-moz-transition: opacity 500ms ease-in-out;
-ms-transition: opacity 500ms ease-in-out;
-o-transition: opacity 500ms ease-in-out;
transition: opacity 500ms ease-in-out;
}
.lives {
display: flex;
align-items: center;
justify-content: center;
border: 1px white solid;
min-width: 130px;
padding: 2px;
padding-left: 5px;
padding-right: 5px;
height: 25px;
border-radius: 25px;
font-size: 18px;
}
.lives span {
-webkit-transition: opacity 300ms ease-out;
-moz-transition: opacity 300ms ease-out;
-ms-transition: opacity 300ms ease-out;
-o-transition: opacity 300ms ease-out;
transition: opacity 300ms ease-out;
}
.yellow-border {
border: 1px #ffd809 solid !important;
}
.purple-border {
border: 1px #c433fe solid !important;
}
.player-emoji {
margin-right: 3px;
}
#player-helper {
position: absolute;
opacity: 0;
transition: opacity 0.2s linear;
color: white;
display: flex;
align-items: center;
justify-content: center;
height: 100%;
width: 100%;
font-size: 32px;
}
#player-helper .bold {
font-weight: 600;
}
#player-helper .padding {
padding-left: 16px;
padding-right: 16px;
}
@media screen and (max-width: 800px) {
#player-helper {
font-size: 24px;
}
}
</style>
</head>
<body>
<div id="you-congrats" class="celebration-container">
<div id="you-celebration-title">You π won, good job π</div>
</div>
<div id="me-congrats" class="celebration-container">
<div id="me-celebration-title">Evil π won, better luck next time!</div>
</div>
<div id="player-helper">
<div class="padding">
<span class="bold">Poke the circle</span>
made by the hand to defeat your opponent
</div>
</div>
<div id="score-container">
<div id="you-container" class="player-container">
<span class="player-emoji">π</span>
<span id="you-lives" class="lives">
<span id="you-life-1">π</span>
<span id="you-life-2">π</span>
<span id="you-life-3">π</span>
<span id="you-life-4">π</span>
<span id="you-life-5">π</span>
<span id="you-life-6">π</span>
</span>
</div>
<div id="me-container" class="player-container">
<span class="player-emoji">π</span>
<span id="evil-lives" class="lives">
<span id="evil-life-1">π</span>
<span id="evil-life-2">π</span>
<span id="evil-life-3">π</span>
<span id="evil-life-4">π</span>
<span id="evil-life-5">π</span>
<span id="evil-life-6">π</span>
</span>
</div>
</div>
<div id="hand-container">
<div id="image-wrapper" class="fade">
<div id="dot"></div>
<img id="image" src="handje-iets-groter.PNG" />
<div id="neutralize-circle"></div>
</div>
</div>
<script>
const handContainer = document.getElementById("hand-container");
const imageWrapper = document.getElementById("image-wrapper");
const image = document.getElementById("image");
const dot = document.getElementById("dot");
const neutralizeCircle = document.getElementById("neutralize-circle");
const youCongrats = document.getElementById("you-congrats");
const meCongrats = document.getElementById("me-congrats");
const timeTheHandNeedsToMoveOutOfTheScreen = 200;
let currentTime = 0;
const counterTime = 20;
const newTryTime = 4000;
const helperForUserTime = 4000;
const fadeOutHandTime = 500;
let reactionTimeForPlayer = 1000;
let transitionTime = 500;
const sizeOfHand = 35;
const winningScore = 0;
let neutralized = false;
let youScore = 6;
let evilScore = 6;
let mainLoop;
let userHasInteractedWithTheHand = false;
let tapBuffer, quackBuffer;
let tapAudioContext, quackAudioContext;
const handleFirstUserInteractionWithHand = () => {
if (!userHasInteractedWithTheHand) {
userHasInteractedWithTheHand = true;
/* Add a setTimeout here to make sure that the help text is currently not displayed */
setTimeout(() => {
document.getElementById("player-helper").remove();
}, helperForUserTime);
// Cleanup
document.removeEventListener(
"click",
handleFirstUserInteractionWithHand
);
}
};
// Hand being clicked is the indicator that the user understand the game
imageWrapper.addEventListener(
"click",
handleFirstUserInteractionWithHand
);
const startGame = () => {
let startTime;
const start = (timestamp) => {
if (startTime === undefined) {
startTime = timestamp;
}
const elapsed = timestamp - startTime;
if (elapsed > counterTime) {
/* counterTime is an arbitrary increment, with this logic if the
* screen goes out of focus and it is out of focus for e.g. 20 seconds
* one increment is added, when it comes back in focus to the currentTime
* in this case 20 miliseconds
*/
currentTime += counterTime;
}
if (currentTime > newTryTime) {
moveHandInScreen();
setTimersToHandleScoring();
currentTime = 0;
startTime = undefined;
}
mainLoop = requestAnimationFrame(start);
};
requestAnimationFrame(start);
};
startGame();
const setTimersToHandleScoring = () => {
setTimeout(() => {
if (neutralized === false) {
moveHandOutOfScreen();
}
setTimeout(() => {
if (neutralized === false) {
setNewPlayerScore();
}
const isGameFinished = checkIfTheGameIsFinished();
if (!userHasInteractedWithTheHand && !isGameFinished) {
// Stop the game to stop all the timers placed to play the game
// Needed because helperUserTime is to longer and otherwise the hand would
// already appear in the screen
cancelAnimationFrame(mainLoop);
setTimeout(() => {
document.getElementById("player-helper").style.opacity = 1;
setTimeout(() => {
document.getElementById("player-helper").style.opacity = 0;
startGame();
}, helperForUserTime);
}, 800);
}
neutralized = false;
}, timeTheHandNeedsToMoveOutOfTheScreen);
}, reactionTimeForPlayer);
};
const handleNeutralize = (event) => {
requestAnimationFrame(() => {
if (handHasnotBeenNeutralizedYet()) {
neutralized = true;
setNewComputerScore();
showClickCircle(event);
fadeOutTheHand();
setTimeout(() => {
moveHandOutOfScreen();
dot.style.display = "none";
}, fadeOutHandTime);
setNewReactionTimes();
}
// Stop the event from progating and the user to able to zoom/drag the image
event.preventDefault();
event.stopPropagation();
});
};
neutralizeCircle.addEventListener("touchstart", handleNeutralize, false);
neutralizeCircle.addEventListener("mousedown", handleNeutralize, false);
/* debug code */
// neutralizeCircle.addEventListener('mousedown', () => {console.log('click')}, false);
const moveHandInScreen = () => {
addFadeInAnimation();
// You do 100 - the size of the hand to make sure that the hand is always in the screen
// TODO: improve this algorithm
// Left has a small band with than top when the screen is portait
// This is the other way around when the phone is in portrait
imageWrapper.style.left = getRandomInt(100 - (sizeOfHand - 5)) + "%";
imageWrapper.style.top = getRandomInt(100 - (sizeOfHand - 5)) + "%";
};
const addFadeInAnimation = () => {
imageWrapper.style.opacity = 1;
};
const moveHandOutOfScreen = () => {
resetFadeAnimation();
imageWrapper.style.left = "-150%";
imageWrapper.style.top = "-150%";
};
const resetFadeAnimation = () => {
imageWrapper.style.opacity = 0;
};
const checkIfTheGameIsFinished = () => {
if (evilScore <= winningScore) {
youCongrats.style.display = "flex";
stopTheGame();
return true;
}
if (youScore <= winningScore) {
meCongrats.style.display = "flex";
stopTheGame();
return true;
}
};
const showClickCircle = (event) => {
dot.style.display = "inline-block";
const coordinatesNeutralizeCircle = neutralizeCircle.getBoundingClientRect();
let x, y;
var touches = event.touches;
if (touches) {
const touch = touches[0];
x = touch.pageX;
y = touch.pageY;
} else {
x = event.clientX;
y = event.clientY;
}
const halfOfDotWith = 5;
// dot.style.left = x - coordinatesNeutralizeCircle.left - halfOfDotWith;
dot.style.left = x - halfOfDotWith;
// dot.style.top = y - coordinatesNeutralizeCircle.top - halfOfDotWith;
dot.style.top = y - halfOfDotWith;
};
const handHasnotBeenNeutralizedYet = () => neutralized === false;
const setNewReactionTimes = () => {
reactionTimeForPlayer -= 50;
};
const fadeOutTheHand = () => {
imageWrapper.style.opacity = 0;
};
const removeHeart = (id) => {
const heartElement = document.getElementById(id);
heartElement.style.opacity = 0;
};
const playAudio = (buffer, audioContext) => {
const source = audioContext.createBufferSource(); // creates a sound source
source.buffer = buffer; // tell the source which sound to play
source.connect(audioContext.destination); // connect the source to the context's destination (the speakers)
source.start(0);
};
const setNewComputerScore = () => {
setLifesBorderOfPlayerThatLostALifeRed("evil");
removeHeart("evil-life-" + evilScore);
evilScore = evilScore - 1;
if (evilScore !== winningScore) {
playAudio(tapBuffer, tapAudioContext);
}
};
const setNewPlayerScore = () => {
setLifesBorderOfPlayerThatLostALifeRed("you");
removeHeart("you-life-" + youScore);
youScore = youScore - 1;
playAudio(quackBuffer, quackAudioContext);
};
const setLifesBorderOfPlayerThatLostALifeRed = (user) => {
let borderColor = "purple";
if (user === "evil") {
borderColor = "yellow";
}
document
.getElementById(`${user}-lives`)
.classList.add(`${borderColor}-border`);
setTimeout(() => {
document
.getElementById(`${user}-lives`)
.classList.remove(`${borderColor}-border`);
}, 1000);
};
const getBrowserWidth = () => document.body.clientWidth;
const getBrowserHeight = () => document.body.clientHeight;
const setHandSizeBasedOnScreenSize = () => {
const screenWidth = getBrowserWidth();
const screenHeight = getBrowserHeight();
if (screenWidth < screenHeight) {
imageWrapper.style.width = "45%";
image.style.width = "100%";
} else {
imageWrapper.style.height = "45%";
image.style.height = "100%";
}
};
setHandSizeBasedOnScreenSize();
const disableZoomAndDragAbilities = () => {
window.addEventListener(
"touchstart",
() => {
if (event.touches.length > 1) {
event.preventDefault();
}
},
false
);
image.setAttribute("draggable", false);
};
disableZoomAndDragAbilities();
const stopTheGame = () => {
cancelAnimationFrame(mainLoop);
};
const getRandomInt = (max) => Math.floor(Math.random() * Math.floor(max));
const supportsTouch =
"ontouchstart" in window || navigator.msMaxTouchPoints;
const createAudioContext = (url) => {
// Audio context is used to not make audio that is already playing on a mobile phone to be stopped.
window.AudioContext = window.AudioContext || window.webkitAudioContext;
// All modern browser support this, therefore no fallback provided
const audioContext = new AudioContext();
const request = new XMLHttpRequest();
request.open("GET", url, true);
request.responseType = "arraybuffer";
// Decode asynchronously
request.onload = () => {
audioContext.decodeAudioData(
request.response,
(buffer) => {
if (url.includes("tap")) {
tapBuffer = buffer;
} else {
quackBuffer = buffer;
}
},
(error) => {
console.log(`sound ${url} could not be loaded `, error);
}
);
};
request.send();
return audioContext;
};
tapAudioContext = createAudioContext("tap.wav");
quackAudioContext = createAudioContext("quack.wav");
</script>
</body>
</html>