您的位置:首页 > 其它

Chrome中canvas报Cannot call method 'getContext' of null

2012-08-17 17:04 295 查看
 今天在看HTML5 的CANVAS,找了一段简单的代码,敲好后,用CHROME打开,竟然报出Cannot call method 'getContext' of null 这个错误,就是说getContext这个方法没有,唉?我就纳闷啦,按理说chrome对html5支持的很好啊,应该不会有这种错误啊,查了些资料也没找到,代码如下
<html><head><script type="text/javascript">var canvas=document.getElementById("screen");var ctx=canvas.getContext("2d");ctx.fillStyle="#f00";ctx.fillRect(30,40,80,100);</script></head><body onload="onReady()"><canvas id="screen" width="600" height="400"></canvas></body></html>
后来,我想了很久,觉得是不是因为js先于页面载入,还没有canvas属性啊,于是改改看
<html><head><script type="text/javascript">function onReady(){var canvas=document.getElementById("screen");var ctx=canvas.getContext("2d");ctx.fillStyle="#f00";ctx.fillRect(30,40,80,100);}</script></head><body onload="onReady()"><canvas id="screen" width="600" height="400"></canvas></body></html>
看来就是这个问题,要在页面载入后才能获得canvas对象,这个问题在firefox不会出现,看来是做了优化,反正还是注意吧,小问题
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