Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="main">
  
  <div class="controls">
    <div data-status="on" class="controls__btn controls--on">
      <svg viewBox="0 0 24 24">
          <path d="M16.56,5.44L15.11,6.89C16.84,7.94 18,9.83 18,12A6,6 0 0,1 12,18A6,6 0 0,1 6,12C6,9.83 7.16,7.94 8.88,6.88L7.44,5.44C5.36,6.88 4,9.28 4,12A8,8 0 0,0 12,20A8,8 0 0,0 20,12C20,9.28 18.64,6.88 16.56,5.44M13,3H11V13H13" />
      </svg>
    </div>
    <div class="controls__btn controls--volume">
       <svg viewBox="0 0 24 24">
          <path d="M3,9H7L12,4V20L7,15H3V9M14,11H22V13H14V11Z" />
      </svg>
    </div>
    <div class="controls__btn controls--volume">
       <svg viewBox="0 0 24 24">
          <path d="M3,9H7L12,4V20L7,15H3V9M14,11H17V8H19V11H22V13H19V16H17V13H14V11Z" />
      </svg>
    </div>
  </div>
  
  <div class= "infoscreen">
    <p>Press any key displayed on the kit to play sound.</p>
  </div>
  
  <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 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 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://www.dl.dropboxusercontent.com/s/yuk0zwqg8p8afto/clap.wav"></audio>
  <audio data-key="83" src="https://www.dl.dropboxusercontent.com/s/34i7si261halbzk/hihat.wav"></audio>
  <audio data-key="68" src="https://www.dl.dropboxusercontent.com/s/lhxserhy0wxwb6w/kick.wav"></audio>
  <audio data-key="70" src="https://www.dl.dropboxusercontent.com/s/ifym68ymiog8vmv/openhat.wav"></audio>
  <audio data-key="71" src="https://www.dl.dropboxusercontent.com/s/liakfx104sp1dwr/boom.wav"></audio>
  <audio data-key="72" src="https://www.dl.dropboxusercontent.com/s/y0g3llaaorfypt7/ride.wav"></audio>
  <audio data-key="74" src="https://www.dl.dropboxusercontent.com/s/7x9rclesue3ujx5/snare.wav"></audio>
  <audio data-key="75" src="https://www.dl.dropboxusercontent.com/s/2ppacuhfj9wgqwn/tom.wav"></audio>
  <audio data-key="76" src="https://www.dl.dropboxusercontent.com/s/6m7w3sq1zdgzzoc/tink.wav"></audio>
              
            
!

CSS

              
                $ash: linear-gradient(to bottom, #606c88, #3f4c6b);
$royal: linear-gradient(to right, #141e30, #243b55);
$Lbluegray: #37474F;
$Dbluegray: #263238;
$Dpurple: #311B92;
$Lpurple: #5E35B1;
$Lgreen: #81C784;
$Lred: #EF5350;
$Dgray: #212121;
$Lgray: #BDBDBD;
$black: #000;
$white: #CFD8DC;

* {
  border-box: box-sizing;
  margin: 0;
  padding: 0;
}

body {
  position: relative;
  width: 100%;
  height:100%;
  font-family: 'Inconsolata', sans-serif;
  font-weight: 400;
  font-size: 0.9rem;
  background: linear-gradient(to right, #FFFFFF, #ECE9E6); 
}

.main {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  
  margin: 100px auto;
  padding: 20px 0;
  width: 550px;
  height: 200px;  
  background: $ash;
  border-radius: 15px;
  border-bottom: 10px solid $Dbluegray;
  box-shadow: 1px 1px 5px 0 $Lbluegray;
}

.controls {
  display: flex;
  justify-content: space-around;
  align-items: center;
  width: 200px;
  height: 100px;
  border-bottom: thin solid $white;
  border-right: thin solid $white;
  
  &#{__btn} {
    position: relative;
    width: 50px;
    height: 45px;
    background-color: $Lbluegray;
    border-radius: 15px;
    border-bottom: 5px solid $Dgray;
    transition: all ease 0.2s;
    
    &:hover {
      background-color: $Dbluegray;
      border-bottom-color: $black;
    }          
  }
  
  &#{--on} {
    svg {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%,-50%);
      display: block;
      width: 70%;
      height: 70%;
      
      path {
        fill: $Lgreen;
      }
    }     
  }
  
  &#{--off} {
    svg path {
        fill: $Lred;
      }     
  }
  
  &#{--volume} {
    svg {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%,-50%);
      display: block;
      width: 70%;
      height: 70%;
      
      path {
        fill: $white;
      }
    }     
  }  
}

