Use clearRect () in the HTML5 Canvas API to implement the eraser function. html5clearrect

Source: Internet
Author: User

Use clearRect () in the HTML5 Canvas API to implement the eraser function. html5clearrect

In the real world, we use a paint brush to draw on the canvas. In html5 canvas, we can also use the canvas paint brush-CanvasRenderingContext2D object to draw on the canvas. As we all know, our paint brushes are usually used with erasers to correct mistakes in the painting process and re-paint. In html5 canvas, the CanvasRenderingContext2D object also provides us with an eraser-clearRect () method that can be reused forever.

Copy XML/HTML Code to clipboard
  1. ClearRect (x, y, width, height)

The clearRect () method of the CanvasRenderingContext2D object is used to clear all image 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.

Next, let's look at a practical example. We first draw a solid circle with a radius of 50 PX, and then erase the local area using the eraser clearRect. The original html5 code for circular drawing is as follows:

Copy the content to the clipboard using JavaScript Code
  1. <! DOCTYPE html>
  2. <Html>
  3. <Head>
  4. <Meta charset = "UTF-8">
  5. <Title> using HTML5 clearRect () to erase the specified rectangular area </title>
  6. </Head>
  7. <Body>
  8. <! -- Add a canvas label and a red border to view the canvas -->
  9. <Canvas id = "myCanvas" width = "400px" height = "300px" style = "border: 1px solid red;">
  10. Your browser does not support canvas labels.
  11. </Canvas>
  12. <Script type = "text/javascript">
  13. // Obtain the Canvas object (Canvas)
  14. Var canvas = document. getElementById ("myCanvas ");
  15. // Check whether the current browser supports Canvas objects to avoid syntax errors in some browsers that do not support html5.
  16. If (canvas. getContext ){
  17. // Obtain the corresponding CanvasRenderingContext2D object (paint brush)
  18. Var ctx = canvas. getContext ("2d ");
  19. // Draw a circle with a coordinate point (, 10) as the center and a radius of 50 PX
  20. Ctx. arc (100,100, 50, 0, Math. PI * 2, false );
  21. // Draw and fill the circle
  22. Ctx. fill ();
  23. }
  24. </Script>
  25. </Body>
  26. </Html>

The corresponding display effect is as follows:

Now, we use the clearRect () method to erase the rectangular area of each 10 PX circle in the center of the circle (100,100.

Copy the content to the clipboard using JavaScript Code
  1. <Script type = "text/javascript">
  2. // Obtain the Canvas object (Canvas)
  3. Var canvas = document. getElementById ("myCanvas ");
  4. // Check whether the current browser supports Canvas objects to avoid syntax errors in some browsers that do not support html5.
  5. If (canvas. getContext ){
  6. // Obtain the corresponding CanvasRenderingContext2D object (paint brush)
  7. Var ctx = canvas. getContext ("2d ");
  8. // Draw a circle with a coordinate point (, 10) as the center and a radius of 50 PX
  9. Ctx. arc (100,100, 50, 0, Math. PI * 2, false );
  10. // Draw and fill the circle
  11. Ctx. fill ();
  12. // Erase the image in the rectangle area
  13. Ctx. clearRect (90, 90, 20, 20 );
  14. }
  15. </Script>

The corresponding display effect is as follows (is it a bit like a copper coin ?).


On the page, we can erase the area on a page to display the background image.
In the following example, we erase the blank area in the rectangle to display the page Background:

Copy the content to the clipboard using JavaScript Code
  1. <! DOCTYPE html>
  2. <Html lang = "zh">
  3. <Head>
  4. <Meta charset = "UTF-8">
  5. <Title> clearRect () </title>
  6. <Style>
  7. Body {background: url ("./images/bg2.jpg") repeat ;}
  8. # Canvas {border: 1px solid # aaaaaa; display: block; margin: 50px auto ;}
  9. </Style>
  10. </Head>
  11. <Body>
  12. <Div id = "canvas-warp">
  13. <Canvas id = "canvas">
  14. Does your browser support Canvas ?! Just change one !!
  15. </Canvas>
  16. </Div>
  17. <Script>
  18. Window. onload = function (){
  19. Var canvas = document. getElementById ("canvas ");
  20. Canvas. width = 800;
  21. Canvas. height = 600;
  22. Var context = canvas. getContext ("2d ");
  23. Context. fillStyle = "# FFF ";
  24. Context. fillRect (0, 0, 800,600 );
  25. // Clear the canvas
  26. Context. clearRect (0, 0, canvas. width, canvas. height );
  27. };
  28. </Script>
  29. </Body>
  30. </Html>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.