css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <video id="monitor" autoplay style="display: none; width: 320px; height: 240px;"></video>

<div id="canvasLayers"  width="320" height="240"  style="position: relative; left: 0px; top: 0px;">
<canvas id="videoCanvas" width="320" height="240" style="z-index: 1; position: absolute; left:0px; top:0px;"></canvas>
<canvas id="layer2"     width="320" height="240" style="z-index: 2; position: absolute; left:0px; top:0px; opacity:0.5;"></canvas>
</div>
<canvas id="blendCanvas" style="display: none; position: relative; left: 400px; top: 0px; width: 320px; height: 240px;"></canvas>
<div id="messageError"></div>
<div id="messageArea" style="position: relative; left: 0px; top: 270px;">Messages will be displayed here.</div>


            
          
!
            
              		body 
		{
			font-family: Monospace;
			font-weight: bold;
			background-color: #fff;
			margin: 5px;
			overflow: hidden;
		}
            
          
!
            
              /**
 * Provides requestAnimationFrame in a cross browser way.
 * http://paulirish.com/2011/requestanimationframe-for-smart-animating/
 */

if (!window.requestAnimationFrame) {

  window.requestAnimationFrame = (function() {

    return window.webkitRequestAnimationFrame ||
      window.mozRequestAnimationFrame ||
      window.oRequestAnimationFrame ||
      window.msRequestAnimationFrame ||
      function( /* function FrameRequestCallback */ callback, /* DOMElement Element */ element) {

        window.setTimeout(callback, 1000 / 60);

      };

  })();

}

navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
window.URL = window.URL || window.webkitURL;

var camvideo = document.getElementById('monitor');

if (!navigator.getUserMedia) {
  document.getElementById('messageError').innerHTML =
    'Sorry. <code>navigator.getUserMedia()</code> is not available.';
}
navigator.getUserMedia({
  video: true
}, gotStream, noStream);

function gotStream(stream) {
  if (window.URL) {
    camvideo.src = window.URL.createObjectURL(stream);
  } else // Opera
  {
    camvideo.src = stream;
  }

  camvideo.onerror = function(e) {
    stream.stop();
  };

  stream.onended = noStream;
}

function noStream(e) {
  var msg = 'No camera available.';
  if (e.code == 1) {
    msg = 'User denied access to use camera.';
  }
  document.getElementById('errorMessage').textContent = msg;
}

// assign global variables to HTML elements
var video = document.getElementById('monitor');
var videoCanvas = document.getElementById('videoCanvas');
var videoContext = videoCanvas.getContext('2d');

var layer2Canvas = document.getElementById('layer2');
var layer2Context = layer2Canvas.getContext('2d');

var blendCanvas = document.getElementById("blendCanvas");
var blendContext = blendCanvas.getContext('2d');

var messageArea = document.getElementById("messageArea");

// these changes are permanent
videoContext.translate(320, 0);
videoContext.scale(-1, 1);

// background color if no video present
videoContext.fillStyle = '#005337';
videoContext.fillRect(0, 0, videoCanvas.width, videoCanvas.height);

var buttons = [];

var button1 = new Image();
button1.src = "https://stemkoski.github.io/Three.js/images/SquareRed.png";
var buttonData1 = {
  name: "red",
  image: button1,
  x: 320 - 110,
  y: 10,
  w: 100,
  h: 80
};
buttons.push(buttonData1);

var button2 = new Image();
button2.src = "https://stemkoski.github.io/Three.js/images/SquareGreen.png";
var buttonData2 = {
  name: "green",
  image: button2,
  x: 10,
  y: 10,
  w: 100,
  h: 80
};
buttons.push(buttonData2);

/*
var button3 = new Image();
button3.src ="https://stemkoski.github.io/Three.js/images/SquareBlue.png";
var buttonData3 = { name:"blue", image:button3, x:320 - 32 - 10, y:10, w:32, h:32 };
buttons.push( buttonData3 );
*/
// start the loop				
animate();

function animate() {
  requestAnimationFrame(animate);

  render();
  blend();
  checkAreas();
}

function render() {
  if (video.readyState === video.HAVE_ENOUGH_DATA) {
    // mirror video
    videoContext.drawImage(video, 0, 0, videoCanvas.width, videoCanvas.height);
    for (var i = 0; i < buttons.length; i++)
      layer2Context.drawImage(buttons[i].image, buttons[i].x, buttons[i].y, buttons[i].w, buttons[i].h);
  }
}

var lastImageData;

function blend() {
  var width = videoCanvas.width;
  var height = videoCanvas.height;
  // get current webcam image data
  var sourceData = videoContext.getImageData(0, 0, width, height);
  // create an image if the previous image doesn�t exist
  if (!lastImageData) lastImageData = videoContext.getImageData(0, 0, width, height);
  // create a ImageData instance to receive the blended result
  var blendedData = videoContext.createImageData(width, height);
  // blend the 2 images
  differenceAccuracy(blendedData.data, sourceData.data, lastImageData.data);
  // draw the result in a canvas
  blendContext.putImageData(blendedData, 0, 0);
  // store the current webcam image
  lastImageData = sourceData;
}

function differenceAccuracy(target, data1, data2) {
  if (data1.length != data2.length) return null;
  var i = 0;
  while (i < (data1.length * 0.25)) {
    var average1 = (data1[4 * i] + data1[4 * i + 1] + data1[4 * i + 2]) / 3;
    var average2 = (data2[4 * i] + data2[4 * i + 1] + data2[4 * i + 2]) / 3;
    var diff = threshold(fastAbs(average1 - average2));
    target[4 * i] = diff;
    target[4 * i + 1] = diff;
    target[4 * i + 2] = diff;
    target[4 * i + 3] = 0xFF;
    ++i;
  }
}

function fastAbs(value) {
  return (value ^ (value >> 31)) - (value >> 31);
}

function threshold(value) {
  return (value > 0x15) ? 0xFF : 0;
}

// check if white region from blend overlaps area of interest (e.g. triggers)
function checkAreas() {
  for (var b = 0; b < buttons.length; b++) {
    // get the pixels in a note area from the blended image
    var blendedData = blendContext.getImageData(buttons[b].x, buttons[b].y, buttons[b].w, buttons[b].h);

    // calculate the average lightness of the blended data
    var i = 0;
    var sum = 0;
    var countPixels = blendedData.data.length * 0.25;
    while (i < countPixels) {
      sum += (blendedData.data[i * 4] + blendedData.data[i * 4 + 1] + blendedData.data[i * 4 + 2]);
      ++i;
    }
    // calculate an average between of the color values of the note area [0-255]
    var average = Math.round(sum / (3 * countPixels));
    if (average > 50) // more than 20% movement detected
    {
      //console.log("Button " + buttons[b].name + " triggered."); // do stuff
      messageArea.innerHTML = "<font size='+4' color=" + buttons[b].name + "><b>Button " + buttons[b].name + " triggered.</b></font>";
      
      op(buttons[b].name);
      
    }
  }
}

function op(n) {
  console.log(n);
  //window.open("https://www.w3schools.com");
}
//,
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console