JS30 #01 - DrumKit

In this example below you will see how to do a JS30 #01 - DrumKit with some HTML / CSS and Javascript

A simple DrumKit app developed to face the 30 days JavaScript challenges by Wes Bos.If you want you can try to: https://javascript30.com

Thumbnail
This awesome code was written by sabbirrahman, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright sabbirrahman ©
  • HTML
  • CSS
  • JavaScript
    <div class="container">
  <h1>DrumKit</h1>
  <div class="keys">
    <div data-key="67" class="key">
      <kbd>C</kbd>
      <span>clap</span>
    </div>
    <div data-key="72" class="key">
      <kbd>H</kbd>
      <span>hihat</span>
    </div>
    <div data-key="75" class="key">
      <kbd>K</kbd>
      <span>kick</span>
    </div>
    <div data-key="79" class="key">
      <kbd>O</kbd>
      <span>openhat</span>
    </div>
    <div data-key="66" class="key">
      <kbd>B</kbd>
      <span>boom</span>
    </div>
    <div data-key="82" class="key">
      <kbd>R</kbd>
      <span>ride</span>
    </div>
    <div data-key="83" class="key">
      <kbd>S</kbd>
      <span>snare</span>
    </div>
    <div data-key="84" class="key">
      <kbd>T</kbd>
      <span>tom</span>
    </div>
    <div data-key="76" class="key">
      <kbd>L</kbd>
      <span>tink</span>
    </div>
  </div>
  <p>Made with &hearts; by <a href="http://github.com/sabbirrahman" target="_blank">Sabbir Rahman</a></p>
</div>

<audio data-key="67" src="http://res.cloudinary.com/sabbirrahman/video/upload/v1502694157/clap_zzxlfj.wav"></audio>
<audio data-key="72" src="http://res.cloudinary.com/sabbirrahman/video/upload/v1502694157/hihat_epvrsa.wav"></audio>
<audio data-key="75" src="http://res.cloudinary.com/sabbirrahman/video/upload/v1502694157/kick_uop9oa.wav"></audio>
<audio data-key="79" src="http://res.cloudinary.com/sabbirrahman/video/upload/v1502694159/openhat_wxy6w1.wav"></audio>
<audio data-key="66" src="http://res.cloudinary.com/sabbirrahman/video/upload/v1502694158/boom_f5o8rw.wav"></audio>
<audio data-key="82" src="http://res.cloudinary.com/sabbirrahman/video/upload/v1502694161/ride_yxqwmi.wav"></audio>
<audio data-key="83" src="http://res.cloudinary.com/sabbirrahman/video/upload/v1502694158/snare_zoc0b6.wav"></audio>
<audio data-key="84" src="http://res.cloudinary.com/sabbirrahman/video/upload/v1502694159/tom_fjbgop.wav"></audio>
<audio data-key="76" src="http://res.cloudinary.com/sabbirrahman/video/upload/v1502694158/tink_qufq8u.wav"></audio>

/*Downloaded from https://www.codeseek.co/sabbirrahman/js30-01-drumkit-eEGVRj */
    * {
  -webkit-touch-callout: none;
    -webkit-user-select: none;
     -khtml-user-select: none;
       -moz-user-select: none;
        -ms-user-select: none;
            user-select: none;
  padding: 0px;
  margin: 0px;
}

body {
  background: url('http://i.imgur.com/e2RA6zR.jpg') center/cover;
}

.container {
  background: rgba(44, 44, 44, 0.75);
  justify-content: space-between;
  flex-direction: column;
  align-items: center;
  min-height: 100vh;
  overflow-y: auto;
  display: flex;
  width: 100vw;
}

h1 {
  font-family: 'Comic Sans MS', 'Helvetica', sans-serif;
  margin: 30px 0px;
  font-size: 42px;
  color: #FFF;
}

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

.key {
  background: rgba(255, 255, 255, 0.2);
  transition: 0.07s all ease-in-out;
  border-radius: 5px;
  text-align: center;
  padding: 15px 0px;
  width: 90px;
  color: #FFF;
  margin: 7px;
}
.key.playing {
  box-shadow: 0px 0px 2px 10px rgba(0, 0, 0, 0.1);
  transform: scale(1.1);
}
.key:hover {
  background: rgba(255, 255, 255, 0.3);
  cursor: pointer;
}

.key kbd {
  font-size: 48px;
  display: block;
}

.key span {
  letter-spacing: 2px;
  display: block;
}

p {
  font-family: 'Comic Sans MS', sans-serif;
  margin: 30px 0px;
  color: #FFF;
}

p a { color: #FFF; }


/*Downloaded from https://www.codeseek.co/sabbirrahman/js30-01-drumkit-eEGVRj */
    const keys = Array.from(document.querySelectorAll('.key'));
  
function removeTransition(ev) {
  if (ev.propertyName !== 'transform') return;
  ev.target.classList.remove('playing');
}

function playSound(ev) {
  const keyCode = ev.type === 'keydown' ? ev.keyCode : ev.currentTarget.dataset.key;
  const audio = document.querySelector(`audio[data-key="${keyCode}"]`);
  if (!audio) return;
  audio.currentTime = 0;
  audio.play();
  
  const key = document.querySelector(`div[data-key="${keyCode}"]`);
  key.classList.add('playing');
}

window.addEventListener('keydown', playSound);
keys.forEach(key => key.addEventListener('click', playSound));
keys.forEach(key => key.addEventListener('transitionend', removeTransition));

Comments