JavaScript - how to clear canvas?

12 points
Created by:

Simple solution:

// clear from x=0, y=0 on area width * height
context.clearRect(0, 0, canvas.width, canvas.height);


Using JavaScript it is possible to clear canvas in following way.

1. clearRect method example

// ONLINE-RUNNER:browser;

<!doctype html>
    #my-canvas { border: 1px solid gray; }
  <button onclick="drawCircle()">Draw circle!</button>
  <button onclick="clearCanvas()">Clear canvas!</button>
  <br /><br />
  <canvas id="my-canvas" width="300" height="300"></canvas>

    var canvas = document.querySelector('#my-canvas');
    var context = canvas.getContext('2d');

    var xCenter = canvas.width / 2;
    var yCenter = canvas.height / 2;
    var circleRadius = 110;
    function drawCircle() {
        context.fillStyle = '#ff0000';
        context.arc(xCenter, yCenter , circleRadius, 0, 2 * Math.PI);

    function clearCanvas() {
        context.clearRect(0, 0, canvas.width, canvas.height);



See also

  1. JavaScript - how to clear canvas with specific color?

Alternative titles

  1. JavaScript - how to clean canvas?
  2. JavaScript - how to clean rect?
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!

JavaScript - HTML5 canvas tutorial

Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