0
我正在嘗試使用HTML5畫布檢測圖像是否具有透明度。這是我的代碼,它是基於邏輯我已經在互聯網上找到用於檢測使用畫布(例如,this question)透明像素:使用HTML5畫布檢測圖像透明度有誤報
//note: this happens during the onload of the element
var found = false;
if((element.src).indexOf('.png') || element.src.indexOf('.tif')) {
var c = document.createElement('canvas');
var ctx = c.getContext('2d');
ctx.drawImage(element,0,0);
var imageData = ctx.getImageData(0, 0, element.width, element.height),
data = imageData.data;
var i = data.length/4 - 1;
do {
if(data[(i*4) + 3] < 255) {
found = true;
}
} while (i-- && !found);
}
return found;
的問題是,即使當我把在不一個PNG有任何透明度,它都會返回true。我用一個計數器進行調查:
console.log('total: '+i);
do {
if(data[(i*4) + 3] === 0) {
//found = true;
found++;
}
} while (i-- /*&& !found*/);
console.log('found: '+found);
//result for http://www.fnordware.com/superpng/pnggrad8rgb.png:
//total: 89999
//found: 45000
我的問題是,我該如何可靠地檢測PNG和TIF文件中的透明度?
設置畫布的大小的工作。跨域問題不適用(尚)。謝謝。 – miyasudokoro 2014-09-22 20:03:21