且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

getImageData总是返回0

更新时间:2023-01-12 17:25:23

在执行比较之前已加载并绘制图像。尝试此操作:

You are not waiting until your images have loaded and drawn before performing your comparison. Try this:

var img = new Image;
img.onload = function(){
  ctx1.drawImage(img,0,0);
  var img = new Image;
  img.onload = function(){
    ctx2.drawImage(img,0,0);
    // diff them here
  };
  img.src = 'cat.jpg';
};
img.src = 'cat.jpg';

如上所示,您应始终设置 src code> onload

As shown above, you should always set your src after your onload.