JS无法调用方法drawImage为null

时间:2013-10-11 14:45:07

标签: javascript html5 canvas

我收到了这个lil文件

/* WaterBall API notification model by Makerimages*/
function WBNotification(x,y,icon,text)
{
this.background="/WBGraphics/WBNotification.png";
this.x=x;
this.y=y;
this.icon=icon;
this.text=text;






this.draw=function()
{

var canvas = document.createElement('canvas');
canvas.id="WBNotificationArea";
canvas.width=88;
canvas.height =43;
document.body.appendChild(canvas);
cvs=document.getElementById("WBNotificationArea");
var context=cvs.getContext();
context.drawImage(this.background,this.x,this.y);
}
 return this;
}

我就像这样使用它

site=new waterContainer();
site.notifications[0]=new WBNotification(10,10,null,"hello there!");
site.notifications[0].draw()

一切正常,但是this.draw()说(在chrome的脚本中读取东西)它无法调用null的方法drawImage,我该如何解决?

1 个答案:

答案 0 :(得分:3)

您需要将字符串"2d"传递给getContext()

如果提供的上下文类型不可用,函数将返回null