有关HTML5的drawImage()和ImageData的问题
为什么我在用完drawImage()之后脚本就不再执行getImageData()和putImageData()了呢?...
为什么我在用完drawImage()之后脚本就不再执行getImageData() 和putImageData()了呢?
展开
展开全部
通常不会. 但是在加载image的时候最好用onload事件判断图片是否加载完成. 然后再getImageData或者putImageData:
var canvas = document.getElementById('canvas');
canvas.width = 200;
canvas.height = 200;
var ctx = canvas.getContext('2d');
var imgDat;
var img = new Image();
img.onload = function() {
ctx.drawImage(img, 0, 0);
imgDat = ctx.getImageData(0, 0, canvas.width, canvas.height);
console.log(imgDat); // Output imageData;
}
img.src = 'comment.gif';
var canvas = document.getElementById('canvas');
canvas.width = 200;
canvas.height = 200;
var ctx = canvas.getContext('2d');
var imgDat;
var img = new Image();
img.onload = function() {
ctx.drawImage(img, 0, 0);
imgDat = ctx.getImageData(0, 0, canvas.width, canvas.height);
console.log(imgDat); // Output imageData;
}
img.src = 'comment.gif';
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询