The 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.height
ImageData
object.imagedata
ImageData
object from which to copy the width and height. The image itself is not copied.A new ImageData
object with the specified width and height. The new object is filled with transparent black pixels.
IndexSizeError
createImageData
methodThis is just a simple code snippet which uses the createImageData
method. For more information, see Pixel manipulation with canvas and the ImageData
object.
<canvas id="canvas"></canvas>
var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); ctx.rect(10, 10, 100, 100); ctx.fill(); console.log(ctx.createImageData(100, 100)); // ImageData { width: 100, height: 100, data: Uint8ClampedArray[40000] }
Specification | Status | Comment |
---|---|---|
WHATWG HTML Living Standard The definition of 'CanvasRenderingContext2D.createImageData' in that specification. | Living Standard |
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
Feature | Android | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
createImageData()
now correctly return 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
exception.createImageData()
now handle negative arguments in accordance with the specification, by flipping the rectangle around the appropriate axis.CanvasRenderingContext2D
.ImageData
© 2005–2017 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/createImageData