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

              
                <nav class="types">
  <button class="type active" data-type="sine">
            Sine
        </button>
  <button class="type" data-type="square">
            Square
        </button>
  <button class="type" data-type="sawtooth">
            Sawtooth
        </button>
  <button class="type" data-type="triangle">
            Triangle
        </button>
</nav>

<section class="piano">
  <div class="keys">
    <div data-frequency="261.626" data-key="a" class="key key-c">
      <span>a</span>
    </div>
    <div data-frequency="277.183" data-key="w" class="key key-cd">
      <span>w</span>
    </div>
    <div data-frequency="293.665" data-key="s" class="key key-d">
      <span>s</span>
    </div>
    <div data-frequency="311.127" data-key="e" class="key key-de">
      <span>e</span>
    </div>
    <div data-frequency="329.628" data-key="d" class="key key-e">
      <span>d</span>
    </div>
    <div data-frequency="349.228" data-key="f" class="key key-f">
      <span>f</span>
    </div>
    <div data-frequency="369.994" data-key="t" class="key key-fg">
      <span>t</span>
    </div>
    <div data-frequency="391.995" data-key="g" class="key key-g">
      <span>g</span>
    </div>
    <div data-frequency="415.305" data-key="y" class="key key-ga">
      <span>y</span>
    </div>
    <div data-frequency="440.000" data-key="h" class="key key-a">
      <span>h</span>
    </div>
    <div data-frequency="466.164" data-key="u" class="key key-ab">
      <span>u</span>
    </div>
    <div data-frequency="493.883" data-key="j" class="key key-b">
      <span>j</span>
    </div>
    <div data-frequency="523.251" data-key="k" class="key key-c">
      <span>k</span>
    </div>
    <div data-frequency="554.365" data-key="o" class="key key-cd">
      <span>o</span>
    </div>
    <div data-frequency="587.330" data-key="l" class="key key-d">
      <span>l</span>
    </div>
    <div data-frequency="622.254" data-key="p" class="key key-de">
      <span>p</span>
    </div>
    <div data-frequency="659.255" data-key=";" class="key key-e">
      <span>;</span>
    </div>
  </div>
</section>

<svg class="note hide" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
        <path d="M12 3v10.55c-.59-.34-1.27-.55-2-.55-2.21 0-4 1.79-4 4s1.79 4 4 4 4-1.79 4-4V7h4V3h-6z" />
    </svg>
              
            
!

CSS

              
                // Vraiables
$white: #fff;
$silver: #f5f5f5;
$black: #151515;

// Global styles
html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
  color: $black;
  font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
  font-size: 14px;
  box-sizing: border-box;
}

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

*,
*:before,
*:after {
  box-sizing: inherit;
}

// Primary styles
.types {
  display: flex;
  justify-content: center;
  padding: 1rem;
}

.type {
  margin: 0 0.25rem;
  padding: 0.5rem 1rem;
  color: $white;
  cursor: pointer;
  border: 1px solid $white;
  background-color: lighten($black, 10%);
  border-radius: 0.25rem;
  transition: all 0.1s cubic-bezier(0.25, 0.1, 0.5, 1.5);
  z-index: 1;
  &:hover {
    background-color: lighten($black, 20%);
  }
  &.active {
    background-color: lighten($black, 50%);
  }
}

.piano {
  perspective: 1000px;
}

.keys {
  height: 60vh;
  width: 90vw;
  max-width: 900px;
  max-height: 350px;
  min-width: 600px;
  min-height: 200px;
  margin: 0 auto;
  display: flex;
  transform: rotateX(45deg);
  perspective: 2000px;
}

.key {
  flex: 1;
  padding: 1rem;
  position: relative;
  cursor: pointer;
  background-color: $silver;
  box-shadow: inset 0 0 0.5rem rgba(0, 0, 0, 0.25);
  transform-origin: center top;
  transform-style: preserve-3d;
  transition: all 0.1s cubic-bezier(0.25, 0.1, 0.5, 1.5);
  perspective: 10000px;
  display: flex;
  span {
    width: 100%;
    text-align: center;
    font-size: 0.75rem;
    align-self: flex-end;
    pointer-events: none;
  }
  &::after,
  &::before {
    left: 0;
    content: '';
    position: absolute;
    background-color: $silver;
    box-shadow: inset 0 0 2rem rgba(0, 0, 0, 0.25);
  }
  &::after {
    top: 100%;
    width: 100%;
    height: 5rem;
    transform-origin: top;
    transform: rotateX(-60deg);
  }
  &::before {
    width: 5rem;
    height: 100%;
    transform-origin: bottom right;
    transform: rotateY(90deg) translate(calc(100% + 2.5rem), 2rem) skew(0, 30deg);
  }
  @for $i from 12 through 17 {
    &:nth-child(#{$i}) {
      z-index: -#{$i};
    }
  }
  @each $key in cd, de, fg, ga, ab {
    &-#{$key} {
      flex: 0.5;
      height: 50%;
      margin: 0 -3.3%;
      background-color: $black;
      box-shadow: inset 0 0 0.5rem rgba(255, 255, 255, 0.75);
      align-self: flex-start;
      z-index: 9 !important;
      span {
        color: $white;
      }
      &::before,
      &::after {
        background-color: $black;
        box-shadow: inset 0 0 2rem rgba(255, 255, 255, 0.25);
      }
      &::before {
        width: 100%;
        height: 100%;
        transform-origin: bottom right;
        transform: rotateY(90deg) translate(90%, 1.5rem) skew(0, 30deg);
      }
      &.down {
        background-color: lighten($black, 7.5%);
      }
    }
  }
  &:nth-child(n+10)::before {
    transform-origin: bottom left;
    transform: rotateY(90deg) translate(0, -1rem) skew(0, 30deg);
    width: 4.5rem;
    height: 100%;
  }
}

