Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw video on canvas HTML5

I'm trying to draw a video on a canvas. To achieve this I capture the onMouseDown and onMouseUp events in Javascript to get the x and y coordinates of each event (that I need to set the position, width and height of the video inside the canvas).

Therefore, every time I draw a video on the canvas, the previous I create should be stopped and the new one has to be played. Two problems:

1) the video doesn't play (the function only draws the first frame), but his audio does

2) how can I get previously drawn videos to stop?

Demo: http://jsfiddle.net/e3c3kore/

<body>
    <canvas id="canvas" width="800" height="600"></canvas>
</body>



var canvas, context, xStart, yStart, xEnd, yEnd;

canvas = document.getElementById("canvas");
context = canvas.getContext("2d");
canvas.addEventListener("mousedown", mouseDown);
canvas.addEventListener("mouseup", mouseUp);

function mouseDown(e) {
    xStart = e.offsetX;
    yStart = e.offsetY;
}

function mouseUp(e) {
    xEnd = e.offsetX;
    yEnd = e.offsetY;
    if (xStart != xEnd && yStart != yEnd) {
    var video = document.createElement("video");
                        video.src = "http://techslides.com/demos/sample-videos/small.mp4";
                        video.addEventListener('loadeddata', function() {
                            video.play();
                            context.drawImage(video, xStart, yStart, xEnd-xStart, yEnd-yStart);
                        });
    }
}

Demo: http://jsfiddle.net/e3c3kore/

like image 336
user3673449 Avatar asked Nov 20 '15 19:11

user3673449


People also ask

How do you add a video in HTML5?

When you click the share button, a share panel will open displaying some more buttons. Now click on the Embed button, it will generate the HTML code to directly embed the video into the web pages. Just copy and paste that code into your HTML document where you want to display the video and you're all set.

What is the correct format for using video in an HTML5 Canvas document?

Supported Video Formats The Canvas media player supports H. 264 video playback.


1 Answers

You need to set up a continuous rendering. You are only rendering the first frame of the video. Canvas is dumb and does not know about videos. You have just dumped pixels from the video onto the canvas. You need to update the canvas continuously.

The best way is to use requestAnimationFrame this makes sure everything is synced up with the browsers own rendering.

In the example below the rendering is started when the video is loaded. Be sure to end the previous updates if you load a second video.

var canvas = document.getElementById("canV");
var ctx = canvas.getContext("2d");

var video = document.createElement("video");
video.src = "http://techslides.com/demos/sample-videos/small.mp4";
video.addEventListener('loadeddata', function() {
  video.play();  // start playing
  update(); //Start rendering
});




function update(){
  ctx.drawImage(video,0,0,256,256);   
  requestAnimationFrame(update); // wait for the browser to be ready to present another animation fram.       
}
#canV {
  width:256px;
  height:256px;
}
<canvas id="canV" width=256 height=256></canvas>
like image 145
Blindman67 Avatar answered Oct 19 '22 02:10

Blindman67