无法在画布中加载图像(在ie8中使用Excanvas)

时间:2012-04-07 12:28:01

标签: javascript html5 canvas

我使用excanvas在ie8中使用canvas元素,但我无法在画布中加载图像。

我的代码是

var el = document.getElementById('cavasid');
G_vmlCanvasManager.initElement(el);
var context = el.getContext('2d');


 var img = new Image();
 img.onload = function() {
 context.drawImage(img, 0,0);
 };
 img.src = "jj.png";

1 个答案:

答案 0 :(得分:1)

确保在head部分添加了excanvas.js,然后尝试以下代码

  var el = document.getElementById('cavasid');
 if (typeof G_vmlCanvasManager != 'undefined') {
     el = G_vmlCanvasManager.initElement(el);
 }

 if (el.getContext) {
     var context = el.getContext('2d');


     var img = new Image();
     img.onload = function () {
         context.drawImage(img, 0, 0);
     };
     img.src = "jj.png";
 }

也为画布提供一些宽度和高度,如

<canvas id="cavasid" width="200" height="200"/>