


Implementing the eraser function using the clearRect() method in the HTML5 Canvas API_html5 tutorial tips
In the real world, we use brushes to draw on the drawing board; in html5 canvas, we can also use the canvas's brush - the CanvasRenderingContext2D object to draw on the canvas. As we all know, our brushes are generally used with erasers to correct mistakes during the painting process and repaint. In html5 canvas, the CanvasRenderingContext2D object also provides us with an eraser that can be reused forever - the clearRect() method.
- clearRect(x, y, width, height)
The clearRect() method of the CanvasRenderingContext2D object is used to clear all graphic pixels in the rectangular area of the canvas with the specified coordinate point (x, y) as the upper left corner, width as width, and height as height.
Now, let’s look at a practical example. We first draw a solid circle with a radius of 50px, and then use the eraser clearRect() to erase a local area within it. The original html5 code for drawing a circle is as follows:
- "UTF-8">
-
Getting started example of using HTML5 clearRect() to erase a specified rectangular area - Your browser does not support the canvas tag.
The corresponding display effect is as follows:
Now, we use the clearRect() method to erase the rectangular area of the solid circle with the center (100,100) as the center and 10px on each side.
The corresponding display effect is as follows (is it a bit like a copper coin?).
On the page, we can erase an area on the page to display the background image.
In the example below we erase the white space in the rectangle and let it show the page background:
- "zh">
- "UTF-8">
-
clearRect() -
"canvas-warp">
- 你的浏览器居然不支持Canvas?!赶快换一个吧!!
- <script> </span> </li> <li class="alt"> <span> window.onload = </span><span class="keyword">function</span><span>(){ </span> </li> <li> <span> </span><span class="keyword">var</span><span> canvas = document.getElementById(</span><span class="string">"canvas"</span><span>); </span> </li> <li class="alt"> <span> canvas.width = 800; </span> </li> <li> <span> canvas.height = 600; </span> </li> <li class="alt"> <span> </span><span class="keyword">var</span><span> context = canvas.getContext(</span><span class="string">"2d"</span><span>); </span> </li> <li> <span> context.fillStyle = </span><span class="string">"#FFF"</span><span>; </span> </li> <li class="alt"> <span> context.fillRect(0,0,800,600); </span> </li> <li> <span> </span> </li> <li class="alt"> <span> </span><span class="comment">//清空画布 </span><span> </span> </li> <li> <span> context.clearRect(0,0,canvas.width,canvas.height); </span> </li> <li class="alt"> <span> </span> </li> <li> <span> }; </span> </li> <li class="alt"> <span></script>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
