-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1115 lines (975 loc) · 32.3 KB
/
index.html
File metadata and controls
1115 lines (975 loc) · 32.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
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
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta property="og:image" content="https://corvid-agent.github.io/typing-test/docs/preview.png">
<meta property="og:image:width" content="1280">
<meta property="og:image:height" content="640">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Typing Speed Test</title>
<link rel="icon" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><text y='.9em' font-size='90'>⌨️</text></svg>">
<meta name="description" content="Retro pixel-style typing speed test with real-time WPM, accuracy tracking, and personal bests.">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap" rel="stylesheet">
<link href="https://fontlibrary.org/face/dogica" rel="stylesheet" type="text/css">
<style>
*, *::before, *::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
:root {
--bg: #08080f;
--card: #12121a;
--text: #e8e8f0;
--accent: #8b5cf6;
--secondary: #06b6d4;
--correct: #4ade80;
--error: #f87171;
--dim: #6b6b80;
--card-border: #1e1e2e;
--font-pixel: 'DogicaPixelRegular', 'Press Start 2P', monospace;
}
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}
html {
font-size: 16px;
scroll-behavior: smooth;
}
body {
font-family: var(--font-pixel);
background-color: var(--bg);
color: var(--text);
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
padding: 1rem;
line-height: 1.6;
font-size: 0.875rem;
}
a {
color: var(--secondary);
text-decoration: underline;
}
a:focus-visible, button:focus-visible, input:focus-visible {
outline: 2px solid var(--accent);
outline-offset: 2px;
}
/* --- Header --- */
.header {
text-align: center;
margin-bottom: 1.5rem;
width: 100%;
max-width: 52rem;
}
.header h1 {
font-size: clamp(1rem, 3vw, 1.5rem);
color: var(--accent);
letter-spacing: 0.1em;
margin-bottom: 0.75rem;
text-transform: lowercase;
}
.header h1 .cursor-blink {
display: inline-block;
width: 0.6em;
background: var(--secondary);
animation: blink 1s step-end infinite;
margin-left: 0.15em;
}
@keyframes blink {
0%, 100% { opacity: 1; }
50% { opacity: 0; }
}
/* --- Mode selector --- */
.mode-selector {
display: flex;
justify-content: center;
gap: 0.5rem;
flex-wrap: wrap;
}
.mode-btn {
font-family: var(--font-pixel);
font-size: 0.75rem;
padding: 0.5rem 1rem;
background: var(--card);
color: var(--dim);
border: 2px solid var(--card-border);
cursor: pointer;
transition: all 0.15s ease;
}
.mode-btn:hover {
border-color: var(--accent);
color: var(--text);
}
.mode-btn.active {
background: var(--accent);
color: #fff;
border-color: var(--accent);
}
/* --- Stats bar --- */
.stats-bar {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 0.75rem;
width: 100%;
max-width: 52rem;
margin-bottom: 1.5rem;
}
.stat-card {
background: var(--card);
border: 1px solid var(--card-border);
padding: 0.75rem;
text-align: center;
}
.stat-card .stat-label {
font-size: 0.625rem;
color: var(--dim);
text-transform: uppercase;
margin-bottom: 0.25rem;
letter-spacing: 0.05em;
}
.stat-card .stat-value {
font-size: clamp(1rem, 3vw, 1.5rem);
color: var(--secondary);
font-weight: bold;
}
.stat-card .stat-value.wpm-value {
color: var(--accent);
}
/* --- Text display area --- */
.text-area-wrapper {
width: 100%;
max-width: 52rem;
margin-bottom: 1.5rem;
position: relative;
}
.text-display {
background: var(--card);
border: 2px solid var(--card-border);
padding: 1.5rem;
font-size: clamp(0.875rem, 2vw, 1.125rem);
line-height: 2;
min-height: 8rem;
cursor: text;
user-select: none;
position: relative;
overflow-wrap: break-word;
word-wrap: break-word;
}
.text-display .word {
display: inline-block;
white-space: nowrap;
}
.text-display:focus-within {
border-color: var(--accent);
}
.text-display .char {
position: relative;
transition: color 0.05s;
}
.text-display .char.dim {
color: var(--dim);
}
.text-display .char.correct {
color: var(--correct);
}
.text-display .char.incorrect {
color: #fff;
background: var(--error);
border-radius: 2px;
}
.text-display .char.current {
border-bottom: 2px solid var(--accent);
animation: cursor-pulse 0.8s ease-in-out infinite;
}
@keyframes cursor-pulse {
0%, 100% { border-bottom-color: var(--accent); }
50% { border-bottom-color: transparent; }
}
.click-hint {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: var(--dim);
font-size: 0.75rem;
pointer-events: none;
text-align: center;
}
.click-hint.hidden {
display: none;
}
/* Hidden input for capturing keystrokes */
.hidden-input {
position: absolute;
opacity: 0;
width: 1px;
height: 1px;
top: 0;
left: 0;
pointer-events: none;
}
/* --- Controls --- */
.controls {
display: flex;
justify-content: center;
gap: 0.75rem;
margin-bottom: 1.5rem;
flex-wrap: wrap;
width: 100%;
max-width: 52rem;
}
.btn {
font-family: var(--font-pixel);
font-size: 0.75rem;
padding: 0.625rem 1.25rem;
border: 2px solid var(--accent);
background: transparent;
color: var(--accent);
cursor: pointer;
transition: all 0.15s ease;
text-transform: lowercase;
}
.btn:hover {
background: var(--accent);
color: #fff;
}
.btn.primary {
background: var(--accent);
color: #fff;
}
.btn.primary:hover {
background: #7c3aed;
}
/* --- Virtual Keyboard --- */
.keyboard {
width: 100%;
max-width: 52rem;
margin-bottom: 1.5rem;
}
.keyboard-row {
display: flex;
justify-content: center;
gap: 3px;
margin-bottom: 3px;
}
.key {
font-family: var(--font-pixel);
font-size: 0.5rem;
width: clamp(1.5rem, 4vw, 2.5rem);
height: clamp(1.5rem, 4vw, 2.5rem);
display: flex;
align-items: center;
justify-content: center;
background: var(--card);
border: 1px solid var(--card-border);
color: var(--dim);
transition: all 0.08s ease;
text-transform: uppercase;
flex-shrink: 0;
}
.key.space {
width: clamp(8rem, 25vw, 16rem);
}
.key.wide {
width: clamp(2.5rem, 6vw, 4rem);
font-size: 0.4rem;
}
.key.pressed {
background: var(--accent);
color: #fff;
border-color: var(--accent);
transform: translateY(1px);
}
.key.correct-key {
background: var(--correct);
color: #000;
border-color: var(--correct);
}
.key.incorrect-key {
background: var(--error);
color: #fff;
border-color: var(--error);
}
/* --- Personal Bests --- */
.personal-bests {
width: 100%;
max-width: 52rem;
margin-bottom: 1.5rem;
}
.personal-bests h2 {
font-size: 0.875rem;
color: var(--accent);
margin-bottom: 0.75rem;
text-transform: lowercase;
}
.bests-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr));
gap: 0.75rem;
}
.best-card {
background: var(--card);
border: 1px solid var(--card-border);
padding: 1rem;
}
.best-card h3 {
font-size: 0.75rem;
color: var(--secondary);
margin-bottom: 0.5rem;
}
.best-card .best-list {
list-style: none;
font-size: 0.625rem;
color: var(--dim);
}
.best-card .best-list li {
padding: 0.25rem 0;
border-bottom: 1px solid var(--card-border);
}
.best-card .best-list li:last-child {
border-bottom: none;
}
.best-card .best-list li .best-wpm {
color: var(--accent);
font-weight: bold;
}
.best-card .best-list li .best-acc {
color: var(--correct);
}
.best-card .no-scores {
font-size: 0.625rem;
color: var(--dim);
font-style: italic;
}
/* --- Results overlay --- */
.results-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(8, 8, 15, 0.92);
display: flex;
align-items: center;
justify-content: center;
z-index: 100;
padding: 1rem;
}
.results-overlay.hidden {
display: none;
}
.results-card {
background: var(--card);
border: 2px solid var(--accent);
padding: 2rem;
max-width: 28rem;
width: 100%;
text-align: center;
}
.results-card h2 {
font-size: 1rem;
color: var(--accent);
margin-bottom: 1.5rem;
text-transform: lowercase;
}
.results-stats {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 1rem;
margin-bottom: 1.5rem;
}
.results-stats .result-item {
padding: 0.75rem;
background: var(--bg);
border: 1px solid var(--card-border);
}
.results-stats .result-label {
font-size: 0.625rem;
color: var(--dim);
text-transform: uppercase;
margin-bottom: 0.25rem;
}
.results-stats .result-value {
font-size: 1.25rem;
color: var(--secondary);
}
.results-stats .result-value.highlight {
color: var(--accent);
}
.results-new-best {
color: var(--correct);
font-size: 0.75rem;
margin-bottom: 1rem;
animation: pulse-glow 1.5s ease-in-out infinite;
}
@keyframes pulse-glow {
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
}
.results-new-best.hidden {
display: none;
}
.results-actions {
display: flex;
gap: 0.75rem;
justify-content: center;
flex-wrap: wrap;
}
/* --- Footer --- */
.footer {
margin-top: auto;
padding-top: 2rem;
text-align: center;
font-size: 0.625rem;
color: var(--dim);
}
/* --- Responsive --- */
@media (max-width: 600px) {
.stats-bar {
grid-template-columns: repeat(2, 1fr);
}
.text-display {
padding: 1rem;
font-size: 0.875rem;
}
.results-stats {
grid-template-columns: 1fr;
}
.key {
font-size: 0.4rem;
}
}
</style>
</head>
<body>
<!-- Header -->
<header class="header">
<h1>typing test<span class="cursor-blink" aria-hidden="true"> </span></h1>
<nav class="mode-selector" aria-label="Test duration">
<button class="mode-btn active" data-time="30" aria-pressed="true">30s</button>
<button class="mode-btn" data-time="60" aria-pressed="false">60s</button>
<button class="mode-btn" data-time="120" aria-pressed="false">120s</button>
</nav>
</header>
<!-- Stats bar -->
<section class="stats-bar" aria-label="Live statistics">
<div class="stat-card">
<div class="stat-label">WPM</div>
<div class="stat-value wpm-value" id="stat-wpm">0</div>
</div>
<div class="stat-card">
<div class="stat-label">Accuracy</div>
<div class="stat-value" id="stat-accuracy">100%</div>
</div>
<div class="stat-card">
<div class="stat-label">Time</div>
<div class="stat-value" id="stat-time">30</div>
</div>
<div class="stat-card">
<div class="stat-label">Errors</div>
<div class="stat-value" id="stat-errors">0</div>
</div>
</section>
<!-- Text display area -->
<div class="text-area-wrapper" role="application" aria-label="Typing test area">
<div class="text-display" id="text-display" tabindex="0" aria-live="polite">
<span class="click-hint" id="click-hint">click here or press any key to start</span>
</div>
<input
type="text"
class="hidden-input"
id="hidden-input"
autocomplete="off"
autocorrect="off"
autocapitalize="off"
spellcheck="false"
aria-label="Type the displayed text here"
>
</div>
<!-- Controls -->
<div class="controls">
<button class="btn primary" id="btn-restart">restart</button>
<button class="btn" id="btn-new-text">new text</button>
</div>
<!-- Virtual Keyboard -->
<section class="keyboard" aria-hidden="true" id="virtual-keyboard">
<div class="keyboard-row">
<div class="key" data-key="`">`</div>
<div class="key" data-key="1">1</div>
<div class="key" data-key="2">2</div>
<div class="key" data-key="3">3</div>
<div class="key" data-key="4">4</div>
<div class="key" data-key="5">5</div>
<div class="key" data-key="6">6</div>
<div class="key" data-key="7">7</div>
<div class="key" data-key="8">8</div>
<div class="key" data-key="9">9</div>
<div class="key" data-key="0">0</div>
<div class="key" data-key="-">-</div>
<div class="key" data-key="=">=</div>
</div>
<div class="keyboard-row">
<div class="key" data-key="q">q</div>
<div class="key" data-key="w">w</div>
<div class="key" data-key="e">e</div>
<div class="key" data-key="r">r</div>
<div class="key" data-key="t">t</div>
<div class="key" data-key="y">y</div>
<div class="key" data-key="u">u</div>
<div class="key" data-key="i">i</div>
<div class="key" data-key="o">o</div>
<div class="key" data-key="p">p</div>
<div class="key" data-key="[">[</div>
<div class="key" data-key="]">]</div>
<div class="key" data-key="\">\</div>
</div>
<div class="keyboard-row">
<div class="key" data-key="a">a</div>
<div class="key" data-key="s">s</div>
<div class="key" data-key="d">d</div>
<div class="key" data-key="f">f</div>
<div class="key" data-key="g">g</div>
<div class="key" data-key="h">h</div>
<div class="key" data-key="j">j</div>
<div class="key" data-key="k">k</div>
<div class="key" data-key="l">l</div>
<div class="key" data-key=";">;</div>
<div class="key" data-key="'">'</div>
</div>
<div class="keyboard-row">
<div class="key wide" data-key="shift">shft</div>
<div class="key" data-key="z">z</div>
<div class="key" data-key="x">x</div>
<div class="key" data-key="c">c</div>
<div class="key" data-key="v">v</div>
<div class="key" data-key="b">b</div>
<div class="key" data-key="n">n</div>
<div class="key" data-key="m">m</div>
<div class="key" data-key=",">,</div>
<div class="key" data-key=".">.</div>
<div class="key" data-key="/">/</div>
<div class="key wide" data-key="backspace">bksp</div>
</div>
<div class="keyboard-row">
<div class="key space" data-key=" ">space</div>
</div>
</section>
<!-- Personal Bests -->
<section class="personal-bests" aria-label="Personal best scores">
<h2>personal bests</h2>
<div class="bests-grid" id="bests-grid"></div>
</section>
<!-- Results overlay -->
<div class="results-overlay hidden" id="results-overlay" role="dialog" aria-modal="true" aria-label="Test results">
<div class="results-card">
<h2>test complete</h2>
<div class="results-new-best hidden" id="results-new-best">new personal best!</div>
<div class="results-stats">
<div class="result-item">
<div class="result-label">WPM</div>
<div class="result-value highlight" id="result-wpm">0</div>
</div>
<div class="result-item">
<div class="result-label">Accuracy</div>
<div class="result-value" id="result-accuracy">100%</div>
</div>
<div class="result-item">
<div class="result-label">Characters</div>
<div class="result-value" id="result-chars">0</div>
</div>
<div class="result-item">
<div class="result-label">Errors</div>
<div class="result-value" id="result-errors">0</div>
</div>
</div>
<div class="results-actions">
<button class="btn primary" id="result-try-again">try again</button>
<button class="btn" id="result-new-text">new text</button>
</div>
</div>
</div>
<!-- Footer -->
<footer class="footer">
<p>built with pixels and patience</p>
</footer>
<script>
(function() {
'use strict';
// --- Text excerpts ---
const TEXTS = [
"It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness. It was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair.",
"Call me Ishmael. Some years ago, never mind how long precisely, having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation.",
"It is a truth universally acknowledged, that a single man in possession of a good fortune, must be in want of a wife. However little known the feelings or views of such a man may be on his first entering a neighbourhood, this truth is so well fixed in the minds of the surrounding families, that he is considered the rightful property of some one or other of their daughters.",
"Once upon a midnight dreary, while I pondered, weak and weary, over many a quaint and curious volume of forgotten lore. While I nodded, nearly napping, suddenly there came a tapping, as of some one gently rapping, rapping at my chamber door. Tis some visitor, I muttered, tapping at my chamber door. Only this and nothing more.",
"In the beginning God created the heaven and the earth. And the earth was without form, and void, and darkness was upon the face of the deep. And the Spirit of God moved upon the face of the waters. And God said, Let there be light, and there was light. And God saw the light, that it was good.",
"All happy families are alike; each unhappy family is unhappy in its own way. Everything was in confusion in the Oblonskys house. The wife had discovered that the husband was carrying on an intrigue with a French girl, who had been a governess in their family, and she had announced to her husband that she could not go on living in the same house with him.",
"It was a bright cold day in April, and the clocks were striking thirteen. Winston Smith, his chin nuzzled into his breast in an effort to escape the vile wind, slipped quickly through the glass doors of Victory Mansions, though not quickly enough to prevent a swirl of gritty dust from entering along with him.",
"The sky above the port was the color of television, tuned to a dead channel. It was not the kind of sky you would want to look at for long. The wind was cold, and the streets were empty. Somewhere in the distance, a siren wailed, cutting through the silence like a blade through water.",
"Far out in the uncharted backwaters of the unfashionable end of the western spiral arm of the Galaxy lies a small unregarded yellow sun. Orbiting this at a distance of roughly ninety-two million miles is an utterly insignificant little blue green planet whose ape-descended life forms are so amazingly primitive that they still think digital watches are a pretty neat idea.",
"In a hole in the ground there lived a hobbit. Not a nasty, dirty, wet hole, filled with the ends of worms and an oozy smell, nor yet a dry, bare, sandy hole with nothing in it to sit down on or to eat. It was a hobbit-hole, and that means comfort. It had a perfectly round door like a porthole, painted green.",
"The quick brown fox jumps over the lazy dog. She sells seashells by the seashore. How much wood would a woodchuck chuck if a woodchuck could chuck wood? Peter Piper picked a peck of pickled peppers. A peck of pickled peppers Peter Piper picked. The rain in Spain stays mainly in the plain.",
"To be or not to be, that is the question. Whether it is nobler in the mind to suffer the slings and arrows of outrageous fortune, or to take arms against a sea of troubles and by opposing end them. To die, to sleep, no more, and by a sleep to say we end the heartache and the thousand natural shocks that flesh is heir to.",
"Space, the final frontier. These are the voyages of the starship Enterprise. Its continuing mission, to explore strange new worlds, to seek out new life and new civilizations, to boldly go where no one has gone before. The universe is a vast and mysterious place, full of wonders that we have only begun to understand.",
"In the middle of the journey of our life, I found myself within a dark wood, where the straight way was lost. How hard a thing it is to say what this wild and rough and difficult wood was, which in thought renews my fear. So bitter is it that death is little more. But to treat of the good I found in it, I will tell of the other things I saw there.",
"The old man sat by the fire, watching the flames dance and flicker in the fading light. Outside, the wind howled through the trees, carrying with it the first snow of winter. He pulled his coat tighter and thought about the years gone by, each one a chapter in a story that was nearing its end. But tonight, there was warmth, and that was enough.",
"It was a dark and stormy night. The wind blew in fierce gusts and the rain pattered loudly against the windows. Through the darkness, a single light burned steadily in the window of a small cottage on the hillside. Inside, the fire crackled and popped, casting long shadows that danced across the walls. The clock on the mantel struck midnight, its chimes echoing through the empty rooms.",
"Technology has changed the way we live, work, and communicate with each other. From the invention of the printing press to the rise of the internet, each new advancement has reshaped our world in profound ways. We carry powerful computers in our pockets, connect with people across the globe in an instant, and have access to more information than any generation before us.",
"The art of writing is the art of discovering what you believe. Every sentence is a journey, every paragraph a destination. The best writers are those who can take the ordinary and make it extraordinary, who can find beauty in the mundane and wisdom in the everyday. Writing is not just about putting words on paper, it is about connecting with another human being.",
"Music fills the silence between heartbeats. It speaks in a language older than words, reaching into the depths of the soul where nothing else can go. A single melody can unlock a thousand memories, each note a key to a door long forgotten. Whether played on a grand piano or hummed softly in the dark, music has the power to heal, to inspire, and to transform.",
"The ocean stretched out before them, endless and blue, meeting the sky at a horizon that seemed to promise everything and nothing at the same time. Waves crashed against the shore in a steady rhythm, ancient and unchanging. The salt air filled their lungs as they stood at the edge of the world, feeling small and infinite all at once."
];
// --- State ---
let currentText = '';
let currentIndex = 0;
let errors = 0;
let totalTyped = 0;
let correctTyped = 0;
let testStarted = false;
let testFinished = false;
let timerInterval = null;
let timeLeft = 30;
let selectedTime = 30;
let startTime = null;
let charStates = []; // 'dim' | 'correct' | 'incorrect'
let lastTextIndex = -1;
// --- DOM references ---
const textDisplay = document.getElementById('text-display');
const hiddenInput = document.getElementById('hidden-input');
const clickHint = document.getElementById('click-hint');
const statWpm = document.getElementById('stat-wpm');
const statAccuracy = document.getElementById('stat-accuracy');
const statTime = document.getElementById('stat-time');
const statErrors = document.getElementById('stat-errors');
const btnRestart = document.getElementById('btn-restart');
const btnNewText = document.getElementById('btn-new-text');
const resultsOverlay = document.getElementById('results-overlay');
const resultWpm = document.getElementById('result-wpm');
const resultAccuracy = document.getElementById('result-accuracy');
const resultChars = document.getElementById('result-chars');
const resultErrors = document.getElementById('result-errors');
const resultTryAgain = document.getElementById('result-try-again');
const resultNewText = document.getElementById('result-new-text');
const resultsNewBest = document.getElementById('results-new-best');
const bestsGrid = document.getElementById('bests-grid');
const modeBtns = document.querySelectorAll('.mode-btn');
const allKeys = document.querySelectorAll('.key');
// --- Utility ---
function getRandomText() {
let idx;
do {
idx = Math.floor(Math.random() * TEXTS.length);
} while (idx === lastTextIndex && TEXTS.length > 1);
lastTextIndex = idx;
return TEXTS[idx];
}
function calcWPM() {
if (!startTime) return 0;
const elapsedMin = (Date.now() - startTime) / 60000;
if (elapsedMin < 0.01) return 0;
return Math.round((correctTyped / 5) / elapsedMin);
}
function calcAccuracy() {
if (totalTyped === 0) return 100;
return Math.round((correctTyped / totalTyped) * 100);
}
// --- Render text display ---
function renderText() {
let html = '';
let inWord = false;
for (let i = 0; i < currentText.length; i++) {
let cls = charStates[i] || 'dim';
if (i === currentIndex && !testFinished) {
cls += ' current';
}
if (currentText[i] === ' ') {
if (inWord) {
html += '</span>';
inWord = false;
}
html += '<span class="char ' + cls + '"> </span> ';
} else {
if (!inWord) {
html += '<span class="word">';
inWord = true;
}
html += '<span class="char ' + cls + '">' + escapeHtml(currentText[i]) + '</span>';
}
}
if (inWord) {
html += '</span>';
}
textDisplay.innerHTML = html;
}
function escapeHtml(str) {
const div = document.createElement('div');
div.textContent = str;
return div.innerHTML;
}
// --- Update stats ---
function updateStats() {
statWpm.textContent = calcWPM();
statAccuracy.textContent = calcAccuracy() + '%';
statTime.textContent = timeLeft;
statErrors.textContent = errors;
}
// --- Timer ---
function startTimer() {
startTime = Date.now();
timerInterval = setInterval(function() {
const elapsed = Math.floor((Date.now() - startTime) / 1000);
timeLeft = Math.max(0, selectedTime - elapsed);
updateStats();
if (timeLeft <= 0) {
endTest();
}
}, 200);
}
function stopTimer() {
clearInterval(timerInterval);
timerInterval = null;
}
// --- End test ---
function endTest() {
testFinished = true;
stopTimer();
renderText();
updateStats();
const wpm = calcWPM();
const acc = calcAccuracy();
resultWpm.textContent = wpm;
resultAccuracy.textContent = acc + '%';
resultChars.textContent = totalTyped;
resultErrors.textContent = errors;
const isNewBest = saveScore(wpm, acc);
if (isNewBest) {
resultsNewBest.classList.remove('hidden');
} else {
resultsNewBest.classList.add('hidden');
}
resultsOverlay.classList.remove('hidden');
renderBests();
// Focus the try-again button for accessibility
resultTryAgain.focus();
}
// --- localStorage personal bests ---
function getStorageKey(time) {
return 'typing-test-bests-' + time;
}
function getScores(time) {
try {
const data = localStorage.getItem(getStorageKey(time));
return data ? JSON.parse(data) : [];
} catch (e) {
return [];
}
}
function saveScore(wpm, accuracy) {
const scores = getScores(selectedTime);
const entry = { wpm: wpm, accuracy: accuracy, date: Date.now() };
scores.push(entry);
scores.sort(function(a, b) { return b.wpm - a.wpm; });
const top5 = scores.slice(0, 5);
try {
localStorage.setItem(getStorageKey(selectedTime), JSON.stringify(top5));
} catch (e) {
// storage full or unavailable
}
// Check if this is a new #1
return top5.length > 0 && top5[0].wpm === wpm && top5[0].date === entry.date;
}
function renderBests() {
let html = '';
[30, 60, 120].forEach(function(time) {
const scores = getScores(time);
html += '<div class="best-card">';
html += '<h3>' + time + 's mode</h3>';
if (scores.length === 0) {
html += '<p class="no-scores">no scores yet</p>';
} else {
html += '<ol class="best-list">';
scores.forEach(function(s, i) {
html += '<li>';
html += '<span class="best-wpm">' + s.wpm + ' WPM</span>';
html += ' · ';
html += '<span class="best-acc">' + s.accuracy + '%</span>';
html += '</li>';
});
html += '</ol>';
}
html += '</div>';
});
bestsGrid.innerHTML = html;
}
// --- Init / Reset ---
function initTest(newText) {
testStarted = false;
testFinished = false;
currentIndex = 0;
errors = 0;
totalTyped = 0;
correctTyped = 0;
startTime = null;
timeLeft = selectedTime;
stopTimer();
if (newText) {
currentText = getRandomText();
}
charStates = new Array(currentText.length).fill('dim');
clickHint.classList.remove('hidden');
resultsOverlay.classList.add('hidden');
hiddenInput.value = '';
renderText();
updateStats();
}
function focusInput() {
hiddenInput.focus();
clickHint.classList.add('hidden');
}
// --- Keyboard highlight ---
function highlightKey(key, isCorrect) {
const k = key.toLowerCase();
allKeys.forEach(function(el) {
const dk = el.getAttribute('data-key');
if (dk === k || (k === ' ' && dk === ' ')) {
el.classList.add('pressed');
if (isCorrect === true) {
el.classList.add('correct-key');
} else if (isCorrect === false) {
el.classList.add('incorrect-key');
}
}
});
// Check if shift should highlight
if (key !== key.toLowerCase() && key !== key.toUpperCase()) {
// It's a letter that could be shifted
}
setTimeout(function() {
allKeys.forEach(function(el) {
el.classList.remove('pressed', 'correct-key', 'incorrect-key');
});
}, 150);
}
// --- Handle input ---
function handleKeyDown(e) {
if (testFinished) return;
// Allow tab for accessibility
if (e.key === 'Tab') return;
// Prevent default for most keys
if (e.key !== 'F5' && e.key !== 'F12' && !(e.ctrlKey || e.metaKey)) {
e.preventDefault();
}
if (e.key === 'Escape') {
initTest(false);
return;
}
if (e.key === 'Backspace') {
if (currentIndex > 0) {
currentIndex--;
if (charStates[currentIndex] === 'incorrect') {
errors = Math.max(0, errors - 1);
}
if (charStates[currentIndex] === 'correct') {
correctTyped = Math.max(0, correctTyped - 1);
}