.down {
  background-color: darken($silver, 5%);
  transform: rotateX(-2.5deg);
}

.note {
  position: absolute;
  fill: $black;
  width: 5rem;
  height: 5rem;
  animation: flow 2s both cubic-bezier(0.5, 0.025, 0.5, 1);
}

// Helpers
.hide {
  display: none;
}

// Animation for notes
@keyframes flow {
  0% {
    opacity: 0;
    transform: scale(0.5);
  }
  20% {
    opacity: 1;
    transform: scale(1) translate(2.5rem, -5rem);
  }
  40% {
    transform: scale(0.9) translate(-2.5rem, -8rem);
  }
  60% {
    transform: scale(1) translate(2.5rem, -12rem);
  }
  80% {
    opacity: 1;
    transform: scale(0.9) translate(-2.5rem, -17rem);
  }
  100% {
    opacity: 0;
    transform: scale(1.1) translate(2.5rem, -25rem);
  }
}
              
            
!

JS

              
                class Piano {
    constructor() {
        this.keys = {}
        this.type = 'sine'
        this.audio = new(window.AudioContext)()

        // Add all event listeners 
        this.addTypeEvents()
        this.addPointerEvents()
        this.addKeyboardEvents()
    }

    // Add events to handle switching oscillator type
    addTypeEvents() {
        Array.from(document.querySelectorAll('.type')).forEach(element => {
            element.addEventListener('click', this.changeType)
        })
    }
    
    // Add events to handle various pointer interactions
    addPointerEvents() {
        const events = {
            'start': ['mousedown', 'touchstart'],
            'stop': ['mouseup', 'mouseout', 'touchend']
        }
        
        Array.from(document.querySelectorAll('.key')).forEach(element => {
            for (event in events) {
                events[event].forEach(e => element.addEventListener(e, this[event]))
            }
        })
    }

    // Add events to handle keyboard interaction
    addKeyboardEvents() {
        window.addEventListener('keydown', this.start)
        window.addEventListener('keyup', this.stop)
    }

    // Change the active oscillator type
    changeType = (event) => {
        const current = document.querySelector('.active')
        current.classList.remove('active')

        const element = this.getElement(event)
        element.classList.add('active')

        this.type = element.dataset.type
    }

    // Create and return a new oscillator
    createOscillator = (element, frequency) => {
        const oscillator = this.audio.createOscillator()

        oscillator.type = this.type
        oscillator.frequency.setValueAtTime(frequency, this.audio.currentTime)
        oscillator.connect(this.audio.destination)

        return oscillator
    }

    // Add class, create and play oscillator
    start = (event) => {
        const element = this.getElement(event)
        if (! element) {
          return
        }
        
        const frequency = element.dataset.frequency
        const oscillator = this.createOscillator(element, frequency)

        element.classList.add('down')
        this.insertNote(element)

        this.keys[frequency] = oscillator
        this.keys[frequency].start()
    }

    // Remove class, stop and remove oscillator
    stop = (event) => {
        const element = this.getElement(event)
        if (! element) {
          return
        }
        
        const frequency = element.dataset.frequency

        element.classList.remove('down')

        if (this.keys[frequency]) {
            this.keys[frequency].stop()
            this.keys[frequency] = null
        }
    }

    // Helper to get correct element
    getElement(event) {
        return event.key ? document.querySelector(`.key[data-key="${event.key}"]`) : event.target
    }

    // Insert note svg for animation
    insertNote(element) {
        const svgNote = document.querySelector('.note')
        const note = svgNote.cloneNode(true)
        const offset = element.getBoundingClientRect()

        note.style.top = `${offset.y - 50}px`
        note.style.left = `${offset.x + 25}px`

        note.classList.remove('hide')
        document.body.appendChild(note)

        setTimeout(() => note.remove(), 2000)
    }

}

new Piano()
              
            
!
999px

Console