-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
401 lines (345 loc) · 16 KB
/
script.js
File metadata and controls
401 lines (345 loc) · 16 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
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
document.addEventListener('DOMContentLoaded', () => {
// DOM Elements
const startBtn = document.getElementById('startBtn');
const input = document.getElementById('input');
const quoteDisplay = document.getElementById('quote');
const inputDisplay = document.getElementById('input-display');
const timer = document.getElementById('timer');
const wpmMetrics = document.querySelector('.wpm-metrics');
const clicksMetrics = document.querySelector('.clicks-metrics');
const clickArea = document.getElementById('click-area');
const textContent = document.querySelector('.text-content');
// Metrics Elements
const wpmDisplay = document.getElementById('wpm');
const wpsDisplay = document.getElementById('wps');
const cpsDisplay = document.getElementById('cps');
const cpmDisplay = document.getElementById('cpm');
const accuracyDisplay = document.getElementById('accuracy');
const navItems = document.querySelectorAll('.nav-item');
// State
let currentTest = 'clicks';
let timerInterval;
let startTime;
let isTestActive = false;
let clickCount = 0;
let totalKeystrokes = 0;
let correctKeystrokes = 0; // Test settings
let TEST_DURATION = 10; // seconds
const timeButtons = document.querySelectorAll('.time-btn');
// Word bank for generating random sentences
const wordBank = [
// Common verbs
"run", "jump", "play", "write", "read", "speak", "listen", "think", "create", "build",
"design", "code", "type", "click", "scroll", "share", "learn", "teach", "grow", "develop",
// Common nouns
"time", "year", "people", "way", "day", "man", "thing", "woman", "life", "child",
"world", "school", "state", "family", "student", "group", "country", "problem", "hand", "part",
"place", "case", "week", "company", "system", "program", "question", "work", "number", "night",
"point", "home", "water", "room", "mother", "area", "money", "story", "fact", "month",
// Technology words
"computer", "internet", "software", "hardware", "website", "database", "network", "server",
"browser", "application", "programming", "developer", "algorithm", "interface", "keyboard",
"monitor", "processor", "memory", "storage", "device",
// Adjectives
"good", "new", "first", "last", "long", "great", "little", "own", "other", "old",
"right", "big", "high", "different", "small", "large", "next", "early", "young", "important",
"few", "public", "bad", "same", "able", "quick", "smart", "clever", "bright", "sharp",
// More nouns
"phone", "book", "eye", "job", "word", "business", "issue", "side", "kind", "head",
"house", "service", "friend", "father", "power", "hour", "game", "line", "end", "member",
"law", "car", "city", "community", "name", "president", "team", "minute", "idea", "kid",
// More verbs
"say", "get", "make", "go", "know", "take", "see", "come", "think", "look",
"want", "give", "use", "find", "tell", "ask", "work", "seem", "feel", "try",
"leave", "call", "begin", "start", "move", "turn", "learn", "change", "play", "live",
// Modern tech terms
"app", "cloud", "data", "email", "file", "video", "social", "digital", "online", "mobile",
"cyber", "viral", "stream", "download", "upload", "backup", "update", "sync", "share", "post",
// Common adverbs
"very", "also", "however", "here", "now", "then", "today", "together", "already", "yet",
"later", "often", "almost", "always", "never", "daily", "quickly", "slowly", "carefully", "easily",
// Prepositions
"in", "on", "at", "with", "by", "from", "up", "about", "into", "over",
"after", "beneath", "under", "above", "across", "through", "behind", "beyond", "during", "without",
// More adjectives
"happy", "sad", "busy", "free", "true", "false", "open", "closed", "full", "empty",
"heavy", "light", "hard", "soft", "hot", "cold", "warm", "cool", "fresh", "tired",
// Modern concepts
"startup", "innovation", "creative", "design", "project", "meeting", "deadline", "goal", "plan", "task",
"team", "group", "leader", "manager", "client", "user", "customer", "product", "service", "market"
];
function generateRandomSentence() {
const patterns = [
"The [adj] [noun] [verb] [prep] the [adj] [noun].",
"[noun] [verb] [prep] the [adj] [noun].",
"A [adj] [noun] [verb] [adv].",
"The [noun] [verb] [prep] [adj] [noun].",
"[adj] [noun] [verb] the [noun] [adv]."
];
const getRandomWord = (type) => {
let words;
switch(type) {
case 'adj':
words = wordBank.filter(w => ['good', 'new', 'first', 'quick', 'smart', 'happy'].includes(w));
break;
case 'noun':
words = wordBank.filter(w => ['time', 'computer', 'phone', 'book', 'app', 'team'].includes(w));
break;
case 'verb':
words = wordBank.filter(w => ['run', 'jump', 'write', 'type', 'click', 'play'].includes(w));
break;
case 'prep':
words = wordBank.filter(w => ['in', 'on', 'at', 'with', 'by', 'from'].includes(w));
break;
case 'adv':
words = wordBank.filter(w => ['very', 'quickly', 'slowly', 'carefully', 'easily'].includes(w));
break;
default:
words = wordBank;
}
return words[Math.floor(Math.random() * words.length)];
};
const pattern = patterns[Math.floor(Math.random() * patterns.length)];
return pattern.replace(/\[(.*?)\]/g, (_, type) => getRandomWord(type));
} function getRandomLines(count) {
let result = [];
let usedSentences = new Set();
while (result.length < count) {
const sentence = generateRandomSentence();
if (!usedSentences.has(sentence)) {
usedSentences.add(sentence);
result.push(sentence);
}
}
return result;
}
let currentTextLines = []; function updateMode(mode) {
if (isTestActive) return; // Don't switch modes during active test
currentTest = mode;
resetTest(); // Reset before changing mode
if (mode === 'clicks') {
clickArea.classList.remove('hidden');
textContent.classList.add('hidden');
input.classList.add('hidden');
clicksMetrics.classList.remove('hidden');
wpmMetrics.classList.add('hidden');
accuracyDisplay.parentElement.classList.add('hidden');
clickArea.textContent = 'Click Start to begin!';
} else {
clickArea.classList.add('hidden');
textContent.classList.remove('hidden');
input.classList.remove('hidden');
clicksMetrics.classList.add('hidden');
wpmMetrics.classList.remove('hidden');
accuracyDisplay.parentElement.classList.remove('hidden');
setupTypingTest();
}
}
function setupTypingTest() {
currentTextLines = getRandomLines(3);
let displayText = currentTextLines.join('\n');
quoteDisplay.innerHTML = displayText.split('\n').map(line =>
`<div class="quote-line">${line}</div>`
).join('');
input.value = '';
input.placeholder = 'Start typing to begin...';
inputDisplay.innerHTML = '';
}
function updateTextLines() {
currentTextLines.shift();
currentTextLines.push(getRandomLines(1)[0]);
let displayText = currentTextLines.join('\n');
quoteDisplay.innerHTML = displayText.split('\n').map(line =>
`<div class="quote-line">${line}</div>`
).join('');
} function calculateMetrics() {
const timeElapsed = Math.max(0.001, (Date.now() - startTime) / 1000);
const minutes = timeElapsed / 60;
if (currentTest === 'clicks') {
const cps = clickCount / timeElapsed;
const cpm = cps * 60;
cpsDisplay.textContent = cps.toFixed(2);
cpmDisplay.textContent = Math.round(cpm);
} else {
// Count actual completed words instead of using keystrokes
const completedWords = currentTextLines.reduce((count, line) => {
return count + line.split(' ').length;
}, 0);
const wpm = (completedWords / minutes);
const wps = completedWords / timeElapsed;
wpmDisplay.textContent = Math.round(wpm);
wpsDisplay.textContent = wps.toFixed(2);
const accuracy = totalKeystrokes > 0 ? (correctKeystrokes / totalKeystrokes) * 100 : 0;
accuracyDisplay.textContent = accuracy.toFixed(1) + '%';
}
} function updateDisplay() {
const currentLine = currentTextLines[0];
const typed = input.value;
let displayHTML = '';
let lineCorrectKeystrokes = 0;
for (let i = 0; i < typed.length; i++) {
if (i < currentLine.length && typed[i] === currentLine[i]) {
displayHTML += typed[i];
lineCorrectKeystrokes++;
} else {
displayHTML += `<span class="error">${typed[i]}</span>`;
}
}
// Update total correct keystrokes
if (typed.endsWith('\n') && typed.trim() === currentLine) {
correctKeystrokes += lineCorrectKeystrokes;
}
inputDisplay.innerHTML = displayHTML;
}
function startTest() {
if (isTestActive) return;
isTestActive = true;
startTime = Date.now();
clickCount = 0;
totalKeystrokes = 0;
correctKeystrokes = 0;
if (currentTest === 'clicks') {
clickArea.textContent = 'Click anywhere!';
clickArea.classList.remove('hidden');
textContent.classList.add('hidden');
input.classList.add('hidden');
} else {
clickArea.classList.add('hidden');
textContent.classList.remove('hidden');
input.classList.remove('hidden'); setupTypingTest();
input.disabled = false;
input.value = '';
inputDisplay.innerHTML = '';
input.focus();
}
timerInterval = setInterval(() => { const timeElapsed = Math.floor((Date.now() - startTime) / 1000);
const timeLeft = Math.max(0, TEST_DURATION - timeElapsed);
timer.textContent = timeLeft;
calculateMetrics();
if (timeElapsed >= TEST_DURATION) {
clearInterval(timerInterval);
isTestActive = false;
if (currentTest === 'clicks') {
clickArea.textContent = 'Test completed! Click Start to try again.';
} else {
input.disabled = true;
}
}
}, 100);
} function resetTest() {
isTestActive = false;
clearInterval(timerInterval);
clickCount = 0;
totalKeystrokes = 0;
correctKeystrokes = 0;
completedLines = [];
timer.textContent = '0';
input.value = '';
inputDisplay.innerHTML = '';
if (currentTest === 'clicks') {
cpsDisplay.textContent = '0';
cpmDisplay.textContent = '0';
} else {
wpmDisplay.textContent = '0';
wpsDisplay.textContent = '0';
accuracyDisplay.textContent = '0%';
input.disabled = true;
}
}
// Time period handling
timeButtons.forEach(btn => {
btn.addEventListener('click', () => {
if (!isTestActive) {
timeButtons.forEach(b => b.classList.remove('active'));
btn.classList.add('active');
TEST_DURATION = parseInt(btn.dataset.time);
resetTest();
}
});
});
// Event Listeners
startBtn.addEventListener('click', startTest); // Click handler with ripple effect
clickArea.addEventListener('click', (e) => {
if (!isTestActive || currentTest !== 'clicks') return;
if (e.target === input) return;
// Create ripple effect
const ripple = document.createElement('div');
ripple.classList.add('ripple');
// Position the ripple at click position
ripple.style.left = `${e.pageX - clickArea.offsetLeft}px`;
ripple.style.top = `${e.pageY - clickArea.offsetTop}px`;
// Add ripple to click area
clickArea.appendChild(ripple);
// Remove ripple after animation
ripple.addEventListener('animationend', () => {
ripple.remove();
});
clickCount++;
calculateMetrics();
}); // Track completed words and accuracy
let completedLines = [];
let lastTypedLength = 0;
input.addEventListener('input', () => {
if (currentTest === 'clicks') return;
if (!isTestActive) {
startTest();
completedLines = []; // Reset completed lines
lastTypedLength = 0;
correctKeystrokes = 0;
}
const currentLine = currentTextLines[0];
const typed = input.value;
// Handle backspace for accuracy calculation
if (typed.length < lastTypedLength) {
// If backspace was pressed, reduce total keystrokes
totalKeystrokes = Math.max(0, totalKeystrokes - (lastTypedLength - typed.length));
} else {
// Only increment total keystrokes for new characters
totalKeystrokes += typed.length - lastTypedLength;
}
lastTypedLength = typed.length;
// Calculate correct keystrokes
let correct = 0;
for (let i = 0; i < typed.length; i++) {
if (i < currentLine.length && typed[i] === currentLine[i]) {
correct++;
}
}
correctKeystrokes = correct;
updateDisplay();
// Check if current line is completed
if (typed.endsWith('\n') && typed.trim() === currentLine) {
completedLines.push(currentLine); // Add to completed lines
// Add the correct keystrokes from this line to total
correctKeystrokes += currentLine.length;
updateTextLines();
input.value = '';
inputDisplay.innerHTML = '';
lastTypedLength = 0;
}
calculateMetrics();
});
input.addEventListener('keydown', (e) => {
if (currentTest === 'clicks') return;
if (e.key === 'Enter') {
e.preventDefault();
const currentLine = currentTextLines[0];
const typed = input.value;
if (typed === currentLine) {
updateTextLines();
input.value = '';
inputDisplay.innerHTML = '';
}
}
}); navItems.forEach(item => {
item.addEventListener('click', (e) => {
e.preventDefault();
updateMode(item.dataset.test);
navItems.forEach(navItem => {
navItem.classList.toggle('active', navItem === item);
});
});
});
// Initialize
updateMode('clicks');
});