views:

41

answers:

1

I am testing out canvas and video and I have read up on alot of examples, but I am running into a slight problem when I press the play button on the video canvas doesn't play the video as well it just shows a snapshot.

I can not figure out why it doesn't play simultaneously. Here is my code in javascript.

function draw()
{
var ctx3 = document.getElementById('c3');
var ctx1 = document.getElementById('c1');
var conts = ctx3.getContext('2d');
var videos = document.getElementById('video');
var frames = conts.getImageData(0,0,300,300);
videos.addEventListener("play", function ()
{
conts.drawImage(videos, 0, 0, 300, 300);

var l = frames.data.length/4;
for (var i = 0; i<1; i++){
var r = frames.data(i * 4 + 0);
var g = frames.data(i * 4 + 1);
var b = frames.data(i * 4 + 2);
if (g > 100 && r > 100 && b < 43)
frames.data(i * 4 + 3) = 0;
ctx1.putImageData(frames,300,300);
return;
}
}); 

}

  </script>
<body onload="draw()">

 <canvas id="c1" width="300" height="300"></canvas>
 <canvas id="c2" width="300" height="300" ></canvas>
 <canvas id="c3" width="300" height="300"></canvas>
 <video id="video" src="https://developer.mozilla.org/samples/video/chroma-key/video.ogv"  width=video.width height= video.height controls="true"></video>
 </body>
A: 

You have to continuously call conts.drawImage

Example: http://jsfiddle.net/Dmy2E/

    function draw()
{
var ctx3 = document.getElementById('c3');
var ctx1 = document.getElementById('c1');
var conts = ctx3.getContext('2d');
var videos = document.getElementById('video');
var frames = conts.getImageData(0,0,300,300);
videos.addEventListener("play", function ()
{
conts.drawImage(videos, 0, 0, 300, 300);

setTimeout(arguments.callee, 0);

var l = frames.data.length/4;
for (var i = 0; i<1; i++){
var r = frames.data(i * 4 + 0);
var g = frames.data(i * 4 + 1);
var b = frames.data(i * 4 + 2);
if (g > 100 && r > 100 && b < 43)
frames.data(i * 4 + 3) = 0;
ctx1.putImageData(frames,300,300);
return;
}

}); 
}
antimatter15
WOW it was that simple of setting the setTimeout Thanks alot for the help
Jose Ortiz