Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
2.00/5 (4 votes)
See more:
Been asked to draw an mp4 video on a canvas? Anyone know how to do this?
Posted
Comments
Sergey Alexandrovich Kryukov 6-Mar-13 16:11pm    
On HTML5 canvas? What it is suppose to mean? Do you even know what it is about? :-)
Ask the one who asked you. Wow!
—SA
Member 9889448 6-Mar-13 16:12pm    
The HTML5 <canvas> element is used to draw graphics, on the fly, via scripting (usually JavaScript).

The <canvas> element is only a container for graphics. You must use a script to actually draw the graphics.

Canvas has several methods for drawing paths, boxes, circles, characters, and adding images.


http://www.w3schools.com/html/html5_canvas.asp
Sergey Alexandrovich Kryukov 6-Mar-13 16:23pm    
OK, do you understand now that it's time to remove your question, or you want to wait for some up-votes from others? :-)
—SA
Andrews Smith 6-Mar-13 18:55pm    
You're making it seem like you can't play a video on a html5 canvas? But you can but the term is draw..
Sergey Alexandrovich Kryukov 6-Mar-13 19:26pm    
Yes. Your sample does not actually draw anything on Canvas. But I can admit that the contradiction is just the wordplay...

Here is how: HTML5 canvas is a pure vector-graphics tool. You questions sounds like you want to show video using this graphics. Formally, "draw on a canvas" really might be interpreted as you simply need to position video on some canvas graphics element. Even though the solution has nothing to do with this canvas (but it uses HTML5), formally it is "on a canvas". Sorry, I wasn't quite accurate, but I really though you are asking about the use of Canvas for showing video. You also could explain it more clearly...

Now, do you have a question?
—SA

1 solution

This is a way of drawing a video on a Canvas for playback, courtesy of oreilly.com

HTML
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CH6EX6: Basic HTML5 Load Video Onto The Canvas</title>
<script src="modernizr-1.6.min.js"></script>
<script type="text/javascript">
window.addEventListener('load', eventWindowLoaded, false);
var videoElement;
var videoDiv;
function eventWindowLoaded() {

   videoElement = document.createElement("video");
   videoDiv = document.createElement('div');
   document.body.appendChild(videoDiv);
   videoDiv.appendChild(videoElement);
   videoDiv.setAttribute("style", "display:none;");
   var videoType = supportedVideoFormat(videoElement);
   if (videoType == "") {
      alert("no video support");
      return;
   }
   videoElement.setAttribute("src", "muirbeach." + videoType);
   videoElement.addEventListener("canplaythrough",videoLoaded,false);

}

function supportedVideoFormat(video) {
   var returnExtension = "";
   if (video.canPlayType("video/webm") =="probably" || 
       video.canPlayType("video/webm") == "maybe") {
         returnExtension = "webm";
   } else if(video.canPlayType("video/mp4") == "probably" || 
       video.canPlayType("video/mp4") == "maybe") {
         returnExtension = "mp4";
   } else if(video.canPlayType("video/ogg") =="probably" || 
       video.canPlayType("video/ogg") == "maybe") {
         returnExtension = "ogg";
   }

   return returnExtension;

}

function canvasSupport () {
     return Modernizr.canvas;
}

function videoLoaded(event) {

   canvasApp();

}

function canvasApp() {

   if (!canvasSupport()) {
          return;
        }

function  drawScreen () {

      //Background
      context.fillStyle = '#ffffaa';
      context.fillRect(0, 0, theCanvas.width, theCanvas.height);
      //Box
      context.strokeStyle = '#000000';
      context.strokeRect(5,  5, theCanvas.width−10, theCanvas.height−10);
      //video
      context.drawImage(videoElement , 85, 30);

   }

   var theCanvas = document.getElementById("canvasOne");
   var context = theCanvas.getContext("2d");
   videoElement.play();

   setInterval(drawScreen, 33);

}

</script>

</meta></head>
<body>
<div style="position: absolute; top: 50px; left: 50px;">

<canvas id="canvasOne" width="500" height="300">
 Your browser does not support HTML5 Canvas.
</canvas>
</div>
</body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900