Use HTML5 Canvas API to draw an arc tutorial, html5canvas

Source: Internet
Author: User

Use HTML5 Canvas API to draw an arc tutorial, html5canvas

Draw standard arc

Before we begin, we will optimize our plotting environment. The idea is the texture of the previous lesson. If you don't like this background, I have provided other background images in the images directory for your choice. In addition, all style sheets are written under 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> new canvas </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. }
  26. </Script>
  27. </Body>
  28. </Html>

Running result:

The reason why we need to draw a blank rectangle to fill the canvas is because we have said that canvas is transparent. If the background color is not set, it will be overwritten by the <body> texture I set, to make it have a background color (white), you only need to draw a rectangle to overwrite the canvas.

How are you doing? Is it really cool?

Draw an arc using arc ()
The method of using arc () is as follows:

Copy the content to the clipboard using JavaScript Code
  1. Context. arc (x, y, radius, startAngle, endAngle, anticlockwise)

The first three parameters are the Center Coordinate and circle radius. StartAngle and endAngle use radians instead of angle values. Radians are absolute, for example.

Anticlockwise indicates the method of drawing, which is clockwise or counterclockwise. It is a Boolean value. "true" indicates clockwise drawing, and "false" indicates clockwise drawing. The default value is false.

The radians are absolute. What do you mean? It refers to the ARC you want to draw, and the radians simply follow the above standard.

For example, if you plot 0.5pi ~ 1 PI arc. If it is clockwise, it is only the 1/4 arc in the lower left corner. If it is counterclockwise, It is the 3/4 arc in the upper right corner. Try it by yourself here.

Draw an arc using a cut point

ArcTo () Introduction:
The arcTo () method receives five parameters, which are the coordinates of two cut points and the arc radius. This method draws an Arc Based on the tangent, that is, an arc is determined by two tangent lines.
The details are as follows.

Copy the content to the clipboard using JavaScript Code
  1. ArcTo (x1, y1, x2, y2, radius)

This function draws an arc with a given radius. The starting point of the arc is tangent to the straight line from the position of the current path to the (x1, y1) point, and the endpoint of the arc is (x1, y1) point to (x2, y2) in a straight line. Therefore, it is usually used with moveTo () or lineTo. Its capabilities can be replaced by a simpler arc () method, and its complexity is complicated by the use of cut points in the rendering method.

Draw an arc using a cut point:
In the following case, I have drawn the tangent to make it clearer.

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> draw an arc </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. Fig (context, 200,200,600,200,600,400,100 );
  26. };
  27. Function drawArcTo (cxt, x0, y0, x1, y1, x2, y2, r ){
  28. Cxt. beginPath ();
  29. Cxt. moveTo (x0, y0 );
  30. Cxt. arcTo (x1, y1, x2, y2, r );
  31. Cxt. lineWidth = 6;
  32. Cxt. strokeStyle = "red ";
  33. Cxt. stroke ();
  34. Cxt. beginPath ();
  35. Cxt. moveTo (x0, y0 );
  36. Cxt. lineTo (x1, y1 );
  37. Cxt. lineTo (x2, y2 );
  38. Cxt. lineWidth = 1;
  39. Cxt. strokeStyle = "# 0088AA ";
  40. Cxt. stroke ();
  41. }
  42. </Script>
  43. </Body>
  44. </Html>

Running result:

This case also demonstrates the role of arcTo. For a clearer explanation, I will add an analysis chart.

Note that the starting point of arcTo () is (x0, y0), but (x0, y0) is not necessarily the arc point. The real arcTo () function only imports (x1, y1) and (x2, y2 ). (X1, y1) is called a control point, and (x2, y2) is the cut point of the arc end point, which is not necessarily on the arc. But (x0, y0) must be on the arc.
There is a little detour. Let's change the parameters of the drawArcTo () function to try it out.
(X2, y2) is not necessarily on the arc:

Copy the content to the clipboard using JavaScript Code
  1. Fig (context, 200,100,600,100,600,400,400 );

(X0, y0) must be on the arc:

Copy the content to the clipboard using JavaScript Code
  1. Fig (context, 400,100,600,100,600,400,400 );

It is quite interesting. It directly connects the cut point (x0, y0) to form a line segment to pass through (x0, y0. Persistent Arc ......

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.