Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions 01 - JavaScript Drum Kit/index-FINISHED.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,30 @@
<audio data-key="76" src="sounds/tink.wav"></audio>

<script>
let justPlayed = null;

function removeTransition(e) {
if (e.propertyName !== 'transform') return;
e.target.classList.remove('playing');
}

function playSound(e) {
if (e.keyCode === justPlayed) return; // If same key has just been played without key being released, don't play again

const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
const key = document.querySelector(`div[data-key="${e.keyCode}"]`);
if (!audio) return;

key.classList.add('playing');
audio.currentTime = 0;
audio.play();
justPlayed = e.keyCode; // Set the key that has just been played
}

const keys = Array.from(document.querySelectorAll('.key'));
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
window.addEventListener('keydown', playSound);
window.addEventListener('keyup', () => justPlayed = null); // Allow key to be played again
</script>


Expand Down