Home >Web Front-end >JS Tutorial >Can You Query Individual Pixel Colors from an HTML Canvas?
Accessing Pixels in HTML Canvas
Can HTML Canvas objects be interrogated to retrieve the color of specific pixels?
Answer:
Yes, you can retrieve individual pixel values from a canvas using the W3C's pixel manipulation methods.
Example:
The example below demonstrates inverting the colors of an image on the canvas:
<code class="javascript">// Obtain the CanvasPixelArray for the specified coordinates and dimensions var imgd = context.getImageData(x, y, width, height); var pix = imgd.data; // Iterate over each pixel and invert its color for (var i = 0, n = pix.length; i </code>
The above is the detailed content of Can You Query Individual Pixel Colors from an HTML Canvas?. For more information, please follow other related articles on the PHP Chinese website!