How can I use JavaScript or jQuery to read a pixel of an image when user clicks it?

If you can draw the image in a canvas element then you can use the getImageData method to return an array containing RGBA values.

var img = new Image();
img.src="https://stackoverflow.com/questions/1041399/image.jpg";
var context = document.getElementById('canvas').getContext('2d');
context.drawImage(img, 0, 0);
data = context.getImageData(x, y, 1, 1).data;

Leave a Comment