.infoscreen {
  display: table;
  padding: 10px;
  width: 290px;
  height: 80px;
  border-bottom: thin solid $white;
  user-select: none;
  
  p {
    display: table-cell;
    padding: 15px;
    background: $royal;
    border-radius: 5px;
    box-shadow:inset 1px 1px 30px 5px $Dgray;
    color: $white;
    vertical-align: middle;
    text-align: center;
    text-transform: uppercase;
  }
  
  
}

.keys {
  flex-basis: 100%;
  padding: 15px;
  display: flex;
  justify-content: space-evenly;
  align-items: center;
  user-select: none;
  
  .key {
    width: 9.7%;
    height: 80px;
    background-color: $white;
    border-radius: 3px;
    border-bottom: 5px solid #BDBDBD;
    transition: all ease 0.07s;
    text-align: center;
    
    
    &:hover {
      background-color: $Lbluegray;
      border-bottom-color: $Dbluegray;
      
      kbd, .sound {
        color: $white;
      }
    }
    
    kbd {
      display: block;
      padding-top: 10px;
      font-weight: 700;
      font-size: 2.5rem;
      color: $Dpurple;
    }

    .sound {
      font-size: 0.8rem;
      color: $Lpurple;
    }  
  }
  
  .playing {
    background-color: $Lgray;
    border-bottom: 0;
    
    kbd, .sound {
      color: $black;
    }
  }
}

.clicked {
  background-color: $Dbluegray;
  border-bottom: 0;
  
  &:hover {
      background-color: $Dgray;
    }  
}

.disabled {
  cursor: not-allowed;
}

              
            
!

JS

              
                const power = document.querySelector('.controls--on');
const vols = document.querySelectorAll('.controls--volume');
const info = document.querySelector('.infoscreen p');
const keys = document.querySelectorAll('.key');
var volume = 1.0;

turnOn();
power.addEventListener('click', onOff);
displayInfo();


//FUNCTIONS
function onOff(e) {
  (this.dataset.status === 'on') ? turnOff() : turnOn();
  this.classList.toggle('controls--off');
}

function turnOn() { 
  power.dataset.status = 'on';
  power.classList.add('clicked');
  volume = 1.0;
  keys.forEach(key => {
    key.addEventListener('transitionend', removeTransition);
    key.addEventListener('mousedown',(e) => key.classList.add('playing'));
    key.addEventListener('mouseup',(e) => key.classList.remove('playing'));
  });
  vols.forEach(vol => {
    vol.addEventListener('click', volumeControl);
    vol.addEventListener('mousedown',(e) => vol.classList.add('clicked'));
    vol.addEventListener('mouseup',(e) => vol.classList.remove('clicked'));
  });
  
  window.addEventListener('keydown', playSound);
  
  vols.forEach(vol => vol.classList.remove('disabled'));
  document.querySelector('.keys').classList.remove('disabled');
}

function turnOff() {
  power.classList.remove('clicked');
  power.dataset.status = 'off';
  keys.forEach(key => {
    key.removeEventListener('transitionend', removeTransition);
    key.addEventListener('mousedown',(e) => key.classList.remove('playing'));
  });
  vols.forEach(vol => {
    vol.removeEventListener('click', volumeControl);
    vol.addEventListener('mousedown',(e) => vol.classList.remove('clicked'));
  });
  window.removeEventListener('keydown', playSound);
  
  vols.forEach(vol => vol.classList.add('disabled'));
  document.querySelector('.keys').classList.add('disabled');
}

