CanvasRenderingContext2D.createImageData()
method of the Canvas 2D API creates a new, blank
ImageData
object with the specified dimensions. All of the pixels in the new object are transparent black.
ImageData ctx.createImageData(width, height); ImageData ctx.createImageData(imagedata);
width
ImageData
object. A negative value flips the rectangle around the vertical axis.
height
ImageData
object. A negative value flips the rectangle around the horizontal axis.
imagedata
ImageData
object from which to copy the width and height. The image itself is
not
copied.
新的
ImageData
object with the specified width and height. The new object is filled with transparent black pixels.
IndexSizeError
width
or
height
arguments is zero.
This snippet creates a blank
ImageData
对象使用
createImageData()
方法。
<canvas id="canvas"></canvas>
The generated object is 100 pixels wide and 50 pixels tall, making 5,000 pixels in all. Each pixel within an
ImageData
object consists of four array values, so the object's
data
property has a length of 4 × 5,000, or 20,000.
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const imageData = ctx.createImageData(100, 50);
console.log(imageData);
// ImageData { width: 100, height: 50, data: Uint8ClampedArray[20000] }
This example creates and fills a new
ImageData
object with purple pixels.
<canvas id="canvas"></canvas>
Since each pixel consists of four values, the
for
loop iterates by multiples of four. The array values associated with each pixel are R (red), G (green), B (blue), and A (alpha), in that order.
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const imageData = ctx.createImageData(100, 100);
// Iterate through every pixel
for (let i = 0; i < imageData.data.length; i += 4) {
// Modify pixel data
imageData.data[i + 0] = 190; // R value
imageData.data[i + 1] = 0; // G value
imageData.data[i + 2] = 210; // B value
imageData.data[i + 3] = 255; // A value
}
// Draw image data to the canvas
ctx.putImageData(imageData, 20, 20);
For more examples using
createImageData()
和
ImageData
object, see
Pixel manipulation with canvas
and
ImageData.data
.
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of 'CanvasRenderingContext2D.createImageData' in that specification. |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
createImageData
|
Chrome Yes | Edge 12 | Firefox 2 | IE Yes | Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes | Firefox Android 4 | Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
完整支持
createImageData()
now correctly returns at least one pixel's worth of image data if a rectangle smaller than one pixel is specified.
createImageData()
now properly throws a
NOT_SUPPORTED_ERR
异常。
createImageData()
now handles negative arguments in accordance with the specification, by flipping the rectangle around the appropriate axis.
CanvasRenderingContext2D
ImageData
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()