I need to get the color of any pixel my mousepointer is currently hovering. I found several solutions for canvas elements, but none for a background image defined in CSS for a element.
How can I achieve this?
I need to get the color of any pixel my mousepointer is currently hovering. I found several solutions for canvas elements, but none for a background image defined in CSS for a element.
How can I achieve this?
Combining the answers from Get a CSS value with JavaScript and How to get a pixel's x,y coordinate color from an image?, I was able to simulate what you are looking for: JSFiddle
I retrieved the computed style (
var style = window.getComputedStyle(div);
) outside of the mouse move function for performance reasons, but if the background image is going to change dynamically, then it may need to be moved into the function.There are some possible browser constraints for using
getComputedStyle
.SCALING You could try editing the code to adjust for the scale:
This also includes a change to the CSS: JSFiddle