【发布时间】:2012-05-04 17:20:42
【问题描述】:
我收到错误Uncaught TypeError: Cannot read property 'getContext' of null 并且文件中的重要部分是...我想知道由于game.js 在下面的目录中,它找不到画布吗?我该怎么办?
./index.html:
<canvas id="canvas" width="640" height="480"></canvas>
./javascript/game.js:
var Grid = function(width, height) {
...
this.draw = function() {
var canvas = document.getElementById("canvas");
if(canvas.getContext) {
var context = canvas.getContext("2d");
for(var i = 0; i < width; i++) {
for(var j = 0; j < height; j++) {
if(isLive(i, j)) {
context.fillStyle = "lightblue";
}
else {
context.fillStyle = "yellowgreen";
}
context.fillRect(i*15, j*15, 14, 14);
}
}
}
}
}
【问题讨论】:
标签: javascript html canvas