Skip to content

Commit 642f522

Browse files
committed
drum box example added
1 parent 4476805 commit 642f522

11 files changed

Lines changed: 130 additions & 0 deletions

File tree

js/drum-box/index.html

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>JS Drum Kit</title>
6+
<link rel="stylesheet" href="style.css">
7+
</head>
8+
<body>
9+
10+
<div class="keys">
11+
<div data-key="65" class="key">
12+
<kbd>A</kbd>
13+
<span class="sound">clap</span>
14+
</div>
15+
<div data-key="83" class="key">
16+
<kbd>S</kbd>
17+
<span class="sound">hihat</span>
18+
</div>
19+
<div data-key="68" class="key">
20+
<kbd>D</kbd>
21+
<span class="sound">kick</span>
22+
</div>
23+
<div data-key="70" class="key">
24+
<kbd>F</kbd>
25+
<span class="sound">openhat</span>
26+
</div>
27+
<div data-key="71" class="key">
28+
<kbd>G</kbd>
29+
<span class="sound">boom</span>
30+
</div>
31+
<div data-key="72" class="key">
32+
<kbd>H</kbd>
33+
<span class="sound">ride</span>
34+
</div>
35+
<div data-key="74" class="key">
36+
<kbd>J</kbd>
37+
<span class="sound">snare</span>
38+
</div>
39+
<div data-key="75" class="key">
40+
<kbd>K</kbd>
41+
<span class="sound">tom</span>
42+
</div>
43+
<div data-key="76" class="key">
44+
<kbd>L</kbd>
45+
<span class="sound">tink</span>
46+
</div>
47+
</div>
48+
49+
<audio data-key="65" src="sounds/clap.wav"></audio>
50+
<audio data-key="83" src="sounds/hihat.wav"></audio>
51+
<audio data-key="68" src="sounds/kick.wav"></audio>
52+
<audio data-key="70" src="sounds/openhat.wav"></audio>
53+
<audio data-key="71" src="sounds/boom.wav"></audio>
54+
<audio data-key="72" src="sounds/ride.wav"></audio>
55+
<audio data-key="74" src="sounds/snare.wav"></audio>
56+
<audio data-key="75" src="sounds/tom.wav"></audio>
57+
<audio data-key="76" src="sounds/tink.wav"></audio>
58+
59+
<script>
60+
function removeTransition(e) {
61+
if (e.propertyName !== 'transform') return;
62+
e.target.classList.remove('playing');
63+
}
64+
65+
function playSound(e) {
66+
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
67+
const key = document.querySelector(`div[data-key="${e.keyCode}"]`);
68+
if (!audio) return;
69+
70+
key.classList.add('playing');
71+
audio.currentTime = 0;
72+
audio.play();
73+
}
74+
75+
const keys = Array.from(document.querySelectorAll('.key'));
76+
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
77+
window.addEventListener('keydown', playSound);
78+
</script>
79+
</body>
80+
</html>

js/drum-box/sounds/boom.wav

130 KB
Binary file not shown.

js/drum-box/sounds/clap.wav

63.4 KB
Binary file not shown.

js/drum-box/sounds/hihat.wav

50.9 KB
Binary file not shown.

js/drum-box/sounds/kick.wav

15.2 KB
Binary file not shown.

js/drum-box/sounds/openhat.wav

238 KB
Binary file not shown.

js/drum-box/sounds/ride.wav

429 KB
Binary file not shown.

js/drum-box/sounds/snare.wav

32.6 KB
Binary file not shown.

js/drum-box/sounds/tink.wav

5.32 KB
Binary file not shown.

js/drum-box/sounds/tom.wav

105 KB
Binary file not shown.

0 commit comments

Comments
 (0)