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

              
                <h1>Spooky 👻 Theremin</h1>

<div class="wrapper">
  <div id="get-started">
    <p>Uh oh, you've stumbled on the spooky ghost theremin! This ghost was stabbed in the back during a robbery gone wrong by his partner who wore a cyan mask. Now he hates the color cyan and will start making his spooky call whenever he sees it! He also adjusts his pitch and volume depending on where he sees the cyan object, which is where the theramin part comes in. Don't read too much into the name.</p>
    <p>To hear his spooky call, allow the page to use your webcam, go grab a cyan object, and hold it in front of your camera! Don't move it around too much or it might get...TOO SPOOKY!</p>
  </div>

  <video id="video" width="640" height="480" preload autoplay loop muted></video>
  <canvas id="canvas" width="640" height="480"></canvas>
</div>
              
            
!

CSS

              
                *, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  position: relative;
}

:root {
  color: white;
  font-family: Muli;
}

body {
  background-color: black;
}

a {
  color: cyan;
}

h1 {
  color: red;
  font-family: 'Creepster';
  font-size: 3rem;
  text-align: center;
}

p {
  line-height: 1.6;
  margin-bottom: 1rem;
}

.wrapper {
  text-align: center;
  max-width: 70ch;
  margin: 0 auto;
}

#video {
  left: 0;
  position: fixed;
  top: 0;
  visibility: hidden;
}

#canvas {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
}

              
            
!

JS

              
                if (location.protocol !== 'https:') {
  document.querySelector('.wrapper').innerHTML = '<p>In order to grant access to your camera,'
    + '<br />you must load this page via HTTPS.'
    + '<br /><a href="https://codepen.io/dgca/pen/b04d25d0e48bbbc3e594ff25f090fabf" target="blank">Click here.</a></p>';
} else {
  document.addEventListener('click', initSpookiness);
}

function initSpookiness() {
  document.removeEventListener('click', initSpookiness);
  document.getElementById('get-started').remove();
  // Canvas
  const canvas = document.getElementById('canvas');
  canvas.height = canvas.clientHeight;
  canvas.width = canvas.clientWidth;
  drawCanvas();
  window.addEventListener('resize', resizeCanvas);
  // Video
  const vHeight = document.getElementById('video').height;
  const vWidth = document.getElementById('video').width;
  // Tone.JS
  const synth = new Tone.MonoSynth({
    oscillator: {
      type: 'sine'
    }
  }).toMaster();
  // Tracker
  const colorToTrack = new tracking.ColorTracker(['cyan']);
  let trackerRects = null;
  colorToTrack.on('track', (e) => {
    if (e.data.length === 0) {
      trackerRects = null;
    } else {
      trackerRects = e.data;
      playNote();
    }
  });
  tracking.track('#video', colorToTrack, {camera: true});

  function resizeCanvas() {
    canvas.height = canvas.clientHeight;
    canvas.width = canvas.clientWidth;
    drawCanvas();
  }

  function drawCanvas() {
    const ctx = canvas.getContext('2d');
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.font = '36px sans-serif';
    
    if (trackerRects) {
      const rect = trackerRects[0];
      const xPos = canvas.width - (((rect.x + (rect.width / 2)) / vWidth) * canvas.width);
      const yPos = ((rect.y + (rect.height / 2)) / vHeight) * canvas.height;
      ctx.fillText('👻', xPos, yPos);
    }

    window.requestAnimationFrame(drawCanvas);
  };

  function playNote() {
    if (trackerRects) {
      // Frequency range is from 200 to 1000
      const y = trackerRects[0].y;
      const freq = 1000 - (y / vHeight * 800).toFixed();

      // volume range is from 0 (loudest) to -40 (quietest)
      const x = trackerRects[0].x;
      const vol = 0 - (x / vWidth * 20);
      synth.volume.value = vol;
      synth.triggerAttackRelease(freq, 1);
    }
  };
}


              
            
!
999px

Console