MickDom MickDom - 1 year ago 86
Javascript Question

Check to see if pixel is black on HTML5 Canvas

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.

Answer Source
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