我试图找出画布上是否有黑色像素
var check = function(player,keyPressed) {
//series of ifs to determine what pixel to check.
}
如果像素为假,我需要返回true或false,我已尝试过getImageData但我无法弄清楚如何正确使用它。
答案 0 :(得分:5)
var canvas= document.getElementById('myCanv');
var pixelData = canvas.getContext('2d').getImageData(event.clientX, event.clientY, 1, 1).data;
就是这样!!
当然,假设你有:
<canvas id="myCanv"></canvas>
然后:
function isBlack(dataPixel){
if(dataPixel[0]==dataPixel[1] && dataPixel[1]==dataPixel[2] && dataPixel[2]===0 ){
return true
}
}