Sometimes, we want to get a pixel from HTML Canvas with JavaScript.
In this article, we’ll look at how to get a pixel from HTML Canvas with JavaScript.
How to get a pixel from HTML Canvas with JavaScript?
To get a pixel from HTML Canvas with JavaScript, we use the canvas context getImageData
method.
For instance, we write
const [r, g, b] = context.getImageData(x, y, 1, 1).data;
to call the canvas context.getImageData
method with the x
and y
coordinates of the pixel we want to get.
Then we get the r
, g
and b
values of the pixel from the data
array property.
Conclusion
To get a pixel from HTML Canvas with JavaScript, we use the canvas context getImageData
method.