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>JS WebCam Motion Detection</h1>
<h2>By Jason Mayes. Subscribe for updates on <a href="https://github.com/jasonmayes/JS-Motion-Detection">official GitHub project page</a>.</h2>
<p>You will need to view this on a secure connection to access the webcam. Please ensure you are using this link: <a href="https://codepen.io/jasonmayes/pen/IrwHG/">https://codepen.io/jasonmayes/pen/IrwHG/</a> and allow access to your webcam when it asks! Otherwise you will see the GIF of me testing it!</p>

<video id="camStream"></video>
<canvas id="canvas"></canvas>
<canvas id="canvasFinal"></canvas>

<p>A simple, fast, and lightweight (just 531 bytes gzipped when minified using Google Closure compiler) motion revealing algorithm using the live webcam feed.</p>

<p>I was trying to devise a super sensitive motion revealing algorithm which was fairly robust against small changes in lighting etc which was capable of running in a fast manner on desktop and mobile devices.</p>

<p>The nice thing about the result is that all non moving pixels converge to a neutral grey colour. Any changes are darker / lighter than that neutral grey colour. Larger shifts leads to greater contrast.</p>
              
            
!

CSS

              
                #camStream, #canvas {
  opacity:0;
  position:fixed;
  top:0;
  left:0;
}

#canvasFinal {
  margin:0 auto;
  display:block;
  min-width: 640px;
  min-height: 480px;
  background-image: url('https://lh3.googleusercontent.com/opBDNnhXBe1aiLOp3C9yatntR-8fQptvgDu8C1LSZM68khdJh6AQgIHdgWU_T80_2MBhQLjgrJwR6mY=w3840-h2160-rw-no');
  background-repeat: no-repeat;
  background-position: center;
}

body {
  text-align: center;
}

h1 {
  text-align:center;
  margin-top:50px;
}

h1, p, h2, a {
  font-family:'arial';
  color:#3d3d3d;
}

p {
  font-size:10pt;
  font-style:italic;
  color:#686868;
}
              
            
!

JS

              
                /*********************************************************************
*  #### JS Motion Visualiser ####
*  Coded by Jason Mayes. www.jasonmayes.com
*  Please keep this disclaimer with my code if you use it anywhere. 
*  Thanks. :-)
*  Got feedback or questions, ask here:
*  Github: https://github.com/jasonmayes/JS-Motion-Detection/
*  Updates will be posted to this site.
*********************************************************************/
navigator.getUserMedia=navigator.getUserMedia||navigator.webkitGetUserMedia||navigator.mozGetUserMedia||navigator.msGetUserMedia;window.URL=window.URL||window.webkitURL||window.mozURL||window.msURL;
var MotionDetector=function(){function g(a){l=a;c.srcObject=a;c.play()}function m(a){console.error(a)}function n(){if(l){d.width=c.offsetWidth;d.height=c.offsetHeight;h.width=c.offsetWidth;h.height=c.offsetHeight;k.drawImage(c,0,0);f[e]=k.getImageData(0,0,d.width,d.height);e=0==e?1:0;a=k.getImageData(0,0,d.width,d.height);for(var g=a.data.length,b=0;b<g;)a.data[b]=.5*(255-a.data[b])+.5*f[e].data[b],a.data[b+1]=.5*(255-a.data[b+1])+.5*f[e].data[b+1],a.data[b+2]=.5*(255-a.data[b+2])+.5*f[e].data[b+
2],a.data[b+3]=255,b+=4;p.putImageData(a,0,0)}}var e=0,d=document.getElementById("canvas"),h=document.getElementById("canvasFinal"),c=document.getElementById("camStream"),k=d.getContext("2d"),p=h.getContext("2d"),l=null,a=null,f=[];return{init:function(){navigator.getUserMedia?navigator.getUserMedia({video:!0},g,m):console.error("Your browser does not support getUserMedia");window.setInterval(n,32)}}}();MotionDetector.init();

              
            
!
999px

Console