-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
executable file
·36 lines (29 loc) · 970 Bytes
/
script.js
File metadata and controls
executable file
·36 lines (29 loc) · 970 Bytes
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
window.addEventListener('keydown', e => playSound(e.keyCode));
const keys = document.querySelectorAll('.keys__key');
keys.forEach(key => {
key.addEventListener('transitionend', removeTransition);
key.addEventListener('click', (e) => {
playSound(e.currentTarget.dataset.key);
});
});
function playSound(keyCode) {
const audio = document.querySelector(`audio[data-key="${keyCode}"]`);
const key = document.querySelector(`.keys__key[data-key="${keyCode}"]`);
if (!audio) return;
/**
* Bonus: if hihat starts to play openhat sound will stop
**/
if (+keyCode === 83) {
const openhatAudio = document.querySelector('audio[data-key="70"]');
if (!openhatAudio.paused) {
openhatAudio.pause();
}
}
audio.currentTime = 0; // play from start
audio.play();
key.classList.add('keys__key--playing');
}
function removeTransition(e) {
if (e.propertyName !== 'transform') return;
this.classList.remove('keys__key--playing');
}