Computer >> Computer tutorials >  >> Programming >> HTML

How to use images with HTML5 canvas?


The HTML5 <canvas> tag is used to draw graphics, animations, etc. using scripting. It is a new tag introduced in HTML5. To use images with HTML5 canvas, use the drawImage() method. This method draws the given image onto the canvas.

How to use images with HTML5 canvas?

You can try to run the following code to learn how to use images with HTML Canvas. Here, the image is a reference to an image or canvas object. x and y form the coordinate on the target canvas where our image should be placed.

Example

<!DOCTYPE HTML>
<html>
   <head>
      <script type="text/javascript">
         function drawShape() {
            // get the canvas element using the DOM
            var canvas = document.getElementById('mycanvas');

            // Make sure we don't execute when canvas isn't supported
            if (canvas.getContext) {

               // use getContext to use the canvas for drawing
               var ctx = canvas.getContext('2d');

               // Draw shapes
               var img = new Image();
               img.src = '/images/backdrop.jpg';
               img.onload = function() {
                  ctx.drawImage(img,0,0);
                  ctx.beginPath();
                  ctx.moveTo(30,96);
                  ctx.lineTo(70,66);
                  ctx.lineTo(103,76);
                  ctx.lineTo(170,15);
                  ctx.stroke();
               }
            } else {
               alert('You need Safari or Firefox 1.5+ to see this demo.');
            }
         }
      </script>
   </head>

   <body onload="drawShape();">
      <canvas id="mycanvas"></canvas>
   </body>
</html>