你好我有这个脚本,我用来检查图像的透明像素,但当我尝试链接图像的html src =" filename.png"脚本不工作,没有找到任何透明像素。如果我为图像输入base64编码,该脚本工作正常。理想情况下,我希望只选择链接目录中的图像。你能指出我需要走的方向吗?感谢
<img src="WORKS ONLY BASE64" alt="Sample image" id="img" />
(function () {
var img = new Image(),
output = document.getElementById('transparent'),
bImg = document.getElementById('img');
img.src = bImg.src;
function isTransparent(e) {
var x = e.pageX - this.offsetLeft,
y = e.pageY - this.offsetTop;
var canvas = document.getElementById('imgcheck-canvas') || (function (_this) {
var e = document.createElement('canvas');
e.setAttribute('width', _this.width);
e.setAttribute('height', _this.height);
e.setAttribute('id', _this.id + '-canvas');
e.setAttribute('style', 'display:none;');
document.body.appendChild(e);
var cx = e.getContext('2d');
cx.drawImage(_this, 0, 0, _this.width, _this.height);
return e;
})(this);
if (canvas.getContext === undefined) {
return false;
}
var ctx = canvas.getContext('2d');
output.innerHTML = ((ctx.getImageData(x, y, 1, 1).data[3] === 0) ? true : false);
}
function init() {
console.log('init');
bImg.addEventListener('mousemove', isTransparent, true);
}
img.addEventListener('load', init, true);
})();