<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>
*, *::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;
}
View Compiled
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);
    }
  };
}

View Compiled

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.