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
|
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
font-family: sans-serif;
}
:root {
--bg: #161616;
}
html {
height: 100vh;
width: 100vw;
background-color: var(--bg);
font-size: 24px;
overflow-x: hidden;
}
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
width: 100vw;
}
#frame {
width: 60%;
height: 75%;
/** border: 1px solid white; */
}
#center {
height: 50px;
width: 50px;
background-color: var(--bg);
border: 5px solid red;
border-radius: 50%;
position: absolute;
border-radius: 50%;
}
.target {
height: 50px;
width: 50px;
background-color: red;
position: absolute;
border-radius: 50%;
}
.target:active {
background-color: orange;
}
.hidden {
display: none;
opacity: 0;
}
.fade {
transition: opacity 1s;
}
.title {
font-weight: bold;
font-size: 2rem;
}
#text-box {
display: flex;
flex-direction: row;
align-content: end;
justify-content: space-evenly;
text-align: center;
color: red;
width: 100%;
min-height: 2rem;
line-height: 1rem;
margin-top: 2rem;
}
@media screen and (max-width: 750px) {
html {
font-size: 32px;
}
#text-box {
flex-direction: column;
align-items: center;
}
.text { font-size: .5rem; }
.title { font-size: 2rem; }
}
</style>
</head>
<body>
<div id="frame">
<!-- where the targets + bullseye are drawn -->
</div>
<!-- where we place text + buttons -->
<div id="text-box">
<!-- intro related -->
<div style="display: flex; gap: 1rem;">
<span id="intro1" class="hidden title">QUICK</span>
<span id="intro2" class="hidden title">DRAW</span>
</div>
<!-- gamemode menu buttons-->
<div id="game-modes" class="hidden fade">
<button id="normal" onclick="handleNormalMode()">normal</button>
<button id="countdown">countdown</button>
<!-- <button id="infinite">infinite</button> -->
</div>
<!-- pre-round text -->
<span id="gameText" class="hidden fade"></span>
<!-- post round text -->
<span id="round-end" class="hidden fade"></span>
<!-- countdown text -->
<span id="ready" class="hidden title">READY?</span>
<span id="countdown3" class="hidden title">3</span>
<span id="countdown2" class="hidden title">2</span>
<span id="countdown1" class="hidden title">1</span>
<span id="draw" class="hidden title">DRAW!</span>
</div>
<script>
let gameBorderWidth = 1;
let targetWidth = 50;
let targetHeight = 50;
let pageHeight;
let pageWidth;
let frameElement;
let frameRect;
let frameHeight;
let frameWidth;
let center;
let ratio;
let xFrameAdjustment;
let yFrameAdjustment;
let hovering = false;
let hoverCount = 0; // number of times the cursor has hovered over bullseye. used to cleanly handle round starting / cancelling
let targetsClickedThisRound = 0;
let gamemode = ""; // can be: "normal", "countdown", "infinite"
let roundmode = ""; // if gamemode is "countdown" or "infinite", can be: "rounds" or "frenzy"
let currentRound = 0;
let countingDown = false;
let totalTime = 0.0;
let roundTimeStart = 0;
let roundTimeEnd = 0;
let roundSeconds = 0.0;
let bestNormalTime = "none";
// text elements
let ready = document.getElementById("ready");
let intro1 = document.getElementById("intro1");
let intro2 = document.getElementById("intro2");
let gameText = document.getElementById("gameText");
let one = document.getElementById("countdown1");
let two = document.getElementById("countdown2");
let three = document.getElementById("countdown3");
let draw = document.getElementById("draw");
window.addEventListener("load", initGame);
function setParameters(event) {
pageHeight = window.innerHeight;
pageWidth = window.innerWidth;
frameElement = document.getElementById("frame");
frameRect = frameElement.getBoundingClientRect();
frameHeight = frameElement.offsetHeight;
frameWidth = frameElement.offsetWidth;
// ratio = (frameHeight - (gameBorderWidth*2)) / (frameWidth - (gameBorderWidth*2));
ratio = frameHeight / frameWidth;
// adjustments to find center of frame in absolute coords (for origin of target cirlce)
xFrameAdjustment = ((frameWidth / 2) + frameRect.left) - (targetWidth / 2);
yFrameAdjustment = ((frameHeight / 2) + frameRect.top) - (targetWidth / 2);
// init logs:
console.log('page: ' + pageWidth + ' x ' + pageHeight);
console.log('frame: ' + frameWidth + ' x ' + frameHeight);
}
async function initGame() {
setParameters();
center = generateTarget(xFrameAdjustment, yFrameAdjustment);
center.id = "center";
document.body.insertAdjacentElement("afterbegin", center);
// await drawTargetsInCircle(2);
await drawTargetsInCircle(false);
// insert intro elements into document flow
intro1.style.display = "inline";
intro2.style.display = "inline";
// reveal intro elements
intro1.style.opacity = "100";
await sleep(500);
intro2.style.opacity = "100";
await sleep(1500);
intro1.classList.add("fade");
intro2.classList.add("fade");
intro1.style.opacity = "0";
intro2.style.opacity = "0";
await sleep(1000);
intro1.style.display = "none";
intro2.style.display = "none";
window.addEventListener("resize", handlePageResize);
document.getElementById("game-modes").style.display = "inline";
await sleep(500);
document.getElementById("game-modes").style.opacity = "100";
if (localStorage.getItem("bestNormalTime") != null) {
bestNormalTime = Number.parseFloat(localStorage.getItem("bestNormalTime"));
}
}
async function handleNormalMode(e) {
gamemode = "normal";
console.log("game mode: " + gamemode);
currentRound = 1;
totalTime = 0;
center.addEventListener("mouseover", handleHoverStart);
center.addEventListener("mouseleave", handleHoverEnd);
document.getElementById("game-modes").style.opacity = "0";
await sleep(500);
document.getElementById("game-modes").style.display = "none";
intro1.style.display = "none";
intro2.style.display = "none";
gameText.innerText = "hover on middle circle to start the countdown,\nthen click the targets as fast as possible!";
gameText.style.opacity = "0";
gameText.style.display = "inline";
await sleep(500);
gameText.style.opacity = "100";
}
async function handleReset(e) {
currentRound = 0;
center.removeEventListener("mouseover", handleReset);
gameText.style.display = "none";
gameText.style.opacity = "0";
intro1.style.display = "inline";
intro2.style.display = "inline";
intro1.style.opacity = "100";
intro2.style.opacity = "100";
document.getElementById("game-modes").style.display = "inline";
await sleep(500);
document.getElementById("game-modes").style.opacity = "100";
await drawTargetsInCircle(2);
}
async function handleHoverStart(e) {
hovering = true;
hoverCount++;
console.log('hover count: ' + hoverCount);
console.log('current round: ' + currentRound);
frameElement.replaceChildren();
// intro1.style.display = "none";
// intro2.style.display = "none";
gameText.style.display = "none";
if (gamemode == "normal") {
await startNormalRound(currentRound);
}
// if (infinityMode)
}
async function startNormalRound(roundNumber) {
if (roundNumber > 3) {
handleReset({});
return;
}
if (!await startCountdown(hoverCount)) {
// if user canceled hover, don't continue round
console.log("player canceled coundown");
return;
}
let numTargets = 0;
if (roundNumber == 1) {
numTargets = 1;
}
else if (roundNumber == 2) {
numTargets = 3;
}
else if (roundNumber == 3) {
numTargets = 5;
}
// effectively begin the round
console.log("starting round " + roundNumber);
document.getElementById("center").remove();
targetsClickedThisRound = 0;
roundTimeStart = Date.now();
placeTarget(numTargets);
while (targetsClickedThisRound < numTargets) {
await sleep(10);
if (targetsClickedThisRound == numTargets) break;
}
roundTimeEnd = Date.now();
roundSeconds = (roundTimeEnd - roundTimeStart) / 1000;
totalTime += roundSeconds;
currentRound++;
// bring bullseye back to screen
center = generateTarget(xFrameAdjustment, yFrameAdjustment);
center.id = "center";
document.body.insertAdjacentElement("afterbegin", center);
// ensure draw isn't still lingering on screen
draw.style.display = "none";
draw.style.opacity = "0";
if (roundNumber == 3) {
// GAME OVER STUFF
if (totalTime < bestNormalTime || bestNormalTime == "none") {
alert("NEW FASTEST TIME!!! previous best: " + bestNormalTime + "s. congratulations cowboy.");
bestNormalTime = totalTime;
localStorage.setItem("bestNormalTime", bestNormalTime);
}
gameText.innerText = "ROUND: " + roundSeconds.toFixed(3) +
"s\nTOTAL: " + totalTime.toFixed(3) +
"s\nBEST SCORE: " + bestNormalTime.toFixed(3) +
"s\nhover on circle to go back to main menu..";
center.addEventListener("mouseover", handleReset);
}
else {
console.log("ROUND: " + roundSeconds.toFixed(3) + "s\nTOTAL: " + totalTime.toFixed(3) + "\nhover on circle to start round " + currentRound);
gameText.innerText = "ROUND: " + roundSeconds.toFixed(3) + "s\nTOTAL: " + totalTime.toFixed(3) + "s\nhover on circle to start round " + currentRound;
center.addEventListener("mouseover", handleHoverStart);
center.addEventListener("mouseleave", handleHoverEnd);
}
gameText.style.display = "inline";
gameText.style.opacity = "100";
return;
}
async function startCountdown(hoverNumber) {
gameText.style.display = "none";
countingDown = true;
// this if-check prevents countdown from continuing if user removes / reapplies hover while we're sleeping
if (!hovering || hoverNumber != hoverCount) return false;
ready.style.display = "inline";
ready.style.opacity = "100";
await sleep(1000);
if (!hovering || hoverNumber != hoverCount) return false;
ready.style.display = "none";
ready.style.opacity = "0";
if (!hovering || hoverNumber != hoverCount) return false;
one.style.opacity = "0";
two.style.opacity = "0";
three.style.opacity = "0";
if (!hovering || hoverNumber != hoverCount) return false;
one.style.display = "inline";
two.style.display = "inline";
three.style.display = "inline";
if (!hovering || hoverNumber != hoverCount) return false;
three.style.opacity = "100";
await sleep(1000);
if (!hovering || hoverNumber != hoverCount) return false;
two.style.opacity = "100";
await sleep(1000);
if (!hovering || hoverNumber != hoverCount) return false;
one.style.opacity = "100";
await sleep(1000);
if (!hovering || hoverNumber != hoverCount) return false;
one.style.display = "none";
two.style.display = "none";
three.style.display = "none";
one.style.opacity = "0";
two.style.opacity = "0";
three.style.opacity = "0";
draw.style.display = "inline";
draw.style.opacity = "100";
countingDown = false;
setTimeout(() => {draw.style.display = "none"; draw.style.opacity = "0"; }, 1000);
return true;
}
function handleHoverEnd(e) {
hovering = false;
if (countingDown) {
ready.style.display = "none";
one.style.display = "none";
two.style.display = "none";
three.style.display = "none";
draw.style.display = "none";
ready.style.opacity = "0";
one.style.opacity = "0";
two.style.opacity = "0";
three.style.opacity = "0";
draw.style.opacity = "0";
if (currentRound <= 1) {
drawTargetsInCircle();
gameText.innerText = "hover on middle circle to start the countdown,\nthen click the targets as fast as possible!";
}
else {
gameText.innerText = "ROUND: " + roundSeconds.toFixed(3) + "s\nTOTAL: " + totalTime.toFixed(3) + "s\nhover on circle to start round " + currentRound;
}
gameText.style.display = "inline";
countingDown = false;
}
}
function handleTargetClick(e) {
targetsClickedThisRound++;
e.target.remove();
}
function handlePageResize(event) {
frameElement.replaceChildren();
setParameters();
document.getElementById("center").remove();
center = generateTarget(xFrameAdjustment, yFrameAdjustment);
center.id = "center";
document.body.insertAdjacentElement("afterbegin", center);
if (currentRound == 0 || (gamemode == "normal" && currentRound == 1)) {
drawTargetsInCircle();
}
if (currentRound >= 1) {
center.addEventListener("mouseover", handleHoverStart);
center.addEventListener("mouseleave", handleHoverEnd);
}
console.log('page: ' + pageWidth + ' x ' + pageHeight);
console.log('frame: ' + frameWidth + ' x ' + frameHeight);
}
async function drawTargetsInCircle(drawDurationSeconds) {
let r, x, y;
let radians;
let target;
for (let i = 0; i < 360; i = i + 2) {
r = (frameWidth / 2) - (targetWidth / 2) - (gameBorderWidth * 2);
radians = degreesToRadians(i);
x = (r * 1 * Math.cos(radians)) + xFrameAdjustment;
y = (r * ratio * Math.sin(radians)) + yFrameAdjustment;
target = generateTarget(x, y);
document.getElementById("frame").insertAdjacentElement("afterbegin", target);
if (drawDurationSeconds) await sleep((drawDurationSeconds * 1000) / 180);
}
}
function degreesToRadians(degrees) {
return degrees * (Math.PI / 180)
}
// function to place a target somewhere random on the circle
function placeTarget(numTargets) {
let radians, radius
let x, y;
let target;
for (let i = 0; i < numTargets; i++){
radians = degreesToRadians(getRandomInt(0, 360));
radius = (frameWidth / 2) - (targetWidth / 2) - (gameBorderWidth * 2);
x = (radius * 1 * Math.cos(radians)) + xFrameAdjustment;
y = (radius * ratio * Math.sin(radians)) + yFrameAdjustment;
target = generateTarget(x, y);
target.classList.add("target");
target.addEventListener("click", handleTargetClick);
document.getElementById("frame").insertAdjacentElement("afterbegin", target);
}
}
function generateTarget(x, y) {
const target = document.createElement("div");
target.classList.add("target");
target.style.top = y +'px';
target.style.left = x + 'px';
// console.log(target);
return target;
}
function getRandomInt(min, max) {
const minCeiled = Math.ceil(min);
const maxFloored = Math.floor(max);
return Math.floor(Math.random() * (maxFloored - minCeiled) + minCeiled); // The maximum is exclusive and the minimum is inclusive
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
</script>
</body>
</html>
|