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
- <! DOCTYPE html>
- <Html lang = "zh">
- <Head>
- <Meta charset = "UTF-8">
- <Title> new canvas </title>
- <Style>
- Body {background: url ("./images/bg3.jpg") repeat ;}
- # Canvas {border: 1px solid # aaaaaa; display: block; margin: 50px auto ;}
- </Style>
- </Head>
- <Body>
- <Div id = "canvas-warp">
- <Canvas id = "canvas">
- Does your browser support Canvas ?! Just change one !!
- </Canvas>
- </Div>
- <Script>
- Window. onload = function (){
- Var canvas = document. getElementById ("canvas ");
- Canvas. width = 800;
- Canvas. height = 600;
- Var context = canvas. getContext ("2d ");
- Context. fillStyle = "# FFF ";
- Context. fillRect (0, 0, 800,600 );
- }
- </Script>
- </Body>
- </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
- 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
- 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
- <! DOCTYPE html>
- <Html lang = "zh">
- <Head>
- <Meta charset = "UTF-8">
- <Title> draw an arc </title>
- <Style>
- Body {background: url ("./images/bg3.jpg") repeat ;}
- # Canvas {border: 1px solid # aaaaaa; display: block; margin: 50px auto ;}
- </Style>
- </Head>
- <Body>
- <Div id = "canvas-warp">
- <Canvas id = "canvas">
- Does your browser support Canvas ?! Just change one !!
- </Canvas>
- </Div>
- <Script>
- Window. onload = function (){
- Var canvas = document. getElementById ("canvas ");
- Canvas. width = 800;
- Canvas. height = 600;
- Var context = canvas. getContext ("2d ");
- Context. fillStyle = "# FFF ";
- Context. fillRect (0, 0, 800,600 );
- Fig (context, 200,200,600,200,600,400,100 );
- };
- Function drawArcTo (cxt, x0, y0, x1, y1, x2, y2, r ){
- Cxt. beginPath ();
- Cxt. moveTo (x0, y0 );
- Cxt. arcTo (x1, y1, x2, y2, r );
- Cxt. lineWidth = 6;
- Cxt. strokeStyle = "red ";
- Cxt. stroke ();
- Cxt. beginPath ();
- Cxt. moveTo (x0, y0 );
- Cxt. lineTo (x1, y1 );
- Cxt. lineTo (x2, y2 );
- Cxt. lineWidth = 1;
- Cxt. strokeStyle = "# 0088AA ";
- Cxt. stroke ();
- }
- </Script>
- </Body>
- </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
- 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
- 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 ......