我不知道为什么,但我的脚本为 alpha 通道返回了错误的值。
这就是我所拥有的:
function getPixel(x,y,px,py,i){
//user click: x y
//picture location: px py
//array key: i
//location of click has to be changed to be relevant to this temp canvas
//as image will now be at position 0,0
var x = Math.round(0 - (px - x) );
var y = Math.round(0 - (py - y) );
//temp canvas
var c = document.createElement('canvas');
var context = c.getContext('2d');
c.width = pArray[i].img.width;
c.height = pArray[i].img.height;
context.drawImage(pArray[i].img,0,0);
var d = context.getImageData(x,y,1,1);
if(d[3] != 0){
console.log('Not Alpha'); //always happens
} else {
console.log('Alpha'); // never happens
}
console.log(x + ', ' + y + ', ' + c.width + ', ' + c.height + ', ' + pArray[i].img.src);
}
我的控制台输出显示:
8, 42, 128, 128, [Full URL Hidden]/images/1.png
这也是我正在测试它的图像:
谁能看到任何明显的错误可以解释为什么 alpha 永远不等于 0 ?
JSFiddle 测试位置 x1 和 y1:http: //jsfiddle.net/darkyen/UCSU2/15/