This course will become read-only in the near future. Tell us at if that is a problem.

Bluescreen and other video editing

I'm going to Haiti, so this will be my last post for a while:  Bluescreen and other video editing.

One of the crucial parts of using HTML5 and open <video> is that we can grab the video frame and add visual effects.  One example of this is at - so if that doesn't work for you, download an updated Firefox, Google Chrome, or Safari browser.

The first thing we need to do is create an HTML video.  For security reasons, these projects only work if you have a video and a page coming over the web, from the same website. If you have Apache on your local computer (free install) or a web server of your own, great.  Otherwise, this is just a primer on how to do it:

Add the video and two <canvas> elements, hiding all but the output canvas:

<video id="player" src="myvideo.ogv" width="500" height="300" style="display:none">
<canvas id="framecopy" width="500" height="300" style="display:none;">
<canvas id="outframe" width="500" height="300">

Grab the video and canvases in JavaScript

video = document.getElementById("player");

canvas = document.getElementById("framecopy");

ctx = canvas.getContext("2d");

canvas2 = document.getElementById("outframe");

ctx2 = canvas2.getContext("2d");

Grab the video's frame and paste it onto the first canvas, then back into the program as frame

ctx.drawImage(video, 0, 0, 500, 300);

frame = ctx.getImageData(0, 0, 500, 300);

Go through each dot in the frame.  Each pixel has red, blue, green, and alpha values.  If a pixel is especially blue, we will set the alpha to zero, making that pixel transparent.  This is how a blue screen works.  Ready?

pixelCount = / 4;

for(var i=0; i < pixelCount; i++) {

    redVal =[ i * 4 ] ;

   greenVal =[ i * 4 + 1];

   blueVal =[ i * 4 + 2];

   alphaVal =[ i * 4 + 3];

   if(blueVal > 220 && greenVal < 30 && redVal < 30){

  [ i * 4 + 3 ] = 0;



Now we paste the altered frame into canvas2, revealing it to the user:

ctx2.putImageData(frame, 0, 0);

If you wanted to create an image out of your edited frame, to save or to paste onto the page, to review motion frame-by-frame or to do further editing, you would use this code:

newImg = document.createElement("img");

newImg.src = canvas2.toDataURL(); = "300px"; = "500px";


Task Discussion

  • romanrandom   May 19, 2012, 3:18 p.m.


    This kind of possibilities make the web an even more exciting place for creating content! 

    I tried the example above but somehow I was always getting this error:

    Uncaught TypeError: Cannot call method 'drawImage' of null

    ctx.drawImage(video, 0, 0, 500, 300);

    I'm a JavaScript newbie so I might be missing something...

    But I could make it work using the code from this tutorial:

     and changing the for loop.