Draw curve of HTML5 canvas Basic Drawing, html5canvas

Source: Internet
Author: User

Draw curve of HTML5 canvas Basic Drawing, html5canvas

<Canvas> </canvas>It is a new label added in HTML5 for drawing graphics. In fact, this label is the same as other labels. Its special feature is that this label can obtain a CanvasRenderingContext2D object, we can use JavaScript scripts to control this object for plotting.

<Canvas> </canvas>It is just a container for drawing graphics. In addition to attributes such as id, class, and style, there are also attributes of height and width. There are three steps to draw an element on the <canvas> element:

1. Obtain the DOM object corresponding to the <canvas> element. This is a Canvas object;
2. Call the getContext () method of the Canvas object to obtain a CanvasRenderingContext2D object;
3. Call the CanvasRenderingContext2D object for plotting.

Draw a curve

There are four functions as follows:

• Context. arc (x, y, r, sAngle, eAngle, counterclockwise );Used to create an ARC/curve (used to create a circle or a part of a circle ). Meaning of received parameters:
| Parameter | meaning |
|: ----- |
| X coordinate of the center of the circle |
| Y coordinate of the center of the circle |
| R | radius of the circle |
| SAngle | start angle, in radians (three o'clock of the arc circle is 0 degrees) |
| EAngle | end angle, in radians |
| Counterclockwise | optional. It is required that the drawing should be clockwise or clockwise. False = clockwise, true = clockwise |

Below are several examples of several arc () functions:

Copy the content to the clipboard using JavaScript Code
  1. Var canvas = document. getElementById ("canvas ");
  2. Var context = canvas. getContext ("2d ");
  3. Context. strokeStyle = "# F22D0D ";
  4. Context. lineWidth = "2 ";
  5. // Draw a circle
  6. Context. beginPath ();
  7. Context. arc (100,100, 40, 0, 2 * Math. PI );
  8. Context. stroke ();
  9. // Draw a semi-circle
  10. Context. beginPath ();
  11. Context. arc (200,100, Math. PI );
  12. Context. stroke ();
  13. // Draw a semi-circle, counterclockwise
  14. Context. beginPath ();
  15. Context. arc (300,100, Math. PI, true );
  16. Context. stroke ();
  17. // Draw a closed semi-circle
  18. Context. beginPath ();
  19. Context. arc (400,100, Math. PI );
  20. Context. closePath ();
  21. Context. stroke ();

The effect is as follows:

• Context. arcTo (x1, y1, x2, y2, r );Create an ARC/curve between two tangent lines on the canvas. Meaning of received parameters:

Note that the starting point of the curve drawn by the arcTo function needs to be set through the moveTo () function. The following uses the arcTo function to draw a rounded rectangle:

Copy the content to the clipboard using JavaScript Code
  1. Function createRoundRect (context, x1, y1, width, height, radius)
  2. {
  3. // Move to the upper left corner
  4. Context. moveTo (x1 + radius, y1 );
  5. // Add a line segment connected to the upper-right corner
  6. Context. lineTo (x1 + width-radius, y1 );
  7. // Add an arc
  8. Context. arcTo (x1 + width, y1, x1 + width, y1 + radius, radius );
  9. // Add a line segment connected to the bottom right corner
  10. Context. lineTo (x1 + width, y1 + height-radius );
  11. // Add an arc
  12. Context. arcTo (x1 + width, y1 + height, x1 + width-radius, y1 + height, radius );
  13. // Add a line segment connected to the lower left corner
  14. Context. lineTo (x1 + radius, y1 + height );
  15. // Add an arc
  16. Context. arcTo (x1, y1 + height, x1, y1 + height-radius, radius );
  17. // Add a line segment connected to the upper left corner
  18. Context. lineTo (x1, y1 + radius );
  19. // Add an arc
  20. Context. arcTo (x1, y1, x1 + radius, y1, radius );
  21. Context. closePath ();
  22. }
  23. // Obtain the DOM object corresponding to the canvas Element
  24. Var canvas = document. getElementById ('mc ');
  25. // Obtain the CanvasRenderingContext2D object drawn on the canvas
  26. Var context = canvas. getContext ('2d ');
  27. Context. lineWidth = 3;
  28. Context. strokeStyle = "# F9230B ";
  29. CreateRoundRect (context, 30, 30,400,200, 50 );
  30. Context. stroke ();

The effect is as follows:

• Context. quadraticCurveTo (cpx, cpy, x, y );Draw a second beset curve. The parameter meanings are as follows:

The starting point of the curve is the last point in the current path. If the path does not exist, use the beginPath () and moveTo () Methods to define the start point.

 • Context. bezierCurveTo (cp1x, cp1y, cp2x, cp2y, x, y );Draw a three-time besell curve with the following parameters:

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.