I'm trying to draw a video from video
element on canvas. I wrote the following JS code:
$(function () {
var video = $("#video");
video.bind("loadedmetadata", function () {
var vw = this.videoWidth;
var vh = this.videoHeight;
var canvas = $('#canvas');
canvas.width(vw).height(vh);
var context = canvas.getContext("2d");
video.addEventListener("play", function () { draw(video, context, vw, vh); }, false);
});
});
function draw(video, context, vw, vh)
{
if (video.paused || video.ended)
{
return false;
}
context.drawImage(video, 0, 0, vw, vh);
setTimeout(draw, 20, video, context, vw, vh);
}
This is my HTML:
<body>
<video id="video" autoplay controls>
<source src="src1.avi" />
<source src="src2.mov" />
<source src="src3.ogg" />
<source src="src4.avi" />
</video>
<canvas id="canvas">
Please update your browser.
</canvas>
JSFiddle here
I can see the video playing, but it's not being redrawn onto the canvas. Where's my mistake?