The HTML5 Canvas provides you with the ability to draw lines, shapes, text, and images as well In this tutorial, we will cover the drawImage() method and show some examples of how this method can be used to draw images onto the HTML5 canvas.
Usage
To draw an image onto the HTML5 Canvas, we can use the drawImage() method which requires an image object and a destination point. The destination point is relative to the top left corner of the image. The drawImage() method requires an image object. In our JavaScript block, we must create an image and once it is loaded, we draw the image onto the HTML5 canvas.
Syntax
Draw the image onto the canvas.
context.drawImage(img,x,y);
Draw the image on the canvas, and specify the width and height of the image.
context.drawImage(img,x,y,width,height);
Crop the image and draw the cropped portion on the canvas.
context.drawImage(img,cx,cy,cwidth,cheight,x,y,width,height);
Examples
The first example is the most basic one where the image is simply drawn on the canvas element. The second example will draw the image according to the dimensions provided. The third example crops the image and draws the results.
Draw the image on the canvas, and specify width and height of the image.