我的画布元素无法正常工作。它不会画一个矩形。请帮我诊断问题。我不知道问题出在哪里,是我的css、javascript还是html?我是否正确设置了所有内容,因为当我运行调试器并将其放在服务器上时没有出现错误?
索引.html
<!DOCTYPE html>
<html>
<head>
<title>Paco Developement</title>
<link rel="stylesheet" href="style.css" />
<script src="game.js"></script>
</head>
<body>
<canvas id="canvasBg" height="500" width="800"></canvas>
<canvas id="character" height="500" width="800"></canvas>
</body>
</html>
样式.css
* {
padding:0px;
margin:0px;
}
header, section, footer, aside, nav, article, figure, hgroup{
display:block;
}
body {
background:#BADA55;
width:100%;
height:100%;
}
#canvasBg {
display:block;
background:#fff;
margin:100px auto 0px;
}
#character {
display:block;
margin:-500px auto 0px;
}
游戏.js
var canvasBg = document.getElementById('canvasBg');
var bgCtx = canvasBg.getContext("2d");
document.addEventListener('load',drawRect,false);
function drawRect(){
bgCtx.fillStyle="#FF0000";
bgCtx.fillRect(0,0,150,75);
}