-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreaction_timer.html
More file actions
235 lines (206 loc) · 6.3 KB
/
reaction_timer.html
File metadata and controls
235 lines (206 loc) · 6.3 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Reaction Timer</title>
<link href="https://fonts.googleapis.com/css2?family=Oxanium:wght@400;600;700&family=Rajdhani:wght@400;600;700&display=swap" rel="stylesheet">
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: 'Rajdhani', sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: #1a1a2e;
color: #eee;
user-select: none;
}
.game {
text-align: center;
width: 400px;
}
h1 { font-family: 'Oxanium', sans-serif; font-size: 2rem; margin-bottom: 1.5rem; }
.box {
width: 300px;
height: 200px;
margin: 0 auto 1.5rem;
border-radius: 12px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
cursor: pointer;
transition: background 0.1s;
background: #16213e;
border: 2px solid #0f3460;
}
.box.waiting { background: #16213e; }
.box.ready { background: #0f3460; }
.box.go { background: #e94560; }
.box .msg {
font-family: 'Oxanium', sans-serif;
font-size: 1.4rem;
font-weight: 700;
}
.box .sub {
font-size: 0.85rem;
opacity: 0.7;
margin-top: 0.4rem;
}
.stats {
display: flex;
justify-content: space-around;
margin-bottom: 1.5rem;
}
.stat { font-size: 0.9rem; }
.stat span { font-family: 'Oxanium', sans-serif; display: block; font-size: 1.6rem; font-weight: 700; }
button {
padding: 0.6rem 1.5rem;
font-size: 1rem;
border: none;
border-radius: 8px;
background: #e94560;
color: #fff;
cursor: pointer;
font-weight: 600;
}
button:hover { opacity: 0.9; }
.history {
margin-top: 1.5rem;
font-size: 0.85rem;
opacity: 0.7;
}
.history span { margin: 0 4px; }
</style>
</head>
<body>
<div class="game">
<h1>Reaction Timer</h1>
<div class="box waiting" id="box">
<div class="msg">Press to Start</div>
<div class="sub">Wait for red, then press!</div>
</div>
<div class="stats">
<div class="stat">Last <span id="last">—</span></div>
<div class="stat">Best <span id="best">—</span></div>
<div class="stat">Average <span id="avg">—</span></div>
</div>
<button id="resetBtn">Reset Stats</button>
<div class="history" id="history"></div>
</div>
<script>
const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
function playBlip() {
const o = audioCtx.createOscillator();
const g = audioCtx.createGain();
o.type = 'sine';
o.frequency.value = 1000;
g.gain.setValueAtTime(0.3, audioCtx.currentTime);
g.gain.exponentialRampToValueAtTime(0.001, audioCtx.currentTime + 0.08);
o.connect(g).connect(audioCtx.destination);
o.start(); o.stop(audioCtx.currentTime + 0.08);
}
function playBlop() {
const o = audioCtx.createOscillator();
const g = audioCtx.createGain();
o.type = 'sine';
o.frequency.setValueAtTime(600, audioCtx.currentTime);
o.frequency.exponentialRampToValueAtTime(200, audioCtx.currentTime + 0.1);
g.gain.setValueAtTime(0.3, audioCtx.currentTime);
g.gain.exponentialRampToValueAtTime(0.001, audioCtx.currentTime + 0.1);
o.connect(g).connect(audioCtx.destination);
o.start(); o.stop(audioCtx.currentTime + 0.1);
}
function playBurrb() {
const o = audioCtx.createOscillator();
const g = audioCtx.createGain();
o.type = 'square';
o.frequency.value = 150;
g.gain.setValueAtTime(0.3, audioCtx.currentTime);
g.gain.exponentialRampToValueAtTime(0.001, audioCtx.currentTime + 0.3);
o.connect(g).connect(audioCtx.destination);
o.start();
o.stop(audioCtx.currentTime + 0.3);
}
const box = document.getElementById('box');
const msg = box.querySelector('.msg');
const sub = box.querySelector('.sub');
const elLast = document.getElementById('last');
const elBest = document.getElementById('best');
const elAvg = document.getElementById('avg');
const elHist = document.getElementById('history');
const resetBtn = document.getElementById('resetBtn');
let state = 'idle'; // idle | waiting | go
let startTime = 0;
let times = JSON.parse(localStorage.getItem('rt_times') || '[]');
function updateStats() {
if (!times.length) {
elLast.textContent = '—';
elBest.textContent = '—';
elAvg.textContent = '—';
elHist.textContent = '';
return;
}
elLast.textContent = times[times.length - 1] + ' ms';
elBest.textContent = Math.min(...times) + ' ms';
elAvg.textContent = Math.round(times.reduce((a, b) => a + b, 0) / times.length) + ' ms';
elHist.textContent = 'Recent: ' + times.slice(-5).reverse().map(t => t + 'ms').join(', ');
}
function startRound() {
state = 'waiting';
box.className = 'box waiting';
msg.textContent = 'Wait for red...';
sub.textContent = "Don't click yet!";
playBlip();
const delay = Math.random() * 2000 + 1000; // 1–3 seconds
setTimeout(() => {
if (state !== 'waiting') return;
state = 'go';
box.className = 'box go';
msg.textContent = 'PRESS NOW!';
sub.textContent = '';
startTime = performance.now();
}, delay);
}
box.addEventListener('mousedown', (e) => {
e.preventDefault();
if (state === 'idle') {
startRound();
return;
}
if (state === 'waiting') {
// Pressed too early
state = 'idle';
box.className = 'box waiting';
msg.textContent = 'Too early!';
sub.textContent = 'Press to try again';
playBurrb();
return;
}
if (state === 'go') {
const reaction = Math.round(performance.now() - startTime);
times.push(reaction);
localStorage.setItem('rt_times', JSON.stringify(times));
updateStats();
state = 'idle';
box.className = 'box waiting';
msg.textContent = reaction + ' ms';
sub.textContent = 'Press to play again';
playBlop();
}
});
resetBtn.addEventListener('click', () => {
times = [];
localStorage.removeItem('rt_times');
updateStats();
state = 'idle';
box.className = 'box waiting';
msg.textContent = 'Press to Start';
sub.textContent = 'Wait for red, then press!';
});
updateStats();
</script>
</body>
</html>