Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML canvas drawImage() method

HTML canvas drawImage() method

Canvas Object Reference Canvas object

The image to use:

The Scream

Example

Draw the image onto the canvas:

YourbrowserdoesnotsupporttheHTML5canvastag.

JavaScript:

var c=document.getElementById("myCanvas");

var ctx=c.getContext("2d");

var img=document.getElementById("scream");

ctx.drawImage(img,10,10);

Try it »


Browser support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

Internet Explorer 9, Firefox, Opera, Chrome and Safari support the drawImage() method.

**Note:** Internet Explorer 8 and earlier versions do not support .


Definition and Usage

The drawImage() method draws an image, canvas, or video onto the canvas.

The drawImage() method can also draw parts of the image, and/or increase or decrease its size.

JavaScript syntax

Position the image on the canvas:

JavaScript syntax: context.drawImage(img,x,y);

Position the image on the canvas and specify its width and height:

JavaScript syntax: context.drawImage(img,x,y,width,height);

Clip the image and position the clipped part on the canvas:

JavaScript syntax: context.drawImage(img,sx,sy,swidth,sheight,x,y,width,height);

Parameter value

Parameters Description
img Specifies the image, canvas, or video to use.
sx Optional. The x-coordinate position where clipping starts.
sy Optional. The y-coordinate position where clipping starts.
swidth Optional. The width of the clipped image.
sheight Optional. The height of the clipped image.
x The x-coordinate position where the image is placed on the canvas.
y The y-coordinate position where the image is placed on the canvas.
width Optional. The width of the image to use (stretch or shrink the image).
height Optional. The height of the image to use (stretch or shrink the image).

More examples

Example

Position the image on the canvas, then specify its width and height:

YourbrowserdoesnotsupporttheHTML5canvastag.

JavaScript:

var c=document.getElementById("myCanvas");

var ctx=c.getContext("2d");

var img=document.getElementById("scream");

ctx.drawImage(img,10,10,150,180);

Try it »

Example

Clip the image and position the clipped part on the canvas:

YourbrowserdoesnotsupporttheHTML5canvastag.

JavaScript:

var c=document.getElementById("myCanvas");

var ctx=c.getContext("2d");

var img=document.getElementById("scream");

ctx.drawImage(img,90,130,50,60,10,10,50,60);

Try it »

Example

The video to use (please press play to start the demonstration):

Canvas:

yourbrowserdoesnotsupportthecanvastag

JavaScript (every 20 milliseconds, the code draws the current frame of the video):

var v=document.getElementById("video1");

var c=document.getElementById("myCanvas");

ctx=c.getContext('2d');

v.addEventListener('play',function() {var i=window.setInterval(function() {ctx.drawImage(v,5,5,260,125)},20);},false);

v.addEventListener('pause',function() {window.clearInterval(i);},false);

v.addEventListener('ended',function() {clearInterval(i);},false);

Try it »


Canvas Object Reference Canvas object