Skip to content

Commit 7b5567e

Browse files
Adds Day 1 Finished Code.
1 parent 45e08db commit 7b5567e

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

01 - JavaScript Drum Kit/index-START.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,30 @@
5858
<audio data-key="76" src="sounds/tink.wav"></audio>
5959

6060
<script>
61+
// There's quite a bit of delay between keydown and when the audio plays,
62+
// but this is functional :P
63+
(function() {
64+
const playingClass = 'playing';
6165

66+
function playSound (e) {
67+
const audio = document.querySelector(`audio[data-key='${e.keyCode}']`);
68+
const key = document.querySelector(`.key[data-key='${e.keyCode}']`);
69+
if (!audio) return;
70+
audio.currentTime = 0; // rewind the audio to the start
71+
audio.play();
72+
key.classList.add(playingClass);
73+
}
74+
75+
function removeTransition (e) {
76+
if (e.propertyName !== 'transform') return;
77+
this.classList.remove(playingClass);
78+
}
79+
80+
const keys = document.querySelectorAll('.key');
81+
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
82+
83+
window.addEventListener('keydown', playSound);
84+
})();
6285
</script>
6386

6487

0 commit comments

Comments
 (0)