I am trying to draw a video on canvas. To do this, I capture the onMouseDown and onMouseUp events in Javascript to get the x and y coordinates of each event (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 one I create must be stopped, and the new one must be played. Two problems:
1) the video does not play (the function only draws the first frame), but its sound does
2) how can I delete videos previously stopped?
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/
source share