Home Web Front-end H5 Tutorial Implementing the eraser function using the clearRect() method in the HTML5 Canvas API_html5 tutorial tips

Implementing the eraser function using the clearRect() method in the HTML5 Canvas API_html5 tutorial tips

May 16, 2016 pm 03:51 PM
api canvas html5

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.

XML/HTML CodeCopy content to clipboard
  1. 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:

JavaScript CodeCopy content to clipboard
  1. "UTF-8">
  2. Getting started example of using HTML5 clearRect() to erase a specified rectangular area
  3. "myCanvas" width="400px" height="300px" style="border: 1px solid red;">
  4. Your browser does not support the canvas tag.

The corresponding display effect is as follows:
2016315111842196.png (421×312)

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.

JavaScript CodeCopy content to clipboard

The corresponding display effect is as follows (is it a bit like a copper coin?).
2016315111914378.png (417×320)


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:

JavaScript CodeCopy content to clipboard
  1.   
  2. "zh">   
  3.   
  4.     "UTF-8">   
  5.     clearRect()   
  6.        
  7.   
  8.   
  9. "canvas-warp">   
  10.     "canvas">   
  11.         你的浏览器居然不支持Canvas?!赶快换一个吧!!   
  12.        
  
  •   
  • <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>   
  •   
  •   
  • 2016315111932638.jpg (1235×714)

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

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

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

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

    HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

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

    Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

    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.

    HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

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

    HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

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

    Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

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

    HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

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

    HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

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

    See all articles