Detailed description of the non-zero surround principle when HTML5 Canvas is used to draw irregular images. html5canvas

Source: Internet
Author: User

Detailed description of the non-zero surround principle when HTML5 Canvas is used to draw irregular images. html5canvas

Path direction and non-zero surround Principle
We usually draw well-organized images. If we use a line bar to draw an abstract work, just like the figure below, how do we use fill () to dye our shoes?

Here we need to use a mathematical method-the non-zero surround principle to determine which region is inside and which region is outside. Next, let's take a look at what is a non-zero surround principle.

First, we have to determine a path for the graph. We only need to "draw a stroke" and "do not take the recurring route ., Indicates a path direction. Let's first assume that the positive direction of the path is 1 (in fact, it can be-1, and the positive and negative directions are opposite to each other, not 0 ), the opposite direction is the opposite number-1.
Then, we take a ray in any direction for any point in the several areas cut by the sub-path. Here I only take the rays in three areas as an example, to determine whether the three regions are "Inside" or "outside ".
Next, let's judge. The ray L1 derived from S1 and the positive direction of the Child path of S1 are intersecting. Then we give the counter + 1 and the result is + 1, which is outside.
The X-ray L2 derived from S2 interacts with the positive direction of the two sub-paths. The counter is + 2 and the result is + 2.
The ray L3 in S3 is intersecting with the two sub-paths, but there is a reverse direction. The counter is + 1-1 and the result is 0. Yes, as long as the result is not 0, the ray is located outside.


Draw a ring
Do you remember the arc method? Its last parameter is to determine the direction of the path. If the two concentric circles with the opposite path are together, what is the magical effect of graph coloring?

The following code is used to implement it.

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> ring </title>
  6. <Style>
  7. Body {background: url ("./images/bg3.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. Context. shadowColor = "#545454 ";
  26. Context. shadowOffsetX = 5;
  27. Context. shadowOffsetY = 5;
  28. Context. shadowBlur = 2;
  29. Context. arc (400,300,200, 0, Math. PI * 2, false );
  30. Context. arc (400,300,230, 0, Math. PI * 2, true );
  31. Context. fillStyle = "#00 AAAA ";
  32. Context. fill ();
  33. };
  34. </Script>
  35. </Body>
  36. </Html>

Running result:

Hollow paper-cut effect
Next, we use the non-zero surround principle and shadow to draw a hollow paper-cut effect.

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> hollow-out paper-cutting effect </title>
  6. <Style>
  7. Body {background: url ("./images/bg3.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. Context. beginPath ();
  26. Context. rect (200,100,400,400 );
  27. Fig (context, 250,150,300,150 );
  28. Fig (context, 345,350,420,450,270,450 );
  29. Context. arc (500,400, 50, 0, Math. PI * 2, true );
  30. Context. closePath ();
  31. Context. fillStyle = "#058 ";
  32. Context. shadowColor = "gray ";
  33. Context. shadowOffsetX = 10;
  34. Context. shadowOffsetY = 10;
  35. Context. shadowBlur = 10;
  36. Context. fill ();
  37. };
  38. // Draw a rectangle counterclockwise
  39. Function drawPathRect (cxt, x, y, w, h ){
  40. /**
  41. * BeginPath and closePath cannot be used here,
  42. * Otherwise, it will not belong to the sub-path but to another new path,
  43. * The non-zero surround principle cannot be used.
  44. */
  45. Cxt. moveTo (x, y );
  46. Cxt. lineTo (x, y + h );
  47. Cxt. lineTo (x + w, y + h );
  48. Cxt. lineTo (x + w, y );
  49. Cxt. lineTo (x, y );
  50. }
  51. // Draw a triangle counterclockwise
  52. Function drawPathTriangle (cxt, x1, y1, x2, y2, x3, y3 ){
  53. Cxt. moveTo (x1, y1 );
  54. Cxt. lineTo (x3, y3 );
  55. Cxt. lineTo (x2, y2 );
  56. Cxt. lineTo (x1, y1 );
  57. }
  58. </Script>
  59. </Body>
  60. </Html>

Running result:

The reason for drawing the rectangle manually here is that we want to obtain the rectangle of the counterclockwise path, and the rect () method provided by the API is drawn as a clockwise rectangle. In addition, you must note that this paper-cut is a graph and a path. You cannot use beginPath () and closePath () in the method of drawing hollow triangles and hollow rectangles. Otherwise, they will be new paths and images, instead of the sub-paths and sub-images of paper-cut, the non-zero surround principle cannot be used.


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.