有时HTML5画布工作并显示图像,有时它不在IE9上

时间:2013-02-10 06:32:03

标签: javascript html canvas

有时,HTML5画布会工作并显示图像,有时在IE9上不会显示此网页:https://www.barnwellmd.com/PainDiagram/PainDiagram.html。它总是适用于Firefox和Chrome。

谁能看到我做错了什么?

以下是加载图片的js代码:

function init () {
// Find the canvas element.
canvas = document.getElementById('imageView');
if (!canvas) {
  alert('Error: I cannot find the canvas element!');
  return;
}

if (!canvas.getContext) {
  alert('Error: no canvas.getContext!');
  return;
}

// Get the 2D canvas context.
context = canvas.getContext('2d');
if (!context) {
  alert('Error: failed to getContext!');
  return;
}
var img=new Image();
img.onload = function(){
context.drawImage(img,0,0);
imageData = context.getImageData(0, 0, 700, 643);
pixels = imageData.data;
};
img.src="PainDiagram.png";
    ...

1 个答案:

答案 0 :(得分:0)

我想如果你交换这两个代码块,就像我在下面做的那样,它应该得到修复:

// Get the 2D canvas context.
context = canvas.getContext('2d');
if (!context) {
  alert('Error: failed to getContext!');
  return;
}


if (!canvas.getContext) {
  alert('Error: no canvas.getContext!');
  return;
}