CanvasRenderingContext2D
方法
getImageData()
of the Canvas 2D API returns an
ImageData
object representing the underlying pixel data for a specified portion of the canvas.
This method is not affected by the canvas's transformation matrix. If the specified rectangle extends outside the bounds of the canvas, the pixels outside the canvas are transparent black in the returned
ImageData
对象。
注意:
Image data can be painted onto a canvas using the
putImageData()
方法。
You can find more information about
getImageData()
and general manipulation of canvas contents in
Pixel manipulation with canvas
.
ctx.getImageData(sx, sy, sw, sh);
sx
ImageData
will be extracted.
sy
ImageData
will be extracted.
sw
ImageData
will be extracted. Positive values are to the right, and negative to the left.
sh
ImageData
will be extracted. Positive values are down, and negative are up.
ImageData
object containing the image data for the rectangle of the canvas specified. The coordinates of the rectangle's top-left corner are
(sx, sy)
, while the coordinates of the bottom corner are
(sx + sw, sy + sh)
.
IndexSizeError
sw
or
sh
are zero.
SecurityError
SecurityError
being thrown in this situation, configure CORS to allow the source image to be used in this way. See
Allowing cross-origin use of images and canvas
.
This example draws a rectangle, and then uses
getImageData()
to grab a portion of the canvas.
<canvas id="canvas"></canvas>
The object retrieved by
getImageData()
has a width of 200 and a height of 100, for a total of 20,000 pixels. Of those pixels, most are either transparent or taken from off the canvas; only 5,000 of them are opaque black (the color of the drawn rectangle).
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
ctx.rect(10, 10, 100, 100);
ctx.fill();
let imageData = ctx.getImageData(60, 60, 200, 100);
ctx.putImageData(imageData, 150, 10);
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of 'CanvasRenderingContext2D.getImageData' in that specification. |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
getImageData
|
Chrome 1 | Edge 12 |
Firefox
2
|
IE 9 | Opera 9.5 | Safari 4 | WebView Android 1 | Chrome Android 18 |
Firefox Android
4
|
Opera Android 10.1 | Safari iOS 3.2 | Samsung Internet Android 1.0 |
完整支持
见实现注意事项。
CanvasRenderingContext2D
ImageData
对象
CanvasRenderingContext2D.putImageData()
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()