function displayInfo() {
  //Get default text (Press any key)
  const defaultText = info.innerText;
  
  //Display which sound, which key : in playSound  
  //Display volume change : in volumeControl
  //Mutation observer for power status change
  const observer = new MutationObserver(function(mutations) {
    mutations.forEach(function(mutation) {
      let powerStt = mutation.target.dataset.status;
      if (powerStt === 'on') {
        info.innerHTML = `Power ${powerStt}.`;
        setTimeout(() => info.innerText = 'Volume maxed out.',600);
        setTimeout(() => info.innerText = defaultText,1200);      
      } else info.innerText = `Power ${powerStt}.`;  
    });
  });
  
  const config = {attributes: true};
  
  observer.observe(power, config);  
}

function playSound(e) {
  //select <audio> and .key element attached to keydown keycode     
  const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
  const key = document.querySelector(`.key[data-key="${e.keyCode}"]`);
  
  //Play Audio
  if(!audio) return; // stop the function if audio not found 
  // else play the audio
  audio.volume = volume; //set the volume, by default is 1.0
  audio.currentTime = 0; //rewind sound to the start, in case pressing same sound repeatedly
  audio.play();
  
  //Toggle .playing class
  key.classList.add('playing');
  
  //Display which key, which sound is being played
  info.innerHTML = `Key: ${key.querySelector('kbd').innerText}. <br>
                    Sound: ${key.querySelector('span').innerText}.`;
}

function removeTransition(e) {
  if(e.propertyName !== 'border-bottom-color') return; //skip if not transition on transform, to prevent catching a whole lot of unnecessary events

  //else remove transition
  // console.log(this); 
  this.classList.remove('playing');  
}

function volumeControl(e) {
  //Change volume  
  if (this === vols[0]) {
    volume = (volume >= 0.1) ? round(volume-0.1,1) : round(volume-0,1);
    vols[1].classList.remove('disabled');
  vols[1].querySelector('path').setAttribute('d','M3,9H7L12,4V20L7,15H3V9M14,11H17V8H19V11H22V13H19V16H17V13H14V11Z');
    
    if (volume == 0) {
      info.innerText = 'Sound is muted';
      vols[0].classList.add('disabled');
  vols[0].querySelector('path').setAttribute('d','M3,9H7L12,4V20L7,15H3V9M16.59,12L14,9.41L15.41,8L18,10.59L20.59,8L22,9.41L19.41,12L22,14.59L20.59,16L18,13.41L15.41,16L14,14.59L16.59,12Z');
    }
  } else {
    volume = (volume <= 0.9) ? round(volume+0.1,1) : round(volume+0,1);
    vols[0].classList.remove('disabled');
  vols[0].querySelector('path').setAttribute('d','M3,9H7L12,4V20L7,15H3V9M14,11H22V13H14V11Z');
    
    if (volume === 1) {
      info.innerText = 'Sound is already maxed out'; 
      vols[1].classList.add('disabled');
  vols[1].querySelector('path').setAttribute('d','M14,3.23V5.29C16.89,6.15 19,8.83 19,12C19,15.17 16.89,17.84 14,18.7V20.77C18,19.86 21,16.28 21,12C21,7.72 18,4.14 14,3.23M16.5,12C16.5,10.23 15.5,8.71 14,7.97V16C15.5,15.29 16.5,13.76 16.5,12M3,9V15H7L12,20V4L7,9H3Z');
    }
  }
  
  //Display volume value
  if (volume === 0 || volume === 1) setTimeout(() => info.innerText = `Volume: ${round(volume,1)}`,600);
  else info.innerText = `Volume: ${round(volume,1)}`;  
 
  //Snippet round function
  function round(number, precision) {
      var factor = Math.pow(10, precision);
      var tempNumber = number * factor;
      var roundedTempNumber = Math.round(tempNumber);
      return roundedTempNumber / factor;
  };
}





              
            
!
999px

Console