This topic describes how to obtain a single pixel in HTML5canvas. Through the ImageData object, we can read and write a data array to manipulate pixel data. After pixel operations are completed, if you want to display them, you need to copy these pixels to the canvas ....,. We can directly obtain a single pixel from the HTML5 canvas. Through the ImageData object, we can read and write a data array to manipulate pixel data. After pixel operations are completed, if you want to display them, you need to copy these pixels to the canvas.
Create an ImageData object
To create an ImageData object, you can use the createImageData () method in the 2D context.
var canvas = document.getElementById("ex1");var context = canvas.getContext("2d"); var width = 100;var height = 100;var imageData = context.createImageData(width, height);
The ImageData object represents the underlying pixel data of a certain area in the canvas. It contains three read-only attributes:
-
Width: The image width, in pixels.
-
Height: The image height, in pixels.
-
Data: A one-dimensional array containing pixel values.
In the preceding example, a x pixel ImageData object is created.
Manage pixels
Each pixel in the data array contains 4 bytes. That is to say, each pixel is represented by four bytes, and each byte represents red, green, blue, and a transparency alpha channel (RGBA) respectively ). The color of a pixel is determined by the final color obtained by the combination of red, green, and blue. The transparency alpha channel determines the transparency of this pixel. The values of the red, green, blue, and alpha channels are between 0 and 25. This is the same principle as the RGB color of light in photoshop.
To read a pixel value, you can use the following code:
Var pixelIndex = 0; var red = imageData. data [pixelIndex]; // red var green = imageData. data [pixelIndex + 1]; // green var blue = imageData. data [pixelIndex + 2]; // blue var alpha = imageData. data [pixelIndex + 3]; // transparency
If you want to receive the following pixel values, you can increase the value of pixelIndex to a multiple of 4. You can use the following method to calculate the index value of a given pixel.
var index = 4 * (x + y * width);
In the preceding statement, x and y indicate the coordinates of the pixel in the Pixel Grid. The pixels in the data array are initialized to a long pixel sequence grid. It starts from the upper left corner and moves forward. When the end of a row is reached, it is displayed from the next row.
The following image is a 20-pixel array of 8-pixel high ImageData pixels ., The sequence starts from the upper left corner and moves to the right. When the maximum value of a row is reached, a new line is displayed.
Copy pixel to canvas
After pixel operations are completed, you can use the putImageData () function in the 2D context to copy them to the canvas. The putImageData () function has two formats. The first format is to copy all pixels to the canvas. The following is an example code:
var canvasX = 25;var canvasY = 25; context.putImageData(imageData, canvasX, canvasY);
The canvasX and canvasY parameters are the x and y coordinates of the inserted pixels on the canvas.
The putImageData () function in the second format can copy pixels in a rectangle area to the canvas. The following is an example code:
var canvasX = 25;var canvasY = 25;var sx = 0;var sy = 0;var sWidth = 25;var sHeight = 25;context.putImageData(imageData, canvasX, canvasY, sx, sy, sWidth, sHeight);
The sx and sy parameters (sourceX and sourceY) are the x and y coordinates in the upper left corner of the rectangle.
The sWidth and sHeight parameters (sourceWidth and sourceHeight) are the width and height of the rectangle.
Retrieve pixels from canvas
We can also get the pixels of a rectangle area from the canvas to an ImageData object. You can use the getImageData function to complete this operation. For example, the following code:
var x = 25;var y = 25;var width = 100;var height = 100;var imageData2 = context.getImageData(x, y, width, height);
The x and y parameters are the coordinates of the upper left corner of the rectangle obtained from the canvas.
The width and height parameters are the width and height of the rectangle obtained from the canvas.
The above is the HTML5 CANVAS: pixel processing content. For more information, see PHP Chinese Network (www.php1.cn )!