Drum Kit Using JavaScript

Drum Kit Using JavaScript
Code Snippet:JavaScript Drum Kit
Author: Timothy Robards
Published: January 9, 2024
Last Updated: January 22, 2024
Downloads: 571
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript code creates a virtual drum kit that you can play by pressing keys on your keyboard. When a key is pressed, it triggers a corresponding drum sound. The core functionality of this code is to associate specific keys with drum sounds and play them in response to key presses. It provides an interactive and fun way to make music using your computer’s keyboard.

How to Create Drum Kit Using JavaScript

1. In your HTML file, create the structure for your drum kit. Use the following HTML code, which defines the keys and their corresponding sounds.

<!-- PRESS THE CORRESPONDING KEY ON YOUR KEYBOARD TO PLAY!-->

<div class="center">
<div class="keys">
  <div data-key="65" class="key">
    <kbd>A</kbd>
    <span class="sound">clap</span>             </div>
  <div data-key="83" class="key">
    <kbd>S</kbd>
    <span class="sound">hihat</span>
  </div>
  <div data-key="68" class="key">
    <kbd>D</kbd>
    <span class="sound">kick</span>
  </div>
  </div>
<div class="keys">
  <div data-key="70" class="key">
    <kbd>F</kbd>
    <span class="sound">openhat</span>
  </div>
  <div data-key="71" class="key">
    <kbd>G</kbd>
    <span class="sound">boom</span>
  </div>
  <div data-key="72" class="key">
    <kbd>H</kbd>
    <span class="sound">ride</span>
  </div>
</div>
<div class="keys">
  <div data-key="74" class="key">
    <kbd>J</kbd>
    <span class="sound">snare</span>
  </div>
  <div data-key="75" class="key">
    <kbd>K</kbd>
    <span class="sound">tom</span>
  </div>
  <div data-key="76" class="key">
    <kbd>L</kbd>
    <span class="sound">tink</span>
  </div>
  
</div>
</div>
    
<audio data-key="65" src="https://timothyrobards.github.io/files/js30/sounds/clap.wav"></audio>
<audio data-key="83" src="https://timothyrobards.github.io/files/js30/sounds/hihat.wav"></audio>
<audio data-key="68" src="https://timothyrobards.github.io/files/js30/sounds/kick.wav"></audio>
<audio data-key="70" src="https://timothyrobards.github.io/files/js30/sounds/openhat.wav"></audio>
<audio data-key="71" src="https://timothyrobards.github.io/files/js30/sounds/boom.wav"></audio>
<audio data-key="72" src="https://timothyrobards.github.io/files/js30/sounds/ride.wav"></audio>
<audio data-key="74" src="https://timothyrobards.github.io/files/js30/sounds/snare.wav"></audio>
<audio data-key="75" src="https://timothyrobards.github.io/files/js30/sounds/tom.wav"></audio>
<audio data-key="76" src="https://timothyrobards.github.io/files/js30/sounds/tink.wav"></audio>

2. Now, copy the CSS code to style your drum kit. You can customize the appearance to match your website’s design.

@import url('https://fonts.googleapis.com/css?family=Roboto+Mono');

