TheHummel
TheHummel

Reputation: 93

Why is clearRect() not removing anything from my canvas?

I want to clear a full canvas. But using clearRect() isn't doing anything. So how can I remove the drawn picture from my canvas?

<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>

Upvotes: 2

Views: 148

Answers (1)

Hydrothermal
Hydrothermal

Reputation: 5081

In your call to .clearRect(), you are using the variable canvas instead of canv. It should work if you use this line instead:

ctx.clearRect(0, 0, canv.width, canv.height);

Upvotes: 3

Related Questions