ImageData object is used to represent the color data on canvas.
ImageData has three parts:
The data property contains image data of the specified ImageData object.
Every pixel in an ImageData
object has four-element-array-like value, the RGBA values:
The color information is stored in an array from
the data
property of the ImageData
object.
width |
Yes | Yes | Yes | Yes | Yes |
The following code manipulate the ImageData object.
<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas" width="300" height="150"></canvas>
<script>
<!--from w ww . j a v a 2 s .co m-->
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var imgData = ctx.createImageData(100, 100);
var i;
for (i = 0; i < imgData.data.length; i += 4) {
imgData.data[i+0] = 200;
imgData.data[i+1] = 200;
imgData.data[i+2] = 200;
imgData.data[i+3] = 255;
}
ctx.putImageData(imgData, 10, 10);
</script>
</body>
</html>
The code above is rendered as follows:
The following code manipulate the ImageData object.
<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas" width="300" height="150"></canvas>
<script>
<!--from w w w. ja v a 2s . com-->
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var imgData = ctx.createImageData(100, 100);
console.log("Height of imgData is: " + imgData.height);
console.log("Width of imgData is: " + imgData.width);
</script>
</body>
</html>
The code above is rendered as follows: