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
|
<!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;
}
html {
height: 100vh;
width: 100vw;
background-color: #161616;
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: 20px;
width: 20px;
background-color: red;
position: absolute;
}
.target {
height: 20px;
width: 20px;
background-color: red;
position: absolute;
}
.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;
color: red;
width: 100%;
min-height: 2rem;
line-height: 2rem;
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"></div>
<div id="text-box">
<span id="intro1" class="hidden title">QUICK</span>
<span id="intro2" class="hidden title">DRAW</span>
<span id="instructions" class="hidden fade">hover on bullseye to start</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>
<div id="game-modes" class="hidden fade">
<button id="normal" onclick="handleNormalMode">normal mode</button>
<button id="normal">infinite mode</button>
<button id="normal">countdown mode</button>
</div>
<span id="draw" class="hidden title">DRAW!</span>
</div>
<script>
let gameBorderWidth = 1;
let targetWidth = 20;
let targetHeight = 20;
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;
let currentRound = 0;
let countingDown = false;
// text elements
let intro1 = document.getElementById("intro1");
let intro2 = document.getElementById("intro2");
let instructions = document.getElementById("instructions");
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(true);
// 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", handleResize);
document.getElementById("game-modes").style.display = "inline";
await sleep(500);
document.getElementById("game-modes").style.opacity = "100";
}
function handleNormalMode(e) {
center.addEventListener("mouseover", handleHoverStart);
center.addEventListener("mouseleave", handleHoverEnd);
// instructions.style.display = "inline";
// instructions.style.opacity = "100";
}
function handleHoverStart(e) {
e.target.style.backgroundColor = "green";
if (currentRound == 0) return;
hovering = true;
hoverCount++;
frameElement.replaceChildren();
intro1.style.display = "none";
intro2.style.display = "none";
instructions.style.display = "none";
// if (infinityMode)
if (currentRound == 0) { startRoundOne(); }
}
async function startRoundOne() {
if (!await startCountdown(hoverCount)) {
// if user canceled hover, don't continue round
return;
}
console.log('success');
}
async function startCountdown(hoverNumber) {
instructions.style.display = "none";
countingDown = true;
await sleep(1000);
one.style.opacity = "0";
two.style.opacity = "0";
three.style.opacity = "0";
// this check prevents countdown from continuing if user removes / reapplies hover while we're sleeping
if (!hovering || hoverNumber != hoverCount) return false;
one.style.display = "inline";
two.style.display = "inline";
three.style.display = "inline";
three.style.opacity = "100";
await sleep(1000);
two.style.opacity = "100";
await sleep(1000);
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;
e.target.style.backgroundColor = "red";
if (countingDown) {
one.style.display = "none";
two.style.display = "none";
three.style.display = "none";
draw.style.display = "none";
one.style.opacity = "0";
two.style.opacity = "0";
three.style.opacity = "0";
draw.style.opacity = "0";
instructions.style.display = "inline";
countingDown = false;
}
}
function handleResize(event) {
frameElement.replaceChildren();
setParameters();
document.getElementById("center").remove();
center = generateTarget(xFrameAdjustment, yFrameAdjustment);
center.id = "center";
document.body.insertAdjacentElement("afterbegin", center);
center.addEventListener("mouseover", handleHoverStart);
center.addEventListener("mouseleave", handleHoverEnd);
if (currentRound == 0) {
drawTargetsInCircle(false);
}
console.log('page: ' + pageWidth + ' x ' + pageHeight);
console.log('frame: ' + frameWidth + ' x ' + frameHeight);
}
async function drawTargetsInCircle(animate) {
let r, x, y;
let radians;
let target;
for (let i = 0; i < 360; i = i + 2) {
r = (frameWidth / 2) - (targetWidth / 2) - (gameBorderWidth * 2);
// r = (frameWidth / 2) - (targetWidth / 2);
radians = degreesToRadians(i);
x = (r * 1 * Math.cos(radians)) + xFrameAdjustment;
y = (r * ratio * Math.sin(radians)) + yFrameAdjustment;
// console.log(x + ', ' + y);
// console.log(ratio);
// console.log(xFrameAdjustment + 'adj, ' + yFrameAdjustment + 'adj');
target = generateTarget(x, y);
document.getElementById("frame").insertAdjacentElement("afterbegin", target);
if (animate) await sleep(15);
}
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function degreesToRadians(degrees) {
return degrees * (Math.PI / 180)
}
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
}
</script>
</body>
</html>
|