-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
590 lines (553 loc) · 23.4 KB
/
index.html
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
<!doctype html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Flashcards | Simple flashcard web app to practice letters, numbers and sight words.</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="description" content="Simple webapp to practice letters, numbers and sight words.">
<meta name=”robots” content="index, follow">
<link rel="shortcut icon" href="icons/favicon.ico">
<link rel="manifest" href="manifest.webmanifest">
<style>
:root {
--border-radius: 4px;
--color-primary: rgb(13, 110, 253);
--content-width: 350px;
}
* {
box-sizing: border-box;
}
body {
background-color: #111;
color: #000;
font-family: Helvetica, Verdana, Tahoma, sans-serif;
margin: 0;
padding: 20px 0;
text-align: center;
text-rendering: optimizeLegibility;
touch-action: pan-x pan-y;
}
main {
background: #fff;
border: 1px solid #aaa;
border-radius: 20px;
margin: 0 auto;
position: relative;
width: var(--content-width);
}
button {
background: none;
border: none;
cursor: pointer;
margin: 0;
padding: 0;
}
dt, dd {
display: inline;
line-height: 1.2em;
margin: 0;
padding: 0;
vertical-align: middle;
}
ul {
padding-left: 20px;
}
p {
margin-top: 0;
}
/** nav styles */
.nav {
align-items: center;
display: flex;
margin: 0 auto 20px;
justify-content: space-evenly;
width: var(--content-width);
}
/* help button */
.nav > button:focus svg,
.nav > button:hover svg {
fill: var(--color-primary);
}
/* buttons */
nav {
display: flex;
justify-content: center;
text-transform: uppercase;
}
nav button {
background: #f8f9fa;
border: 1px solid #d8d7d7;
color: #000;
cursor: pointer;
display: inline-block;
font-size: 1rem;
font-weight: 400;
line-height: 1.42857143;
padding: 6px 12px;
text-align:center;
vertical-align: middle;
}
nav.letters button[data-type=letters],
nav.numbers button[data-type=numbers],
nav.sight button[data-type=sight],
nav button:focus,
nav button:hover {
background-color: var(--color-primary);
border-color: var(--color-primary);
color: #fff;
}
nav button:first-child {
border-bottom-left-radius: var(--border-radius);
border-right: 0;
border-top-left-radius: var(--border-radius);
}
nav button:last-child {
border-bottom-right-radius: var(--border-radius);
border-left: 0;
border-top-right-radius: var(--border-radius);
}
/** card container */
section {
align-items: center;
cursor: pointer;
display: flex;
font-size: 10em;
justify-content: center;
height: 400px;
touch-action: manipulation;
user-select: none;
}
h1.sight {
font-size: .4em;
width: 100%;
}
/** progress bar styles **/
progress {
background: #fff;
border: 0;
top: 20px;
color: var(--color-primary);
height: 5px;
left: 0;
padding: 0 10px;
position: absolute;
width: 100%;
}
progress::-webkit-progress-bar {
background: #fff;
}
progress::-moz-progress-bar {
background: var(--color-primary);
}
progress::-webkit-progress-value {
background: var(--color-primary);
}
/** controls */
fieldset {
align-items: center;
border: none;
display: flex;
justify-content: space-evenly;
margin: 0;
padding: 0;
position: relative;
}
fieldset .controls {
position: relative;
top: -15px;
}
input {
cursor: pointer;
font-size: 24px;
height: 80px;
position: relative;
transform: rotate(180deg);
width: 20px;
-webkit-appearance: slider-vertical;
}
select {
border-radius: 10px;
font-size: 1em;
display: block;
margin: 1em auto;
padding: 5px;
}
label {
display: inline-block;
font-size: 4.5em;
position: relative;
top: -15px;
transform: scaleX(-1);
}
fieldset button svg:hover {
fill: red;
}
button#play svg {
display:none;
}
button#play.play .play {
display:block;
}
button#play.pause .pause {
display:block;
}
/** help */
#help {
background: #fff;
border-radius: 20px;
bottom: -1px;
color: #333;
display: none;
left: -1px;
line-height: 1.2em;
padding: 20px;
position: absolute;
right: -1px;
text-align: left;
top: -1px;
width: var(--content-width);
}
#help.show {
display: block;
}
.close {
font-size: 1em;
font-weight: bold;
position: absolute;
right: 1em;
top: 1em;
}
</style>
<script async src="https://www.googletagmanager.com/gtag/js?id=G-SW0KSFR1MN"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-SW0KSFR1MN');
</script>
</head>
<body>
<div class="nav">
<nav class="letters">
<button data-type="letters">Letters</button>
<button data-type="numbers">Numbers</button>
<button data-type="sight">Sight Words</button>
</nav>
<button id="help-btn">
<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="#fff"><path d="M0 0h24v24H0V0z" fill="none"/><path d="M11 7h2v2h-2zm0 4h2v6h-2zm1-9C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8z"/></svg>
</button>
</div>
<main>
<progress id="progress" value="0" max="26">0%</progress>
<section id="container" accesskey="space">
<h1 class="letter">A</h1>
</section>
<fieldset>
<span class="controls">
<button id="play" class="play" title="Play" accesskey="s">
<svg class="play" xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" height="50px" viewBox="0 0 24 24" width="50px" fill="#000000"><g><rect fill="none" height="24" width="24"/></g><g><path d="M12,2C6.48,2,2,6.48,2,12s4.48,10,10,10s10-4.48,10-10S17.52,2,12,2z M12,20c-4.41,0-8-3.59-8-8s3.59-8,8-8s8,3.59,8,8 S16.41,20,12,20z M9.5,16.5l7-4.5l-7-4.5V16.5z"/></g></svg>
<svg class="pause" xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" height="50px" viewBox="0 0 24 24" width="50px" fill="#000000"><g><rect fill="none" height="24" width="24"/></g><g><g><path d="M9,16h2V8H9V16z M12,2C6.48,2,2,6.48,2,12s4.48,10,10,10s10-4.48,10-10S17.52,2,12,2z M12,20c-4.41,0-8-3.59-8-8 s3.59-8,8-8s8,3.59,8,8S16.41,20,12,20z M13,16h2V8h-2V16z"/></g></g></svg>
</button>
<button id="restart" title="Restart" accesskey="r">
<svg xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" height="55px" viewBox="0 0 24 24" width="55px" fill="#000000"><g><path d="M0,0h24v24H0V0z" fill="none"/></g><g><g><path d="M6,13c0-1.65,0.67-3.15,1.76-4.24L6.34,7.34C4.9,8.79,4,10.79,4,13c0,4.08,3.05,7.44,7,7.93v-2.02 C8.17,18.43,6,15.97,6,13z M20,13c0-4.42-3.58-8-8-8c-0.06,0-0.12,0.01-0.18,0.01l1.09-1.09L11.5,2.5L8,6l3.5,3.5l1.41-1.41 l-1.08-1.08C11.89,7.01,11.95,7,12,7c3.31,0,6,2.69,6,6c0,2.97-2.17,5.43-5,5.91v2.02C16.95,20.44,20,17.08,20,13z"/></g></g></svg>
</button>
<button id="voice" title="Speak card" accesskey="v">
<svg xmlns="http://www.w3.org/2000/svg" height="55px" viewBox="0 0 24 24" width="55px" fill="#000000"><path d="M0 0h24v24H0V0z" fill="none"/><path d="M3 9v6h4l5 5V4L7 9H3zm7-.17v6.34L7.83 13H5v-2h2.83L10 8.83zM16.5 12c0-1.77-1.02-3.29-2.5-4.03v8.05c1.48-.73 2.5-2.25 2.5-4.02zM14 3.23v2.06c2.89.86 5 3.54 5 6.71s-2.11 5.85-5 6.71v2.06c4.01-.91 7-4.49 7-8.77 0-4.28-2.99-7.86-7-8.77z"/></svg>
</button>
</span>
<span>
<input type="range" min="500" max="2500" value="1500" step="500" id="speed" title="Adjust speed">
<label for="speed" id="label">🐺</label>
</span>
</fieldset>
<div id="help" aria-modal="true" role="dialog">
<p>Choose the flashcard type via the navigation above. Click, tap or swipe the card to view the next card.</p>
<h4>Controls</h4>
<dl></dl>
<div>
<dt>
<svg class="play" xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"><g><rect fill="none" height="24" width="24"/></g><g><path d="M12,2C6.48,2,2,6.48,2,12s4.48,10,10,10s10-4.48,10-10S17.52,2,12,2z M12,20c-4.41,0-8-3.59-8-8s3.59-8,8-8s8,3.59,8,8 S16.41,20,12,20z M9.5,16.5l7-4.5l-7-4.5V16.5z"/></g></svg>
</dt>
<dd>Auto change the card at a given interval. You can use the slider to change the speed at which the card changes.</dd>
</div>
<div>
<dt>
<svg xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" height="26px" viewBox="0 0 24 24" width="26px" fill="#000000"><g><path d="M0,0h24v24H0V0z" fill="none"/></g><g><g><path d="M6,13c0-1.65,0.67-3.15,1.76-4.24L6.34,7.34C4.9,8.79,4,10.79,4,13c0,4.08,3.05,7.44,7,7.93v-2.02 C8.17,18.43,6,15.97,6,13z M20,13c0-4.42-3.58-8-8-8c-0.06,0-0.12,0.01-0.18,0.01l1.09-1.09L11.5,2.5L8,6l3.5,3.5l1.41-1.41 l-1.08-1.08C11.89,7.01,11.95,7,12,7c3.31,0,6,2.69,6,6c0,2.97-2.17,5.43-5,5.91v2.02C16.95,20.44,20,17.08,20,13z"/></g></g></svg>
</dt>
<dd>Restart the flashcards from the beginning with a new random order.</dd>
</div>
<div>
<dt>
<svg xmlns="http://www.w3.org/2000/svg" height="26px" viewBox="0 0 24 24" width="26px" fill="#000000"><path d="M0 0h24v24H0V0z" fill="none"/><path d="M3 9v6h4l5 5V4L7 9H3zm7-.17v6.34L7.83 13H5v-2h2.83L10 8.83zM16.5 12c0-1.77-1.02-3.29-2.5-4.03v8.05c1.48-.73 2.5-2.25 2.5-4.02zM14 3.23v2.06c2.89.86 5 3.54 5 6.71s-2.11 5.85-5 6.71v2.06c4.01-.91 7-4.49 7-8.77 0-4.28-2.99-7.86-7-8.77z"/></svg>
</dt>
<dd>Says the current card out loud.</dd>
</div>
</dl>
<h4>Keyboard Shortcuts</h4>
<p>On a desktop computer, you can use the following keys to navigate the app:</p>
<ul>
<li>
<kbd>spacebar</kbd> - Change cards
</li>
<li>
<kbd>s</kbd> - Autoplay the cards
</li>
<li>
<kbd>r</kbd> - Restart the flashcards
</li>
<li>
<kbd>v</kbd> - Say the currently displayed card
</li>
</ul>
<button id="help-close" class="close" aria-label="Close">
<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"><path d="M0 0h24v24H0V0z" fill="none" opacity=".87"/><path d="M12 2C6.47 2 2 6.47 2 12s4.47 10 10 10 10-4.47 10-10S17.53 2 12 2zm0 18c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8zm3.59-13L12 10.59 8.41 7 7 8.41 10.59 12 7 15.59 8.41 17 12 13.41 15.59 17 17 15.59 13.41 12 17 8.41z"/></svg>
</button>
</div>
</main>
</body>
<script>
(async function() {
// elements
const containerEl = document.getElementById('container');
const helpEl = document.getElementById('help');
const labelEl = document.getElementById('label');
const navEl = document.getElementsByTagName('nav')[0];
const playEl = document.getElementById('play');
const progressEl = document.getElementById('progress');
const speedEl = document.getElementById('speed');
const data = {
letters: {
data: [
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o','p','q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
],
run: () => {
if (progressCount === 26) {
return;
}
progressCount++;
const randomLetter = getRandomItem(data.letters.data);
const randomCase = getRandomItem([String.prototype.toUpperCase, String.prototype.toLowerCase]);
return randomCase.call(randomLetter);
}
},
numbers: {
data: Array(20),
run: () => {
const total = data.numbers.data.length;
if (progressCount === total) {
return;
}
progressCount++;
return (Math.round(Math.random() * (total - 1) + 1));
}
},
sight: {
data: [
'a','about','all','am','an','and','are','as','ask','asks','at','at','away',
'be','because','big','but','by',
'can','come','could',
'do','down',
'eat',
'find','for','from',
'gets','give','go','goes','good',
'had','has','have','he','help','helps','her','here','him','his','how',
'i','in,','into','is','it',
'lay','let\'s','like','little','live','look',
'make','me','my',
'no','not','now',
'of','off','on','one','our','our','over',
'play','put','puts',
'said','saw','say','says','see','she','should','so','some','something',
'take','than','that','the','them','then','there','this','to','too',
'under','up','us',
'very',
'want','was','we','went','were','what','when','where','why','will','with','works','works','would',
'you','your'
],
run: () => {
const total = data.sight.data.length;
if (progressCount === total) {
return;
}
progressCount++;
return getRandomItem(data.sight.data);
}
}
};
// vars
let currentType = 'letters';
let interval;
let isPlaying = false;
let progressCount = 0;
let speed = 1500;
const doneEmojis = ['⭐️', '🌟', '🏁', '♥️', '✔️', '💯', '🚀'];
// methods
const changeSpeed = () => {
speed = Number(speedEl.value);
updateLabel(speed);
if (interval) {
clearInterval(interval);
}
interval = setInterval(updateCard, speed);
};
const getRandomItem = (arr) => arr[Math.floor(Math.random() * arr.length)];
const showHelp = () => {
helpEl.classList.toggle('show');
helpEl.focus();
}
const restart = () => {
updateCard();
progressCount = 0;
progressEl.setAttribute('value', progressCount);
};
const sayCard = () => {
const currentItemEl = document.getElementsByTagName('h1')[0];
const utterThis = new SpeechSynthesisUtterance(currentItemEl.innerText);
window.speechSynthesis.speak(utterThis);
};
const start = () => {
changeSpeed();
setPlay();
}
const setPlay = () => {
playEl.className = 'pause';
isPlaying = true;
};
const setPause = () => {
clearInterval(interval);
playEl.className = 'play';
isPlaying = false;
};
const togglePlay = () => {
if (!isPlaying) {
start();
} else {
setPause();
}
};
const updateCard = () => {
const text = data[currentType].run();
// all done
if (!text) {
containerEl.innerHTML = getRandomItem(doneEmojis);
setPause();
return;
}
// update card
containerEl.innerHTML = `<h1 class="${currentType}">${text}</h1>`;
// update progress
progressEl.setAttribute('max', data[currentType].data.length);
progressEl.setAttribute('value', progressCount);
const percent = Math.round((progressCount / data[currentType].data.length) * 100);
progressEl.innerText = `${percent}%`;
};
const updateLabel = (speed) => {
let text;
switch (speed) {
case 500: text = '🐆'; break;
case 1000: text = '🐅'; break;
case 1500: text = '🐺'; break;
case 2000: text = '🐘'; break;
case 2500: text = '🐢'; break;
}
labelEl.innerHTML = text;
};
// attach delegate listener on the document
document.addEventListener('click', (e) => {
let eventCategory = currentType;
let eventAction;
let eventLabel;
// nav
if (e.target.getAttribute('data-type')) {
currentType = e.target.getAttribute('data-type');
navEl.className = currentType;
restart();
eventCategory = 'navigate';
eventAction = currentType;
}
// container
if (e.target.closest('#container')) {
updateCard();
eventAction = 'click';
}
// help button
if (e.target.closest('#help-btn')) {
showHelp();
eventCategory = 'help';
eventAction = helpEl.classList.contains('show') ? 'show' : 'hide';
}
// close button
if (e.target.closest('#help-close')) {
helpEl.classList.toggle('show');
eventCategory = 'help';
eventAction = 'close';
}
if (e.target.closest('#play')) {
togglePlay();
eventAction = isPlaying ? 'play' : 'pause';
eventLabel = progressCount;
};
if (e.target.closest('#restart')) {
restart();
eventAction = 'restart';
eventLabel = progressCount;
}
if (e.target.closest('#speed')) {
start();
eventAction = 'speed';
eventLabel = e.target.value;
}
if (e.target.closest('#voice')) {
sayCard();
eventAction = 'say';
}
if (eventAction) {
gtag('event', eventCategory, {
action: eventAction,
label: eventLabel
});
}
});
// touch handling
let drag = {};
containerEl.addEventListener('touchstart', (e) => {
const { clientX } = e.changedTouches[0];
drag.startX = clientX;
}, true);
containerEl.addEventListener('touchmove', (e) => {
const { clientX } = e.changedTouches[0];
// swiped left then change card
if (clientX < drag.startX) {
// prevent page scroll
document.body.style.overflow = 'hidden';
updateCard();
gtag('event', currentType, { action: 'swipe' });
}
drag = {};
}, true);
containerEl.addEventListener('touchend', (e) => {
document.body.style.overflow = 'scroll';
drag = {};
}, true);
// attach keyboard events
document.addEventListener('keyup', (e) => {
// log event if keyCode matches
const codes = [27, 32, 82, 83, 86, 191];
codes.includes(e.keyCode) && gtag('event', currentType, { action: 'keyboard', label: e.code });
switch (e.keyCode) {
case 27: showHelp(); break; // close help
case 32: updateCard(); break; // spacebar
case 82: restart(); break; // r
case 83: togglePlay(); break; // s
case 86: sayCard(); break; // v
case 191: (e.shiftKey && showHelp()); break; // ?
}
});
// add service worker for the web manifest
try {
const reg = await navigator.serviceWorker.register('/flashcards/service-worker.js');
} catch(e) {}
}());
</script>
</html>