JS不能调用方法绘制空的图像

JS cant call method drawImage of null

本文关键字:图像 绘制 方法 不能 调用 JS      更新时间:2023-09-26

我得到了这个 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的脚本读取中)它不能调用方法drawImage的null,我该如何解决它?

您需要传入字符串"2d"getContext()

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