-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
72 lines (62 loc) · 2.32 KB
/
script.js
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
function getKey (e) {
var location = e.location;
var selector;
if (location === KeyboardEvent.DOM_KEY_LOCATION_RIGHT) {
selector = ['[data-key="' + e.keyCode + '-R"]']
} else {
var code = e.keyCode || e.which;
selector = [
'[data-key="' + code + '"]',
'[data-char*="' + encodeURIComponent(String.fromCharCode(code)) + '"]'
].join(',');
}
return document.querySelector(selector);
}
function pressKey (char) {
var key = document.querySelector('[data-char*="' + char.toUpperCase() + '"]');
if (!key) {
return console.warn('No key for', char);
}
key.setAttribute('data-pressed', 'on');
setTimeout(function () {
key.removeAttribute('data-pressed');
}, 200);
}
var h1 = document.querySelector('h1');
var originalQueue = h1.innerHTML;
var queue = h1.innerHTML;
function next () {
var c = queue[0];
queue = queue.slice(1);
h1.innerHTML = originalQueue.slice(0, originalQueue.length - queue.length);
pressKey(c);
if (queue.length) {
setTimeout(next, Math.random() * 200 + 50);
}
}
h1.innerHTML = " ";
setTimeout(next, 500);
document.body.addEventListener('keydown', function (e) {
var key = getKey(e);
if (!key) {
return console.warn('No key for', e.keyCode);
}
key.setAttribute('data-pressed', 'on');
});
document.body.addEventListener('keyup', function (e) {
var key = getKey(e);
key && key.removeAttribute('data-pressed');
});
function size () {
var size = keyboard.parentNode.clientWidth / 90;
keyboard.style.fontSize = size + 'px';
console.log(size);
}
var keyboard = document.querySelector('.keyboard');
window.addEventListener('resize', function (e) {
size();
});
size();
//load page with cursor in input field (focus)
$(document).ready(function(){ document.getElementById("text").focus();
});