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.

              <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>
              #camStream, #canvas {

#canvasFinal {
  margin:0 auto;
  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 {

h1, p, h2, a {

p {
*  #### 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.
var MotionDetector=function(){function g(a){l=a;c.src=window.URL&&window.URL.createObjectURL(a)||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();

🕑 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 ..................