我是初学者,我坚持在画布中用html 5画一个矩形; 我的HTML代码:
<script src="new 3y.js"></script>
<title>test</title>
</head>
<body>
<section id="main">
<canvas id="canvas" width="600" height="400">
test
</canvas>
</section>
</body
我的js代码:
function doFirst() {
var x = document.getElementbyId('canvas');
canvas = x.getContext("2d");
canvas.strokeRect(10,10,100,200);
}
window.addEventListener("load" , doFirst ,false);
答案 0 :(得分:2)
问题是您使用的是document.getElementbyId
,但b
必须为大写。
解决这个问题,您的代码运行良好:
function doFirst() {
var x = document.getElementById('canvas');
canvas = x.getContext("2d");
canvas.strokeRect(10,10,100,200);
}
window.addEventListener("load" , doFirst ,false);
<body>
<section id="main">
<canvas id="canvas" width="600" height="400">
test
</canvas>
</section>
</body>