-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReactionTimeTest.html
More file actions
138 lines (122 loc) · 4.28 KB
/
ReactionTimeTest.html
File metadata and controls
138 lines (122 loc) · 4.28 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Reaction Time Test</title>
<style>
:root {
--bg-wait: #2b87d1;
--bg-ready: #ce2635;
--bg-click: #4bdb66;
--text: #ffffff;
}
body {
margin: 0;
padding: 0;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: var(--bg-wait);
color: var(--text);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
text-align: center;
user-select: none;
transition: background-color 0.1s ease;
}
#arena {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
cursor: pointer;
}
h1 { font-size: 3rem; margin: 10px; }
p { font-size: 1.5rem; }
.stats {
position: absolute;
top: 20px;
font-weight: bold;
}
#best-time {
color: #ffd700;
}
</style>
</head>
<body>
<div class="stats">
Personal Best: <span id="best-time">-</span>ms
</div>
<div id="arena">
<div id="icon"></div>
<h1 id="status-text">Reaction Test</h1>
<p id="sub-text">Click anywhere to start</p>
</div>
<script>
const arena = document.getElementById('arena');
const statusText = document.getElementById('status-text');
const subText = document.getElementById('sub-text');
const bestDisplay = document.getElementById('best-time');
let state = 'START';
let startTime = 0;
let timeoutId = null;
// Load record
const savedBest = localStorage.getItem('reactionBest');
if (savedBest) bestDisplay.innerText = savedBest;
function setMode(mode) {
state = mode;
switch (mode) {
case 'START':
document.body.style.backgroundColor = '#2b87d1';
statusText.innerText = 'Reaction Test';
subText.innerText = 'Click anywhere to start';
break;
case 'WAITING':
document.body.style.backgroundColor = '#ce2635';
statusText.innerText = 'Wait for green...';
subText.innerText = '';
const delay = Math.floor(Math.random() * 3000) + 2000;
timeoutId = setTimeout(() => setMode('READY'), delay);
break;
case 'READY':
document.body.style.backgroundColor = '#4bdb66';
statusText.innerText = 'CLICK!';
subText.innerText = '';
startTime = performance.now();
break;
case 'TOO_SOON':
clearTimeout(timeoutId);
document.body.style.backgroundColor = '#2b87d1';
statusText.innerText = 'Too soon!';
subText.innerText = 'Click to try again';
break;
case 'RESULT':
document.body.style.backgroundColor = '#2b87d1';
subText.innerText = 'Click to try again';
break;
}
}
arena.addEventListener('mousedown', () => {
if (state === 'START' || state === 'RESULT' || state === 'TOO_SOON') {
setMode('WAITING');
} else if (state === 'WAITING') {
setMode('TOO_SOON');
} else if (state === 'READY') {
const endTime = performance.now();
const reactionTime = Math.round(endTime - startTime);
statusText.innerText = reactionTime + ' ms';
setMode('RESULT');
const currentBest = localStorage.getItem('reactionBest');
if (!currentBest || reactionTime < parseInt(currentBest)) {
localStorage.setItem('reactionBest', reactionTime);
bestDisplay.innerText = reactionTime;
}
}
});
</script>
</body>
</html>