Top community members
All Wiki Articles Create Wiki Article

Have you ever heard about encyclopedia for code?

What about: Wiki for Code project?

* because I've tried to share code and my solutions on different services and it was removed or I was unwelcomed
   - maybe it wasn't good place, but this one is.

if you think you have solution that can help someone, you are welcome to share this solution - Click here

JavaScript - how to clear canvas?

0 contributions
9 points

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.lineWidth = 5.0;
        context.strokeStyle = '#0000ff';
        context.fillStyle = '#ff0000';
        context.arc(xCenter, yCenter , circleRadius, 0, 2 * Math.PI);

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




0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more