html {
  background-color: #aa7400 !important;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='400' height='400' viewBox='0 0 200 200'%3E%3Cg fill='none' stroke='%237F3' stroke-width='1' stroke-opacity='0.2'%3E%3Crect x='-40' y='40' width='75' height='75'/%3E%3Crect x='-35' y='45' width='65' height='65'/%3E%3Crect x='-30' y='50' width='55' height='55'/%3E%3Crect x='-25' y='55' width='45' height='45'/%3E%3Crect x='-20' y='60' width='35' height='35'/%3E%3Crect x='-15' y='65' width='25' height='25'/%3E%3Crect x='-10' y='70' width='15' height='15'/%3E%3Crect x='-5' y='75' width='5' height='5'/%3E%3Crect width='35' height='35'/%3E%3Crect x='5' y='5' width='25' height='25'/%3E%3Crect x='10' y='10' width='15' height='15'/%3E%3Crect x='15' y='15' width='5' height='5'/%3E%3Crect x='40' width='75' height='75'/%3E%3Crect x='45' y='5' width='65' height='65'/%3E%3Crect x='50' y='10' width='55' height='55'/%3E%3Crect x='55' y='15' width='45' height='45'/%3E%3Crect x='60' y='20' width='35' height='35'/%3E%3Crect x='65' y='25' width='25' height='25'/%3E%3Crect x='70' y='30' width='15' height='15'/%3E%3Crect x='75' y='35' width='5' height='5'/%3E%3Crect x='40' y='80' width='35' height='35'/%3E%3Crect x='45' y='85' width='25' height='25'/%3E%3Crect x='50' y='90' width='15' height='15'/%3E%3Crect x='55' y='95' width='5' height='5'/%3E%3Crect x='120' y='-40' width='75' height='75'/%3E%3Crect x='125' y='-35' width='65' height='65'/%3E%3Crect x='130' y='-30' width='55' height='55'/%3E%3Crect x='135' y='-25' width='45' height='45'/%3E%3Crect x='140' y='-20' width='35' height='35'/%3E%3Crect x='145' y='-15' width='25' height='25'/%3E%3Crect x='150' y='-10' width='15' height='15'/%3E%3Crect x='155' y='-5' width='5' height='5'/%3E%3Crect x='120' y='40' width='35' height='35'/%3E%3Crect x='125' y='45' width='25' height='25'/%3E%3Crect x='130' y='50' width='15' height='15'/%3E%3Crect x='135' y='55' width='5' height='5'/%3E%3Crect y='120' width='75' height='75'/%3E%3Crect x='5' y='125' width='65' height='65'/%3E%3Crect x='10' y='130' width='55' height='55'/%3E%3Crect x='15' y='135' width='45' height='45'/%3E%3Crect x='20' y='140' width='35' height='35'/%3E%3Crect x='25' y='145' width='25' height='25'/%3E%3Crect x='30' y='150' width='15' height='15'/%3E%3Crect x='35' y='155' width='5' height='5'/%3E%3Crect x='200' y='120' width='75' height='75'/%3E%3Crect x='40' y='200' width='75' height='75'/%3E%3Crect x='80' y='80' width='75' height='75'/%3E%3Crect x='85' y='85' width='65' height='65'/%3E%3Crect x='90' y='90' width='55' height='55'/%3E%3Crect x='95' y='95' width='45' height='45'/%3E%3Crect x='100' y='100' width='35' height='35'/%3E%3Crect x='105' y='105' width='25' height='25'/%3E%3Crect x='110' y='110' width='15' height='15'/%3E%3Crect x='115' y='115' width='5' height='5'/%3E%3Crect x='80' y='160' width='35' height='35'/%3E%3Crect x='85' y='165' width='25' height='25'/%3E%3Crect x='90' y='170' width='15' height='15'/%3E%3Crect x='95' y='175' width='5' height='5'/%3E%3Crect x='120' y='160' width='75' height='75'/%3E%3Crect x='125' y='165' width='65' height='65'/%3E%3Crect x='130' y='170' width='55' height='55'/%3E%3Crect x='135' y='175' width='45' height='45'/%3E%3Crect x='140' y='180' width='35' height='35'/%3E%3Crect x='145' y='185' width='25' height='25'/%3E%3Crect x='150' y='190' width='15' height='15'/%3E%3Crect x='155' y='195' width='5' height='5'/%3E%3Crect x='160' y='40' width='75' height='75'/%3E%3Crect x='165' y='45' width='65' height='65'/%3E%3Crect x='170' y='50' width='55' height='55'/%3E%3Crect x='175' y='55' width='45' height='45'/%3E%3Crect x='180' y='60' width='35' height='35'/%3E%3Crect x='185' y='65' width='25' height='25'/%3E%3Crect x='190' y='70' width='15' height='15'/%3E%3Crect x='195' y='75' width='5' height='5'/%3E%3Crect x='160' y='120' width='35' height='35'/%3E%3Crect x='165' y='125' width='25' height='25'/%3E%3Crect x='170' y='130' width='15' height='15'/%3E%3Crect x='175' y='135' width='5' height='5'/%3E%3Crect x='200' y='200' width='35' height='35'/%3E%3Crect x='200' width='35' height='35'/%3E%3Crect y='200' width='35' height='35'/%3E%3C/g%3E%3C/svg%3E") !important;
  /* background customized from SVGBackgrounds.com */
  font-size: 10px;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: 'Roboto Mono', monospace;
  font-weight: 400;
}

.center {
  position: relative;
  top: 50%;
  transform: translateY(-50%);
}

.keys {
  display: flex;
  justify-content: center;
  align-content: center;
}

.key {
  border: .2rem solid #000;
  border-radius: 6rem;
  margin: 1rem;
  font-size: 1.5rem;
  padding: 1rem .5rem;
  transition: all .07s ease;
  width: 10rem;
  text-align: center;
  color: #fff;
  background: #000;
}

.playing {
  transform: scale(1.1);
  border-color: #FFA500;
  box-shadow: 0 0 5rem #fff;
}

.playing .sound {
  color: #FFA500;
}

kbd {
  display: block;
  font-size: 5rem;
  font-family: 'Roboto Mono', monospace;
  font-weight: 200;
  color: #fff;
}

.sound {
  font-size: 1.2rem;
  letter-spacing: .1rem;
  color: #FFA500;
}

3. Finally, incorporate the JavaScript code into your HTML document. This code handles key presses and plays the associated drum sound when a key is pressed. It also adds a visual effect to the pressed key.

function removeTransition(e) {
    if (e.propertyName !== 'transform') return;
    e.target.classList.remove('playing');
  }
  function playSound(e) {
    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();
  }
  const keys = Array.from(document.querySelectorAll('.key'));
  keys.forEach(key => key.addEventListener('transitionend', removeTransition));
  window.addEventListener('keydown', playSound);

Feel free to customize the drum kit further. You can add more sounds, change the styling, or enhance the user experience according to your preferences.

That’s all! hopefully, you have successfully created a Drum Kit Using JavaScript. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X