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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
|
<!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;
padding: 1rem;
}
#target-frame {
width: 60%;
height: 75%;
/** border: 1px solid white; */
}
#center {
height: 50px;
width: 50px;
background-color: red;
/** border: 5px solid red; */
border-radius: 50%;
position: absolute;
border-radius: 50%;
}
.target {
height: 50px;
width: 50px;
background-color: var(--bg);
border: 10px solid red;
position: absolute;
border-radius: 50%;
}
.target:active {
background-color: orange;
}
.target::after {
position: absolute;
background-color: red;
border-radius: 2rem;
height: 10px;
width: 10px;
top: 10px;
left: 10px;
content: "";
}
.red-dot {
height: 50px;
width: 50px;
background-color: red;
position: absolute;
border-radius: 50%;
}
.hidden {
display: none;
opacity: 0;
}
.fade {
transition: opacity 1s;
}
.title {
font-weight: bold;
font-size: 2rem;
}
#title-container {
justify-self: center;
justify-content: center;
flex-direction: row;
width: 100%;
gap: 1rem;
}
#text-frame {
display: flex;
flex-direction: column;
justify-content: flex-end;
gap: 1rem;
min-height: 15%;
width: 100%;
color: red;
text-align: center;
line-height: 1rem;
margin-top: 2rem;
}
#game-modes {
}
.button-container {
/** display: flex; */
align-content: center;
align-items: center;
justify-content: center;
width: 100%;
gap: 1rem;
}
#game-over-buttons {
gap: 2rem;
}
button {
background-color: var(--bg);
color: red;
padding: 12px 12px;
border: 2px solid red;
border-radius: 1rem;
font-size: .75rem;
font-weight: bold;
}
button:hover {
cursor: pointer;
text-decoration: underline;
}
button:active {
background-color: red;
color: var(--bg);
font-style: italic;
}
@media screen and (max-width: 1024px) {
html {
font-size: 32px;
overflow: hidden;
}
body {
padding: .5rem .25rem;
max-height: 80vh;
}
#target-frame {
width: 90%;
}
#text-frame {
margin-top: 1.25rem;
}
#game-modes {
gap: .5rem;
}
#normal-sub-modes, #countdown-sub-modes, #game-over-buttons {
flex-direction: column;
gap: .5rem;
}
#game-text {
font-size: .8rem;
}
.title { font-size: 1.5rem; }
button { font-size: .75rem; }
}
</style>
</head>
<body>
<!-- where the targets + bullseye are drawn -->
<div id="target-frame">
</div>
<!-- where we place text + buttons -->
<div id="text-frame">
<!-- intro related -->
<div id="title-container">
<span id="title1" class="hidden title">QUICK</span>
<span id="title2" class="hidden title">DRAW</span>
</div>
<!-- gamemode menu buttons-->
<div id="game-modes" class="button-container hidden fade">
<button id="normal-mode">normal</button>
<button id="countdown-mode">countdown</button>
<!-- <button id="infinite">infinite</button> -->
</div>
<!-- normal mode sub-modes -->
<div id="normal-sub-modes" class="button-container hidden fade">
<button id="normal-3-rounds">3 rounds</button>
<button id="normal-6-rounds">6 rounds</button>
<button id="normal-10-rounds">10 rounds</button>
</div>
<!-- countdown mode sub-modes -->
<div id="countdown-sub-modes" class="button-container hidden fade">
<button id="countdown-15s">15 seconds</button>
<button id="countdown-30s">30 seconds</button>
<button id="countdown-60s">60 seconds</button>
</div>
<!-- pre-round text -->
<span id="game-text" class="hidden fade"></span>
<!-- post game buttons -->
<div id="game-over-buttons" class="button-container hidden fade">
<button id="play-again">play again</button>
<button id="reset-game">back to main menu</button>
</div>
<!-- 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>
// SCREEN / SETUP VARIABLES
let gameBorderWidth = 1;
let targetWidth = 50;
let targetHeight = 50;
let pageHeight;
let pageWidth;
let targetFrame;
let frameRect;
let frameHeight;
let frameWidth;
let center;
let ratio;
let xFrameAdjustment;
let yFrameAdjustment;
// UX HELPERS
let drawingCircle = false;
let needsResizeAdjustment = false;
// GAME STATE VARIABLES
let gamemode = ""; // can be: "normal" or "countdown"
let submode = "";
let countingDown = false;
let hovering = false;
let hoverCount = 0; // number of times the cursor has hovered over bullseye. used to cleanly handle round starting / cancelling
// GAMEPLAY VARIABLES
let highScore = 0;
let targetsClicked = 0;
let numTargets = 0;
// "NORMAL" MODE ONLY VARIABLES
let currentRound = 0;
let maxNormalRounds = 0;
let totalTime = 0.0;
let roundSeconds = 0.0;
let roundTimeStart = 0;
let roundTimeEnd = 0;
let normalBestTimes = {};
let normalBullseyeNeeded = false;
// "COUNTDOWN" MODE ONLY VARIABLES
let countdownBullseyeNeeded = false;
let countdownTime = 0;
let countdownHighScores = {};
// menu / text elements
let titleContainer = document.getElementById("title-container");
let title1 = document.getElementById("title1");
let title2 = document.getElementById("title2");
let gameModes = document.getElementById("game-modes");
let normalSubModes = document.getElementById("normal-sub-modes");
let countdownSubModes = document.getElementById("countdown-sub-modes");
let gameText = document.getElementById("game-text");
let gameOverButtons = document.getElementById("game-over-buttons");
let ready = document.getElementById("ready");
let one = document.getElementById("countdown1");
let two = document.getElementById("countdown2");
let three = document.getElementById("countdown3");
let draw = document.getElementById("draw");
// setup initialization
window.addEventListener("load", initGame);
async function initGame() {
// begin listening for changes
addEventListeners();
// determine screen parameters
setParameters();
// draw into animation
center = generateTarget(xFrameAdjustment, yFrameAdjustment);
center.id = "center";
document.body.insertAdjacentElement("afterbegin", center);
await drawCircle(2);
if (needsResizeAdjustment) {
// auto-reset screen if page was resized during animation
handlePageResize();
}
await fadeMainMenuIn();
// load saved high scores
if (localStorage.getItem("normalBestTimes") != null) {
normalBestTimes = JSON.parse(localStorage.getItem("normalBestTimes"));
}
if (localStorage.getItem("countdownHighScores") != null) {
countdownHighScores = JSON.parse(localStorage.getItem("countdownHighScores"));
}
}
function addEventListeners() {
window.addEventListener("resize", handlePageResize);
// prevent mobile browsers from tweaking when pressing+holding on mobile
document.addEventListener('touchstart', (e) => {e.preventDefault()}, {passive:false});
document.addEventListener('touchmove', (e) => {e.preventDefault()}, {passive:false});
document.addEventListener('touchend', (e) => {e.preventDefault()}, {passive:false});
document.addEventListener('touchcancel', (e) => {e.preventDefault()}, {passive:false});
// set each element's funtion.
// normal mode buttons
document.getElementById("normal-mode").addEventListener('click', (e) => {
e.preventDefault(); handleNormalMode();
}, { passive: false });
document.getElementById("normal-mode").addEventListener('touchend', (e) => {
e.preventDefault(); handleNormalMode();
}, { passive: false });
document.getElementById("normal-3-rounds").addEventListener('click', (e) => {
e.preventDefault(); setupNormalMode(3);
}, { passive: false });
document.getElementById("normal-3-rounds").addEventListener('touchend', (e) => {
e.preventDefault(); setupNormalMode(3);
}, { passive: false });
document.getElementById("normal-6-rounds").addEventListener('click', (e) => {
e.preventDefault(); setupNormalMode(6);
}, { passive: false });
document.getElementById("normal-6-rounds").addEventListener('touchend', (e) => {
e.preventDefault(); setupNormalMode(6);
}, { passive: false });
document.getElementById("normal-10-rounds").addEventListener('click', (e) => {
e.preventDefault(); setupNormalMode(10);
}, { passive: false });
document.getElementById("normal-10-rounds").addEventListener('touchend', (e) => {
e.preventDefault(); setupNormalMode(10);
}, { passive: false });
// countdown mode buttons
document.getElementById("countdown-mode").addEventListener('click', (e) => {
e.preventDefault(); handleCountdownMode();
}, { passive: false });
document.getElementById("countdown-mode").addEventListener('touchend', (e) => {
e.preventDefault(); handleCountdownMode();
}, { passive: false });
document.getElementById("countdown-15s").addEventListener('click', (e) => {
e.preventDefault(); setupCountdownMode(15);
}, { passive: false });
document.getElementById("countdown-15s").addEventListener('touchend', (e) => {
e.preventDefault(); setupCountdownMode(15);
}, { passive: false });
document.getElementById("countdown-30s").addEventListener('click', (e) => {
e.preventDefault(); setupCountdownMode(30);
}, { passive: false });
document.getElementById("countdown-30s").addEventListener('touchend', (e) => {
e.preventDefault(); setupCountdownMode(30);
}, { passive: false });
document.getElementById("countdown-60s").addEventListener('click', (e) => {
e.preventDefault(); setupCountdownMode(60);
}, { passive: false });
document.getElementById("countdown-60s").addEventListener('touchend', (e) => {
e.preventDefault(); setupCountdownMode(60);
}, { passive: false });
// gameover menu buttons
document.getElementById("play-again").addEventListener('click', (e) => {
e.preventDefault(); handlePlayAgain();
}, { passive: false });
document.getElementById("play-again").addEventListener('touchend', (e) => {
e.preventDefault(); handlePlayAgain();
}, { passive: false });
document.getElementById("reset-game").addEventListener('click', (e) => {
e.preventDefault(); handleReset();
}, { passive: false });
document.getElementById("reset-game").addEventListener('touchend', (e) => {
e.preventDefault(); handleReset();
}, { passive: false });
}
// helper function to determine screen-size + ratios for rendering targets
function setParameters(event) {
pageHeight = window.innerHeight;
pageWidth = window.innerWidth;
targetFrame = document.getElementById("target-frame");
frameRect = targetFrame.getBoundingClientRect();
frameHeight = targetFrame.offsetHeight;
frameWidth = targetFrame.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 handleNormalMode(e) {
gamemode = "normal";
normalBullseyeNeeded = true;
await fadeMainMenuOut();
normalSubModes.style.display = "flex";
normalSubModes.style.opacity = "100";
}
async function setupNormalMode(numRounds) {
// fade buttons out
normalSubModes.style.opacity = "0";
sleep(500);
normalSubModes.style.display = "none";
// ensure mode parameters are [re]set
currentRound = 1;
maxNormalRounds = numRounds;
totalTime = 0;
submode = numRounds;
if (Object.keys(normalBestTimes).indexOf(submode.toString()) == -1) {
highScore = 0;
}
else {
highScore = normalBestTimes[submode];
}
// set game instruction text + fade it in
gameText.innerText = "hover on red circle to start the countdown, then click the targets as fast as possible!";
gameText.style.opacity = "0";
gameText.style.display = "inline";
await sleep(500);
gameText.style.opacity = "100";
// enable player to start first round
center.addEventListener("mouseover", handleHoverStart, {passive:false});
center.addEventListener("touchstart", handleHoverStart, {passive:false});
center.addEventListener("mouseleave", handleHoverEnd, {passive:false});
center.addEventListener("touchend", handleHoverEnd, {passive:false});
center.addEventListener("touchmove", handleHoverEnd, {passive:false});
center.addEventListener("touchcancel", handleHoverEnd, {passive:false});
}
async function startNormalRound(roundNumber) {
if (roundNumber > maxNormalRounds) {
handleReset({});
return;
}
// if user cancels hover countdown, don't start round
if (!await drawCountdown(hoverCount)) {
console.log("player canceled coundown");
return;
}
targetsClicked = 0;
if (roundNumber == maxNormalRounds) {
numTargets = roundNumber * 2;
}
else {
numTargets = roundNumber;
}
// effectively begin the round
console.log("starting round " + roundNumber);
center.remove();
normalBullseyeNeeded = false;
roundTimeStart = Date.now();
randomTargetGenerator(numTargets);
while (targetsClicked < numTargets) {
await sleep(10);
if (targetsClicked == numTargets) break;
}
roundTimeEnd = Date.now();
roundSeconds = (roundTimeEnd - roundTimeStart) / 1000;
totalTime += roundSeconds;
// auto-reset bullseye if page gets resized at all
if (needsResizeAdjustment) {
setParameters();
}
// ensure "DRAW" isn't still lingering on screen after round
draw.style.display = "none";
draw.style.opacity = "0";
// GAME OVER STUFF
if (roundNumber == maxNormalRounds) {
if (totalTime < highScore || highScore == 0) {
if (highScore == 0) {
alert("high score set!");
}
else {
alert("NEW FASTEST TIME!!! previous best: " + highScore.toFixed(3) + "s. congratulations cowboy.");
}
highScore = totalTime;
normalBestTimes[submode] = highScore;
localStorage.setItem("normalBestTimes", JSON.stringify(normalBestTimes));
}
gameText.innerText = "ROUND: " + roundSeconds.toFixed(3) +
"s\nTOTAL: " + totalTime.toFixed(3) +
"s\nBEST " + submode.toString() + "-ROUND TIME: " + highScore.toFixed(3) + "s";
numTargets = 0;
targetsClicked =0;
gameOverButtons.style.display = "flex";
gameOverButtons.style.opacity = "100";
}
// NORMAL END OF ROUND STUFF
else {
center = generateTarget(xFrameAdjustment, yFrameAdjustment);
center.id = "center";
document.body.insertAdjacentElement("afterbegin", center);
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, {passive:false});
center.addEventListener("touchstart", handleHoverStart, {passive:false});
center.addEventListener("mouseleave", handleHoverEnd, {passive:false});
center.addEventListener("touchend", handleHoverEnd, {passive:false});
center.addEventListener("touchmove", handleHoverEnd, {passive:false});
center.addEventListener("touchcancel", handleHoverEnd, {passive:false});
normalBullseyeNeeded = true;
currentRound++;
}
gameText.style.display = "inline";
gameText.style.opacity = "100";
return;
}
async function setupCountdownMode(gameDuration) {
// fade buttons out
countdownSubModes.style.opacity = "0";
sleep(500);
countdownSubModes.style.display = "none";
// [re]set game parameters
targetsClicked = 0;
numTargets = 0;
countdownTime = gameDuration;
submode = gameDuration;
if (Object.keys(countdownHighScores).indexOf(submode.toString()) == -1) {
highScore = 0;
}
else {
highScore = countdownHighScores[submode];
}
// set game instruction text + fade it in
gameText.innerText = "hover on red circle to start the countdown, then click as many targets as possible!";
gameText.style.opacity = "0";
gameText.style.display = "inline";
await sleep(500);
gameText.style.opacity = "100";
// enable player to start game
center.addEventListener("mouseover", handleHoverStart, {passive:false});
center.addEventListener("touchstart", handleHoverStart, {passive:false});
center.addEventListener("mouseleave", handleHoverEnd, {passive:false});
center.addEventListener("touchend", handleHoverEnd, {passive:false});
center.addEventListener("touchmove", handleHoverEnd, {passive:false});
center.addEventListener("touchcancel", handleHoverEnd, {passive:false});
}
async function handleCountdownMode(e) {
gamemode = "countdown";
countdownBullseyeNeeded = true;
await fadeMainMenuOut();
countdownSubModes.style.display = "flex";
countdownSubModes.style.opacity = "100";
}
async function handleHoverStart(e) {
e.preventDefault();
hovering = true;
hoverCount++;
console.log('hover count: ' + hoverCount);
console.log('current round: ' + currentRound);
// remove all targets
targetFrame.replaceChildren();
gameText.style.display = "none";
if (gamemode == "normal") {
await startNormalRound(currentRound);
}
else if (gamemode == "countdown") {
await startCountdownMode();
}
// if (infinityMode)
}
async function startCountdownMode() {
// if user cancels hover countdown, don't start round
if (!await drawCountdown(hoverCount)) {
console.log("player canceled coundown");
return;
}
// clear screen + render countdown / score
center.remove();
gameText.innerText = countdownTime.toFixed(3) + "s\ntargets clicked: " + targetsClicked;
// delay reveal of score until after "DRAW" fades
setTimeout(() => {
gameText.style.display = "inline";
gameText.style.opacity = "100";}, 1000);
// begin actual game loop!
randomTargetGenerator(1);
numTargets++;
countdownBullseyeNeeded = false;
while (countdownTime > 0.0) {
gameText.innerText = countdownTime.toFixed(2) + "s\ntargets clicked: " + targetsClicked;
// auto-reset target spawn paremeters if page was resized
if (needsResizeAdjustment) {
handlePageResize();
}
// generate new target if last one was clicked
if (targetsClicked == numTargets) {
randomTargetGenerator(1);
numTargets++;
}
countdownTime = countdownTime - .01;
await sleep(10);
}
// ensure no targets are still on screen after timer ends
targetFrame.replaceChildren();
// show end of round text. if high score, alert player and save
if (targetsClicked > highScore || highScore == 0) {
if (targetsClicked == 0) {
alert("NEW HIGHEST SCORE!!! congratulations cowboy.");
}
else {
alert("NEW HIGHEST SCORE!!! previous best: " + highScore + " targets. congratulations cowboy.");
}
highScore = targetsClicked;
countdownHighScores[submode] = highScore;
localStorage.setItem("countdownHighScores", JSON.stringify(countdownHighScores));
}
gameText.innerText = "TARGETS CLICKED: " + targetsClicked +
"\n" + submode.toString() + "-SECOND HIGH SCORE: " + highScore;
numTargets = 0;
targetsClicked =0;
gameOverButtons.style.display = "flex";
gameOverButtons.style.opacity = "100";
}
/** HELPER TO KICK-OFF COUNTDOWNS FROM HOVER */
async function drawCountdown(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(500);
if (!hovering || hoverNumber != hoverCount) return false;
two.style.opacity = "100";
await sleep(500);
if (!hovering || hoverNumber != hoverCount) return false;
one.style.opacity = "100";
await sleep(500);
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;
// keep "DRAW" on screen for first second of the round
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 (gamemode == "normal") {
if (currentRound == 1) {
gameText.innerText = "hover on red circle to start the countdown, then 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;
}
}
else if (gamemode == "countdown") {
gameText.innerText = "hover on red circle to start the countdown, then click as many targets as possible!";
}
gameText.style.display = "inline";
countingDown = false;
}
}
async function handlePlayAgain(e) {
// hide game over buttons
gameOverButtons.style.display = "none";
gameOverButtons.style.opacity = "0";
// remove game text from DOM flow
gameText.style.display = "none";
gameText.style.opacity = "0";
setParameters();
center = generateTarget(xFrameAdjustment, yFrameAdjustment);
center.id = "center";
document.body.insertAdjacentElement("afterbegin", center);
if (gamemode == "normal") {
handleNormalMode(e);
}
else if (gamemode == "countdown") {
handleCountdownMode(e);
}
else {
alert("cannot play again, gamemode does not exit?");
}
}
async function handleReset(e) {
// bullseye won't be on screen after a game
setParameters();
center = generateTarget(xFrameAdjustment, yFrameAdjustment);
center.id = "center";
document.body.insertAdjacentElement("afterbegin", center);
// refresh game state variables
gamemode = "";
currentRound = 0;
// hide buttons
gameOverButtons.style.display = "none";
gameOverButtons.style.opacity = "0";
// remove game text from DOM flow
gameText.style.display = "none";
gameText.style.opacity = "0";
// reintroduce main menu elements into DOM flow
await sleep(800);
await drawCircle(2);
await fadeMainMenuIn();
// auto-reset bullseye if page was resized during animation
if (needsResizeAdjustment) {
handlePageResize();
}
}
function handleTargetClick(e) {
targetsClicked++;
e.target.remove();
}
function handlePageResize(event) {
// take note if page was resized during animation or mid gameplay to auto-reset at earliest convenience
if (drawingCircle || (targetsClicked < numTargets)) {
needsResizeAdjustment = true;
return;
}
targetFrame.replaceChildren();
setParameters();
// if at main menu, move bullseye
if (gamemode == "") {
center.style.top = yFrameAdjustment + "px";
center.style.left = xFrameAdjustment + "px";
drawCircle(0);
}
// if inbetween gameplay rounds, move bullseye w/ game event listener
else if ((gamemode == "normal" && normalBullseyeNeeded) ||
(gamemode == "countdown" && countdownBullseyeNeeded)
) {
center.style.top = yFrameAdjustment + "px";
center.style.left = xFrameAdjustment + "px";
}
console.log('page: ' + pageWidth + ' x ' + pageHeight);
console.log('frame: ' + frameWidth + ' x ' + frameHeight);
needsResizeAdjustment = false;
}
async function drawCircle(drawDurationSeconds) {
drawingCircle = true;
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);
target.classList.add("red-dot");
targetFrame.insertAdjacentElement("afterbegin", target);
if (drawDurationSeconds) {
target.classList.add("fade");
await sleep((drawDurationSeconds * 1000) / 180);
}
}
drawingCircle = false;
}
// helpers for rendering main menu stuff
async function fadeMainMenuIn() {
title1.classList.remove("fade");
title2.classList.remove("fade");
// add main menu elements to DOM flow
titleContainer.style.display = "flex";
title1.style.display = "inline";
title2.style.display = "inline";
gameModes.style.display = "flex";
// make title elements actually visible
title1.style.opacity = "100";
await sleep(250);
title2.style.opacity = "100";
await sleep(1500);
// add fade class for when elements are removed
title1.classList.add("fade");
title2.classList.add("fade");
// fade in gamemodes
await sleep(500);
gameModes.style.opacity = "100";
}
async function fadeMainMenuOut() {
let targets = document.getElementsByClassName("red-dot");
for (let i = 0; i < targets.length; i++) {
console.log(targets[i]);
targets[i].style.opacity = 0;
};
title1.style.opacity = "0";
title2.style.opacity = "0";
gameModes.style.opacity = "0";
await sleep(500);
targetFrame.replaceChildren();
titleContainer.style.display = "none";
gameModes.style.display = "none";
title1.style.display = "none";
title2.style.display = "none";
}
function degreesToRadians(degrees) {
return degrees * (Math.PI / 180)
}
// function to place a target somewhere random on the circle
function randomTargetGenerator(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);
target.addEventListener("touchend", handleTargetClick);
targetFrame.insertAdjacentElement("afterbegin", target);
}
}
function generateTarget(x, y) {
const target = document.createElement("div");
target.style.top = y +'px';
target.style.left = x + 'px';
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>
|