Reputation: 157
I'm trying to find if a pixel is black or not on a canvas
var check = function(player,keyPressed) {
//series of ifs to determine what pixel to check.
}
I would need to return either true or false if the pixel is false, I've tried getImageData but I wasn't able to figure out how to use it properly.
Upvotes: 3
Views: 2255
Reputation: 93203
var canvas= document.getElementById('myCanv');
var pixelData = canvas.getContext('2d').getImageData(event.clientX, event.clientY, 1, 1).data;
That is it !!
Of course, Assuming that , you have :
<canvas id="myCanv"></canvas>
Then :
function isBlack(dataPixel){
if(dataPixel[0]==dataPixel[1] && dataPixel[1]==dataPixel[2] && dataPixel[2]===0 ){
return true
}
}
http://jsfiddle.net/abdennour/4kdLfooj/11/
Upvotes: 6