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);
image
CanvasImageSource
), specifically, a
CSSImageValue
,
HTMLImageElement
,
SVGImageElement
,
HTMLVideoElement
,
HTMLCanvasElement
,
ImageBitmap
,或
OffscreenCanvas
.
sx
可选
image
to draw into the destination context.
sy
可选
image
to draw into the destination context.
sWidth
可选
image
to draw into the destination context. If not specified, the entire rectangle from the coordinates specified by
sx
and
sy
to the bottom-right corner of the image is used.
sHeight
可选
image
to draw into the destination context.
dx
image
.
dy
image
.
dWidth
可选
image
in the destination canvas. This allows scaling of the drawn image. If not specified, the image is not scaled in width when drawn.
dHeight
可选
image
in the destination canvas. This allows scaling of the drawn image. If not specified, the image is not scaled in height when drawn.
INDEX_SIZE_ERR
If the canvas or source rectangle width or height is zero.
INVALID_STATE_ERR
The image has no image data.
TYPE_MISMATCH_ERR
The specified source element isn't supported.
NS_ERROR_NOT_AVAILABLE
.complete === true
and
.onload
to determine when it is ready.
This example draws an image to the canvas using the
drawImage()
方法。
<canvas id="canvas"></canvas>
<div style="display:none;">
<img id="source"
src="https://yari-demos.prod.mdn.mozit.cloud/files/5397/rhino.jpg"
width="300" height="227">
</div>
The source image is taken from the coordinates (33, 71), with a width of 104 and a height of 124. It is drawn to the canvas at (21, 20), where it is given a width of 87 and a height of 104.
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const image = document.getElementById('source');
image.addEventListener('load', e => {
ctx.drawImage(image, 33, 71, 104, 124, 21, 20, 87, 104);
});
drawImage()
method uses the source element's
intrinsic size in CSS pixels
when drawing.
For example, if you load an
图像
and specify the optional size parameters in its
constructor
, you will have to use the
naturalWidth
and
naturalHeight
properties of the created instance to properly calculate things like crop and scale regions, rather than
element.width
and
element.height
. The same goes for
videoWidth
and
videoHeight
if the element is a
<video>
element, and so on.
<canvas id="canvas"></canvas>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const 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);
}
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of 'CanvasRenderingContext2D: drawImage' in that specification. |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
drawImage
|
Chrome 1 | Edge 12 | Firefox 1.5 | IE 9 | Opera 9 | Safari 2 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 |
| ImageBitmap as source image | Chrome Yes | Edge 79 | Firefox 42 | IE ? | Opera ? | Safari ? | WebView Android Yes | Chrome Android Yes | Firefox Android 42 | Opera Android ? | Safari iOS ? | Samsung Internet Android Yes |
| Smoothing when downscaling | Chrome Yes | Edge ≤79 |
Firefox
56
|
IE ? | Opera Yes | Safari ? | WebView Android Yes | Chrome Android Yes |
Firefox Android
56
|
Opera Android Yes | Safari iOS ? | Samsung Internet Android Yes |
| SVGImageElement as source image | Chrome 59 | Edge ≤79 | Firefox 56 | IE ? | Opera ? | Safari ? | WebView Android 59 | Chrome Android 59 | Firefox Android 56 | Opera Android ? | Safari iOS ? | Samsung Internet Android 7.0 |
完整支持
兼容性未知
见实现注意事项。
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
特性)。
drawImage()
will always use the source element's
intrinsic size in CSS pixels
when drawing, cropping, and/or scaling.
drawImage()
will ignore all EXIF metadata in images, including the Orientation. This behavior is especially troublesome on iOS devices. You should detect the Orientation yourself and use
rotate()
to make it right.
CanvasRenderingContext2D
CanvasRenderingContext2D
addHitRegion()
arc()
arcTo()
beginPath()
bezierCurveTo()
clearHitRegions()
clearRect()
clip()
closePath()
createImageData()
createLinearGradient()
createPattern()
createRadialGradient()
drawFocusIfNeeded()
drawImage()
drawWidgetAsOnScreen()
drawWindow()
ellipse()
fill()
fillRect()
fillText()
getImageData()
getLineDash()
getTransform()
isPointInPath()
isPointInStroke()
lineTo()
measureText()
moveTo()
putImageData()
quadraticCurveTo()
rect()
removeHitRegion()
resetTransform()
restore()
rotate()
save()
scale()
scrollPathIntoView()
setLineDash()
setTransform()
stroke()
strokeRect()
strokeText()
transform()
translate()