-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
67 lines (56 loc) · 1.77 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
'use strict';
const soundsRoot = 'assets/sounds/';
const drumSounds = [
{ key: 'q', sound: 'clap.wav' },
{ key: 'w', sound: 'crash.wav' },
{ key: 'e', sound: 'hihat.wav' },
{ key: 'a', sound: 'kick.wav' },
{ key: 's', sound: 'openhat.wav' },
{ key: 'd', sound: 'ride.wav' },
{ key: 'z', sound: 'shaker.wav' },
{ key: 'x', sound: 'snare.wav' },
{ key: 'c', sound: 'tom.wav' },
];
const $keysContainer = document.querySelector('.keys');
const $keys = Array.from(document.querySelectorAll('.key'));
const getAudioDOM = (index) => {
const audioDOM = document.createElement('audio');
audioDOM.src = soundsRoot + drumSounds[index].sound;
audioDOM.dataset.key = drumSounds[index].key;
return audioDOM;
};
const playSound = (key) => {
const $audio = document.querySelector(`audio[data-key="${key}"]`);
const $keyDOM = document.querySelector(`div[data-key="${key}"]`);
if (!$audio && !$keyDOM) return;
$keyDOM.classList.add('playing');
$audio.currentTime = 0;
$audio.play();
};
const onKeyDown = (event) => {
const key = event.key;
const setKeys = drumSounds.filter((drum) => drum.key === event.key);
if (setKeys.length === 0) return;
playSound(key);
};
const onTransitionEnd = (event) => {
if (event.propertyName === 'transform') {
event.target.classList.remove('playing');
}
};
const init = () => {
window.addEventListener('keydown', onKeyDown);
$keys.forEach((key, index) => {
const audio = getAudioDOM(index);
key.appendChild(audio);
key.dataset.key = drumSounds[index].key;
key.addEventListener('transitionend', onTransitionEnd);
});
};
$keysContainer.addEventListener('click', (event) => {
const target = event.target;
if (!target.matches('.key')) return;
const key = target.dataset.key;
playSound(key);
});
init();