Home > Article > Web Front-end > How to Get the Pixel Color Under the Mouse on a Canvas in JavaScript?
Getting Pixel Color from Canvas on Mouse Move
The mousemove event can be used to track the position of the user's mouse as it moves over a canvas element. By getting the mouse position relative to the canvas and using the getImageData() method, it's possible to retrieve the RGB values of the pixel under the mouse cursor.
To get the pixel color under the mouse in JavaScript, follow these steps:
Example Code
Here's a complete example that demonstrates how to get the pixel color under the mouse:
This example will log the RGB color of the pixel under the mouse cursor to the console when the mouse moves over the canvas.
The above is the detailed content of How to Get the Pixel Color Under the Mouse on a Canvas in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!