我正在使用 HTML5 画布,javascript。我在 html5 canvas 和 javascript 的帮助下创建了一个页面。但是当我运行它时,它给了我这个错误
(“错误=无法获取属性'getContext'的值:对象为空或未定义”)
但它不会终止程序..程序运行良好。
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>
在看到肯尼贝克给出的答案后,如果我的代码发生了变化
if (canvas != null && canvas.getContext)
.
使用此行后,getContext 错误不会出现,但是在执行绘制画布之后的页面后,它给了我新的错误(“您的浏览器不支持画布”)。在这里帮帮我。