HTML5 Tutorial: How to Empty or Clear your Drawing Canvas

In HTML5 we have been given a great drawing canvas that allows you to draw directly on a webpage itself. These drawings can be static and drawn just once when you load the webpage but they can also be dynamic and change all the time! Sometimes you want to empty or clear your drawing canvas to draw something else.

In this tutorial I'll show you how to do just that: with a simple method call you can empty your HTML5 drawing canvas and reset it to its background color. I will also show that it is not very different from clearing just a part of your canvas, if that is what you wish to do.

As an example, here is a canvas and a small piece of JavaScript code that draws something on the canvas:

<canvas id="mycanvas" width="300" height="300">
</canvas>
<script type="text/javascript">
var canvas = document.getElementById('mycanvas');
var ctx = canvas.getContext('2d');
ctx.rect(10, 10, 100, 100);
ctx.lineWidth = 10;
ctx.strokeStyle = "red";
ctx.stroke();
</script>

You can empty or clear your HTML5 drawing canvas by using the following method call:

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

where width and height are the width and height of your canvas respectively. We have a canvas of 300 by 300 pixels so we need to use the following method call:

ctx.clearRect(0, 0, 300, 300);

You are now clearing a rectangle that starts at the coordinate (0, 0) with dimensions 300 by 300 pixels. Those are the dimensions of your canvas and that means you are clearing your whole HTML drawing canvas. The canvas will now be empty and it will be colored using the background color. This is usually white but you can change the background color of a canvas by using CSS styling.

How to empty a part of the HTML5 drawing canvas?

You can use the same method call to only empty a part of the canvas. You can give the top-left coordinates and dimensions of a rectangle or square and that part of the canvas will be cleared. For example, the following piece of code will clear a rectangle located at the coordinate (50, 50) with a width of 200 and a height of 100:

ctx.clearRect(50, 50, 200, 100);

This article was written by Simeon Visser. I am earning money online by writing here at HubPages.com. Would you like to earn money online as well? Read the success stories and sign up today to get started!

More by this Author


Comments 5 comments

carljsp profile image

carljsp 5 years ago

I am beginner to html what html editor should i use ?


simeonvisser profile image

simeonvisser 5 years ago Author

You should use a text editor, not a HTML editor. You will want to write the HTML code yourself so you know what you are doing. If you use Windows then a program like Notepad is good enough. In any case, you don't want a HTML editor that generates the HTML for you.

But better programs are programmer text editors like Notepad++ and others that provide features like syntax coloring. That is also very useful when you learn more about web development and when you will start working with programming languages like PHP or Python.


g33kco profile image

g33kco 3 years ago from Riverton, WY

carljsp: If you're looking for a good editor and plan to get further into development, like php, I recommend you get the NetBeans PHP version here http://netbeans.org/downloads/index.html or you can get Aptana Studio http://www.aptana.com/products/studio3/download get the standard version not the eclipse plugin.


Letitia 23 months ago

Play inmiofatrve for me, Mr. internet writer.


Vishnu 23 months ago

MLM works and is the vehicle reliopsnbse for helping many people on the road to find financial independence, typically within 2-5 years.There is, however, much confusion and misunderstanding concerning this business model. A pyramid scheme has no real product so commissions are based on bringing new people into the scheme who in turn also bring new people into the scheme. It's usually the people at the top that get the most while those at the bottom get very little. Eventually all pyramid schemes collapse because there is no real product being sold.MLM, on the other hand, usually has a very real product that is sold either by retail or through members personal purchases (usually both). Members are encouraged to build a network of distributors to market the product.In MLM you can earn more than those at the top if you apply some effort.In a pyramid scheme you can never earn more than those above you, so when investigating an MLM company see if you can find out if there are people earning more than their sponsors, (this is the crucial test to weed out pyramid schemes)

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working