-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
83 lines (78 loc) · 2.6 KB
/
scripts.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
73
74
75
76
77
78
79
80
81
82
83
// The very thing that makes keys go brr
let triggerKey = (key, state) => {
// The line below prevents errors from pushing other keys
if (!key) return;
if (state) {
new Audio(`media/${title.textContent.toLowerCase()}/${key.lastElementChild.textContent.toLowerCase()}.wav`).play();
key.classList.add('played');
} else {
key.classList.remove('played');
}
}
// Keyboard controls
document.addEventListener('keydown', e => {
// The line below prevents repeated sounds when the key is held
if (e.repeat) return;
triggerKey(document.querySelector(`[data-key="${e.code}"]`), 1);
});
document.addEventListener('keyup', e => {
triggerKey(document.querySelector(`[data-key="${e.code}"]`), 0);
});
keys = document.querySelector('.keys').children;
for (key of keys) {
// Mouse controls
key.addEventListener('mousedown', e => {
triggerKey(e.currentTarget, 1);
});
key.addEventListener('mouseup', e => {
triggerKey(e.currentTarget, 0);
})
key.addEventListener('mouseleave', e => {
triggerKey(e.currentTarget, 0);
})
// Touch controls
key.addEventListener('touchstart', e => {
triggerKey(e.currentTarget, 1);
e.preventDefault();
});
key.addEventListener('touchend', e => {
triggerKey(e.currentTarget, 0);
e.preventDefault();
})
// IF YOU MOVE YOUR FINGER OFF IT IS STILL KINDA ACTIVE
key.addEventListener('touchcancel', e => {
triggerKey(e.currentTarget, 0);
e.preventDefault();
})
}
// Dumb easter egg
const soundpacks = new Map([
['Drumkey', ['CLAP', 'HIHAT', 'KICK', 'PERCUSSION', 'RIDE', 'RIMSHOT', 'SHAKER', 'SNAP', 'SNARE']],
['Drunkey', ['CAN', 'CAN', 'CAN', 'CAN', 'CAN', 'CAN', 'CAN', 'CAN', 'CAN']]
]);
function checkTitle(title) {
switch (title) {
case 'Drumkey':
for (let i = 0; i < keys.length; i++) {
keys[i].lastElementChild.textContent = soundpacks.get('Drumkey')[i];
}
return true;
case 'Drunkey':
for (let i = 0; i < keys.length; i++) {
keys[i].lastElementChild.textContent = soundpacks.get('Drunkey')[i];
}
return true;
}
}
title = document.querySelector('header .primary');
title.addEventListener('input', e => {
if (checkTitle(e.target.textContent)) {
e.target.blur();
}
})
// Attempt at preloading audio so that there's no "first delay"
for (const [key, values] of soundpacks.entries()) {
for (value of values) {
audio = new Audio(`media/${key.toLowerCase()}/${value.toLowerCase()}.wav`);
}
}