DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > How to Clear an HTML Canvas

How to Clear an HTML Canvas

Let's look at this quick tip to clear an HTML canvas partially or completely for redrawing.

Johnny Simpson user avatar by
Johnny Simpson
CORE ·
Mar. 17, 22 · Web Dev Zone · Code Snippet
Like (4)
Save
Tweet
2.41K Views

Join the DZone community and get the full member experience.

Join For Free

When we've created a canvas, sometimes we'll want to clear it completely before redrawing something else. Let's look at how to do that.

How to Clear HTML Canvas for Redrawing

Let's say we have a canvas, and we've already written code like this:

JavaScript
 
let myCanvas = document.getElementById('myCanvas');
let ctx = myCanvas.getContext('2d');

If we want to clear the entire canvas and leave it blank, we use clearRect(). clearRect has the 4 arguments - x, y, width and height.

If we want to clear a canvas entirely, starting at (0, 0), our code looks like this:

JavaScript
 
let myCanvas = document.getElementById('myCanvas');
let ctx = myCanvas.getContext('2d');

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

Although we can clear different areas if we want. The below code clears a canvas starting at the xy coordinates (150, 150), and only clearing 100px by 150px (width by height) of content:

JavaScript
 
let myCanvas = document.getElementById('myCanvas');
let ctx = myCanvas.getContext('2d');

ctx.clearRect(150, 150, 100, 150);

Clearing an HTML Canvas With a Custom Color

To clear a canvas with a custom color, you can instead use the normal rect() function, with a custom color. That looks like this:

JavaScript
 
let myCanvas = document.getElementById('myCanvas');
let ctx = myCanvas.getContext('2d');

ctx.rect(0, 0, myCanvas.width, myCanvas.height);
// Fill our rectangle
ctx.fillStyle = 'red';
ctx.fill();

The above code will clear our canvas and fill it with red instead.

HTML Clear (Unix)

Published at DZone with permission of Johnny Simpson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Modern Application Security Requires Defense in Depth
  • Applying Domain-Driven Design Principles to Microservice Architectures
  • Practice on Pushing Messages to Devices of Different Manufacturers
  • SQL vs. NoSQL: Pros and Cons

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo