我正在使用 HTML5 Canvas,javascript。我在 html5 canvas 和 javascript 的帮助下制作了一个页面..但是当我运行它时它给了我这个错误
("Error=Unable to get the value of the property 'getContext': object is null or undefined")
但它不会终止程序..程序运行良好。
function draw(canvas0) {
var canvas = canvas0;
options = null;
// Canvas good?
if (canvas !== null && canvas.getContext)
}
function initCanvas() {
var canvas1 = document.getElementById('tutorial');
var canvas2 = document.getElementById('tutorial1');
var canvas3 = document.getElementById('tutorial2');
var canvas4 = document.getElementById('tutorial3');
var canvas5 = document.getElementById('tutorial4');
draw(canvas1);
draw(canvas2);
draw(canvas3);
draw(canvas4);
draw(canvas5);
}
这是我获取 id 并返回这些值的地方
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta charset="utf-8" />
<title>Speedometer HTML5 Canvas</title>
<script src="script copy.js">
</script>
</head>
<body onload='initCanvas();'>
<canvas id="tutorial" width="440" height="220" style="zoom:50%">
Canvas not available.
</canvas>
<canvas id="tutorial1" width="440" height="220" style="zoom:50%">
Canvas not available.
</canvas>
<canvas id="tutorial2" width="440" height="220" style="zoom:50%">
Canvas not available.
</canvas>
<canvas id="tutorial3" width="440" height="220" style="zoom:50%">
Canvas not available.
</canvas>
<canvas id="tutorial4" width="440" height="220" style="zoom:50%">
Canvas not available.
</canvas>
<form id="drawTemp">
</form>
</div>
</body>
</html>
看到kennebec给出的答案后如果在我的代码中创建了更改
if (canvas != null && canvas.getContext)
。
使用此行后,getContext 错误没有出现,但是在执行绘制 Canvas 后的页面后,它给了我新的错误(“您的浏览器不支持 Canvas ”)。在这里帮助我。
最佳答案
我查过这个,我没明白,你为什么会遇到这个问题,虽然我对画面不太了解
canvas != null && canvas.getContext
由此您会收到另一个错误“您的浏览器不支持 Canvas 当我做这个时,这个错误是在绘制 Canvas 之后出现的。然后我就删除了那个给我这个警报消息的警报。我想你必须试试这个,搜索这个警报,看看接下来会发生什么,它不会影响程序,它运行良好
关于javascript - 如何解决 Canvas 不受支持的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18092570/