Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How to clear the canvas for redrawing

In the wake of trying different things with composite tasks and drawing pictures on the canvas I'm presently attempting to eliminate pictures and compositing. How would I do this?

I need to clear the canvas for redrawing different pictures; this can continue for some time so I don't think drawing another square shape each time will be the most proficient choice.
by

2 Answers

ninja01
Given that canvas is a canvas element,

const context = canvas.getContext('2d');

context.clearRect(0, 0, canvas.width, canvas.height);
sandhya6gczb
Use clearRect method by passing x,y co-ordinates and height and width of canvas. ClearRect will clear whole canvas as :

canvas = document.getElementById("canvas");
ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);

Login / Signup to Answer the Question.