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

              
                - var notes = 'C/B#,C#/Db,D,D#/Eb,E/Fb,F/Eb,F#/Gb,G,G#/Ab,A,A#/Bb,B/Cb'.split(',').map(function(n) { return n.split('/'); })
- var isNatural = function(names) { return names[0].length == 1; }

mixin key(names, octave)
  .piano-key(class=[
    'piano-key-' + (isNatural(names) ? 'natural' : 'accidental'),
    'piano-key-octave-' + octave,
  ].concat(names.map(function(n) { return 'piano-key-' + n; }))
  .concat(names.map(function(n) { return 'piano-key-' + n + octave; }))
  id='piano_key_' + names[0] + octave,
  data-note=names[0] + octave)

.piano
  +key(['A'], 0) //- A0
  +key(['A#', 'Bb'], 0)  //- A#0
  +key(['B', 'Cb'], 0) //- B0
  - for(var octave = 1; octave <= 7; octave++) {
    for names in notes
      +key(names, octave)
  - }
  +key(['C'], 8) //- C8
              
            
!

CSS

              
                /*
You won't see the full piano if your screen/window is less than 1560px.
If you want to see it despite this, comment out the media queries at
the bottom.
*/

$key-white-width: 30px;
$key-white-height: 120px;
$key-black-width: $key-white-width * (2 / 3); // should be divisible by 2
$key-black-height: $key-white-height * 0.55;

html, body {
  height: 100%;
}

body {
  display: flex;
  align-items: center;
  justify-content: center;
}

.piano {
  padding-left: 1px;
}

.piano-key {
  float: left;
  border-radius: 0 0 4px 4px;
}

.piano-key-natural {
  background: #f5f5f5 linear-gradient(to bottom, transparent, #fff, transparent);
  border: 1px solid;
  border-color: rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.3) rgba(0, 0, 0, 0.15);
  border-right-width: 0;
  border-bottom-width: 2px;
  height: $key-white-height;
  width: $key-white-width;
  margin-left: -1px;
  
  &:last-child {
    border-right-width: 1px;
  }
  
  &.piano-key-pressed,
  &:active {
    background: #d9d9d9 linear-gradient(to bottom, #f1f1f1, transparent, #f1f1f1);
    border-bottom-width: 1px;
    border-left-color: rgba(0, 0, 0, .15);
    box-shadow: inset -2px 0 0 0 rgba(0, 0, 0, .15),
                inset  1px 0 0 0 rgba(0, 0, 0, .15);
  }
}

.piano-key-accidental {
  background: #111 linear-gradient(to bottom, transparent, rgba(255, 255, 255, .075), transparent);
  box-shadow: inset 0 0 0 1px #000,
              inset 0 -4px 0 1px #000,
              inset 0 -5px 0 1px rgba(255, 255, 255, .2);
  height: $key-black-height;
  width: $key-black-width;
  position: relative;
  left: -1 * ($key-black-width / 2);
  margin-right: -1 * $key-black-width;
  
  &.piano-key-pressed,
  &:active {
    background: #000 linear-gradient(to bottom, transparent, rgba(255, 255, 255, .2));
    box-shadow: inset 0 0 0 1px #000,
                inset 0 -2px 0 1px #000,
                inset 0 -3px 0 1px rgba(255, 255, 255, .05);
  }
}

// Responsive Piano Media Queries
// Comment below if you want the full Grand Piano, regardless
// of your screen/window size

//*
$octave-width: 7 * $key-white-width;
$full-width: 7 * $octave-width + 3 * $key-white-width;

@media (max-width: $full-width + 1px) {
  .piano-key-octave-0,
  .piano-key-octave-8 {
    display: none;
  }
}

@media (max-width: 7 * $octave-width + 1px) {
  .piano-key-octave-1 {
    display: none;
  }
}

@media (max-width: 6 * $octave-width + 1px) {
  .piano-key-octave-7 {
    display: none;
  }
}

@media (max-width: 5 * $octave-width + 1px) {
  .piano-key-octave-2 {
    display: none;
  }
}

@media (max-width: 4 * $octave-width + 1px) {
  .piano-key-octave-6 {
    display: none;
  }
}

@media (max-width: 3 * $octave-width + 1px) {
  .piano-key-octave-3 {
    display: none;
  }
}

@media (max-width: 2 * $octave-width + 1px) {
  .piano-key-octave-5 {
    display: none;
  }
}

//*/
              
            
!

JS

              
                /*
Have a MIDI device? Connect it to your computer and start playing.
This piano will recognize your device (if your browser supports
WebMIDI or the WebMIDI polyfill) and mirror your playing.
*/

(function(window, document, undefined) {

  var notes, midi, currentInput;

  function onMidiMessage(msg) {
    var action = isNoteOffMessage(msg) ? 'remove' :
                   (isNoteOnMessage(msg) ? 'add' : null),
        noteDiv;

    if(action && (noteDiv = getNoteDiv(msg))) {
      noteDiv.classList[action]('piano-key-pressed');
    }
  }
  
  const MIDI_A0_NUM = 21;

  function getNoteDiv(msg) {
    var noteNum = getMessageNote(msg) - MIDI_A0_NUM;
    
    if(notes && 0 <= noteNum && noteNum < notes.length) {
      return notes[noteNum];
    }
  }

  const CMD_NOTE_ON = 9;
  const CMD_NOTE_OFF = 8;

  function isNoteOnMessage(msg) {
    return getMessageCommand(msg) == CMD_NOTE_ON;
  }

  function isNoteOffMessage(msg) {
    var cmd = getMessageCommand(msg);
    return cmd == CMD_NOTE_OFF ||
      (cmd == CMD_NOTE_ON && getMessageVelocity(msg) == 0);
  }

  function getMessageCommand(msg) { return msg.data[0] >> 4; }
  function getMessageNote(msg) { return msg.data[1]; }
  function getMessageVelocity(msg) { return msg.data[2]; }

  function selectInput(input) {
    if(input != currentInput) {
      if(currentInput) {
        currentInput.removeEventListener('midimessage', onMidiMessage);
        currentInput.close();
      }
      
      input.addEventListener('midimessage', onMidiMessage);
      currentInput = input;
    }
  }

  function populateInputList() {
    var inputs = Array.from(midi.inputs.values());

    if(inputs.length == 1) {
      selectInput(inputs[0]);
    } else {
      // TODO: handle multiple MIDI inputs
    }
  }

  function onMIDIAccessSuccess(access) {
    midi = access;
    access.addEventListener('statechange', populateInputList, false);
    populateInputList();
  }

  function onMIDIAccessFail() {
    console.error('Request for MIDI access was denied!');
  }

  if('requestMIDIAccess' in window.navigator) {
    window.navigator
      .requestMIDIAccess()
      .then(onMIDIAccessSuccess, onMIDIAccessFail);
  } else {
    console.error('Your device doesn\' support WebMIDI or its polyfill');
  }

  document.addEventListener('DOMContentLoaded', function() {
    notes = document.getElementsByClassName('piano-key');
  }, false);

})(window, window.document);
              
            
!
999px

Console