我想清除一块完整的画布。但是使用clearRect()不会做任何事情。那么如何从画布上删除绘制的图片?
<canvas id="myCanvas" width="600" height="580"></canvas>
<script>
window.onload = function() {
canv = document.getElementById("myCanvas");
ctx = canv.getContext("2d");
falcon = document.getElementById("milenium_falcon");
ctx.drawImage(falcon, 315, 500, 75, 75);
document.addEventListener("keydown",keyPush);
setInterval(game,1000/15);
}
x = 0;
function game() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(falcon, 315 - x, 500, 75, 75);
}
function keyPush(evt) {
switch(evt.keyCode) {
case 37:
x -= 5;
break;
case 39:
x += 5;
break;
}
}
</script>
答案 0 :(得分:2)
在调用.clearRect()
时,您使用的是变量canvas
而不是canv
。如果您改用以下行,它应该可以工作:
ctx.clearRect(0, 0, canv.width, canv.height);