I Just want to get pixel color on mouse move in jquery or javascript. Is there any possible way to get that as I don't want to use canvas or any third party plugin. I need color of that pixel on my cursor position.
Any one please help?
I Just want to get pixel color on mouse move in jquery or javascript. Is there any possible way to get that as I don't want to use canvas or any third party plugin. I need color of that pixel on my cursor position.
Any one please help?
If all you want is background color you can use document.elementFromPoint()
and getComputedStyle()
.
For more advanced issues like pixel color in an image this will not work
window.addEventListener('click', e=>{
const {clientX:x, clientY:y} = e,
el = document.elementFromPoint(x, y),
style = window.getComputedStyle(el);
console.log(style.backgroundColor)
});
initDemo()
function initDemo(){
const colors =['red','blue', 'purple','green','grey']
document.querySelectorAll('.box').forEach((el,i)=>{
el.style.backgroundColor = colors[i]
});
}
.box{ border:2px solid #ccc; width:50px; margin:30px; height:50px; display:inline-block; color:yellow}
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>