The CanvasRenderingContext2D.drawImage() method of the Canvas 2D API provides different ways to draw an image onto the canvas.
void ctx.drawImage(image, dx, dy); void ctx.drawImage(image, dx, dy, dWidth, dHeight); void ctx.drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight);
imageCanvasImageSource), specifically, a CSSImageValue, an HTMLImageElement, an SVGImageElement, an HTMLVideoElement, an HTMLCanvasElement, an ImageBitmap, or an OffscreenCanvas.dximage.dyimage.dWidthimage in the destination canvas. This allows scaling of the drawn image. If not specified, the image is not scaled in width when drawn.dHeightimage in the destination canvas. This allows scaling of the drawn image. If not specified, the image is not scaled in height when drawn.sxsysWidthsx and sy to the bottom-right corner of the image is used.sHeightINDEX_SIZE_ERRINVALID_STATE_ERRTYPE_MISMATCH_ERRdrawImage methodThis is just a simple code snippet which uses the drawImage method.
<canvas id="canvas"></canvas>
<div style="display:none;">
<img id="source" src="https://mdn.mozillademos.org/files/5397/rhino.jpg"
width="300" height="227">
</div>
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var image = document.getElementById('source');
ctx.drawImage(image, 33, 71, 104, 124, 21, 20, 87, 104);
Edit the code below and see your changes update live in the canvas:
<canvas id="canvas" width="400" height="200" class="playable-canvas"></canvas> <div style="display:none;"> <img id="source" src="https://mdn.mozillademos.org/files/5397/rhino.jpg" width="300" height="227"> </div> <div class="playable-buttons"> <input id="edit" type="button" value="Edit" /> <input id="reset" type="button" value="Reset" /> </div> <textarea id="code" class="playable-code"> ctx.drawImage(image, 33, 71, 104, 124, 21, 20, 87, 104);</textarea>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var image = document.getElementById('source');
var textarea = document.getElementById("code");
var reset = document.getElementById("reset");
var edit = document.getElementById("edit");
var code = textarea.value;
function drawCanvas() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
eval(textarea.value);
}
reset.addEventListener("click", function() {
textarea.value = code;
drawCanvas();
});
edit.addEventListener("click", function() {
textarea.focus();
})
textarea.addEventListener("input", drawCanvas);
window.addEventListener("load", drawCanvas);
The drawImage() method uses the source element's intrinsic size in CSS pixels when drawing.
For example, if an image was loaded using the optional size parameters in the constructor you will have to use the naturalWidth and naturalHeight properties of its instance (or videoWidth and videoHeight if the element is a <video> element, etc.) to properly calculate things like crop and scale regions, rather than element.width and element.height.
<canvas id="canvas"></canvas>
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var image = new Image(60, 45); // using optional size for image
image.onload = drawImageActualSize; // draw when image has loaded
// load an image of intrinsic size 300x227 in CSS pixels
image.src = 'https://mdn.mozillademos.org/files/5397/rhino.jpg';
function drawImageActualSize() {
// use the intrinsic size of image in CSS pixels for the canvas element
canvas.width = this.naturalWidth;
canvas.height = this.naturalHeight;
// will draw the image as 300x227 ignoring the custom size of 60x45
// given in the constructor
ctx.drawImage(this, 0, 0);
// To use the custom size we'll have to specify the scale parameters
// using the element's width and height properties - lets draw one
// on top in the corner:
ctx.drawImage(this, 0, 0, this.width, this.height);
} | Specification | Status | Comment |
|---|---|---|
| HTML Living Standard The definition of 'CanvasRenderingContext2D.drawImage' in that specification. | Living Standard |
| Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
ImageBitmap as source image | (Yes) | No support | 42 (42) | ? | ? | ? |
SVGImageElement as source image | 59 | ? | 56 (56) | ? | ? | ? |
| Smoothing when downscaling | (Yes) | ? | 56 (56)[1] | ? | (Yes) | ? |
| Feature | Android Webview | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
ImageBitmap as source image | (Yes) | (Yes) | No support | 42.0 (42) | ? | ? | ? |
SVGImageElement as source image | 59 | 59 | ? | 56.0 (56) | ? | ? | ? |
| Smoothing when downscaling | (Yes) | (Yes) | ? | 56.0 (56)[1] | ? | (Yes) | ? |
[1] See bug 1360415 for details.
sw and sh was added in Gecko 5.0 (Firefox 5.0 / Thunderbird 5.0 / SeaMonkey 2.2).drawImage() handles negative arguments in accordance with the specification, by flipping the rectangle around the appropriate axis.null or undefined image when calling or drawImage() correctly throws a TYPE_MISMATCH_ERR exception starting with (Firefox 5.0 / Thunderbird 5.0 / SeaMonkey 2.2).drawImage() only works correctly on an HTMLVideoElement when its HTMLMediaElement.readyState is greater than 1 (i.e, seek event fired after setting the currentTime property)drawImage() will always use the source element's intrinsic size in CSS pixels when drawing, cropping and/or scaling. For example, if an image is loaded using the constructor's optional size parameters the actual intrinsic size of the bitmap in CSS pixels will still be used.drawImage() will ignore all EXIF metadata in images, including the Orientation. This behavior is espacially troublesome on iOS devices. You should detect the Orientation yourself and use rotate() to make it right.CanvasRenderingContext2D.
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawImage