Cannot read property 'getContext' of null和Failed to execute 'getContext' on 'HTMLCanvasElement':

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36688143/article/details/82022231

1、chrome/极速360浏览器报错:Cannot read property 'getContext' of null

filefox报错:TypeError: c is null

IE Edge:控制台没有报错,但是canvas图像也不显示

IE10/IE9报错:SCRIPT5007: 无法获取未定义或 null 引用的属性“getContext”

解决方法:

 将绘制方法,即绘制图片的<script>的js方法,放在body<canvas></canvas>内容下面

       因为Chrome下需要文档载入完成后才能获得canvas对象。

ps:

修正问题后,IE8报错:SCRIPT438: 对象不支持“getContext”属性或方法

IE8及以下的浏览器不支持canvas.

2、Uncaught TypeError: Failed to execute 'getContext' on 'HTMLCanvasElement': 1 argument required, but only 0 present
出错原因:有以下代码

if(document.getElementById("myCanvas").getContext  ){
}


解决办法:getContext方法需要接收参数:2d,即

if(document.getElementById("myCanvas").getContext("2d")  ){
}

猜你喜欢

转载自blog.csdn.net/qq_36688143/article/details/82022231