2

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>

1 Answer 1

3

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.