--- title: CanvasRenderingContext2D.drawImage() slug: Web/API/CanvasRenderingContext2D/drawImage tags: - API - Canvas - CanvasRenderingContext2D - Refer translation_of: Web/API/CanvasRenderingContext2D/drawImage ---
Canvas 2D API 中的 CanvasRenderingContext2D
.drawImage()
方法提供了多种方式在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
sx
{{optional_inline}}image
的矩形(裁剪)选择框的左上角 X 轴坐标。sy
{{optional_inline}}image
的矩形(裁剪)选择框的左上角 Y 轴坐标。sWidth
{{optional_inline}}image
的矩形(裁剪)选择框的宽度。如果不说明,整个矩形(裁剪)从坐标的sx
和sy
开始,到image
的右下角结束。sHeight
{{optional_inline}}image
的矩形(裁剪)选择框的高度。dx
image
的左上角在目标canvas上 X 轴坐标。dy
image
的左上角在目标canvas上 Y 轴坐标。dWidth
{{optional_inline}}image
在目标canvas上绘制的宽度。 允许对绘制的image
进行缩放。 如果不说明, 在绘制时image
宽度不会缩放。dHeight
{{optional_inline}}image
在目标canvas上绘制的高度。 允许对绘制的image
进行缩放。 如果不说明, 在绘制时image
高度不会缩放。INDEX_SIZE_ERR
INVALID_STATE_ERR
TYPE_MISMATCH_ERR
NS_ERROR_NOT_AVAILABLE
.complete === true
和.onload
确定何时准备就绪。drawImage
方法这是一段使用 drawImage
方法的简单的代码片段。
<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);
<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>
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); });
{{ EmbedLiveSample('Drawing_an_image_to_the_canvas', 700, 180) }}
drawImage()
方法在绘制时使用源元素的CSS大小。
例如,如果加载图像并在其构造函数中指定可选的大小参数,则必须使用所创建实例的naturalWidth
和naturalHeight
属性来正确计算裁剪和缩放区域等内容,而不是element.width
和element.height
。如果元素是{{htmlelement("video")}} 元素,则videoWidth
和videoHeight
也是如此,依此类推。
<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);
}
<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); }
{{EmbedLiveSample('Understanding_source_element_size', 700, 260)}}
Specification | Status | Comment |
---|---|---|
{{SpecName('HTML WHATWG', "scripting.html#dom-context-2d-drawimage", "CanvasRenderingContext2D.drawImage")}} | {{Spec2('HTML WHATWG')}} |
{{Compat("api.CanvasRenderingContext2D.drawImage")}}
drawImage()
按照规范处理负参数,沿着合适的轴翻转矩形。null
或者 undefined
图像时,会抛出 TYPE_MISMATCH_ERR
异常。drawImage()
需要在{{domxref("HTMLVideoElement")}}工作时,仅当{{domxref("HTMLMediaElement.readyState")}}大于1时drawImage()
才能正常工作。drawImage()
将始终使用CSS像素中源元素的固有尺寸。drawImage()
将忽略图像中的所有EXIF元数据,包括方向。 此行为在iOS设备上尤其麻烦。 您应该自己检测方向并使用rotate()
使其正确。