An HTML5canvas path connects a series of vertices by drawing commands. These vertices form a straight line or curve. The path can be used to draw various types of images on HTML5canvas: straight lines, circles, polygon, etc ....,.
An HTML5 canvas path connects a series of vertices by drawing commands. These vertices form a straight line or curve. Paths can be used to draw various types of images on HTML5 canvas: straight lines, circles, polygon, and so on. Path rendering is the core of canvas and must be well understood and mastered.
Start or end a path
To start and close a path, you can use the beginPath () and closePath () functions in the 2D context. For example:
- Var canvas = document. getElementById ("ex1 ");
- Var context = canvas. getContext ("2d ");
-
- Context. beginPath ();
-
- //... Draw the path
-
- Context. closePath ();
MoveTo () function
When you draw a path in the canvas, you can imagine that you are using a "virtual pen ". This virtual pen is always located at a certain position. You can use the moveTo (x, y) function in the 2D context to move this virtual pen. For example, the following code:
- Context. moveTo (10, 10 );
In this example, the "virtual pen" is moved to the coordinate point (10, 10.
LineTo () function
The lineTo function is used to draw a straight line from the current position of the virtual pen to the point specified by the lineTo () function. The following is an example:
- Context. beginPath ();
-
- Context. moveTo (10, 10 );
- Context. lineTo (50, 50 );
-
- Context. closePath ();
In this example, move the virtual pen to the coordinate point (10, 10), and draw a straight line from this point to the coordinate point (50, 50.
The lineTo () function also moves the virtual pen to the end point of the execution. In the above example, the position is moved to (50, 50.
Stroke () function + fill () function
You will not actually draw anything on the canvas until you have not notified the 2D context to draw the path. You can use the stroke () or fill () function to notify the 2D context.
The stroke () function is used to operate the outer contour of a specified image in a path.
The fill () function is used to fill in the image specified by the path operation.
The following example shows how to use the stroke () or fill () functions.
- Context. beginPath ();
- Context. moveTo (10, 10 );
- Context. lineTo (60, 50 );
- Context. lineTo (110,50 );
- Context. lineTo (10, 10 );
- Context. stroke ();
- Context. closePath ();
-
- Context. beginPath ();
- Context. moveTo (100,10 );
- Context. lineTo (150,50 );
- Context. lineTo (200,50 );
- Context. lineTo (100,10 );
- Context. fill ();
- Context. closePath ();
The returned results of the code above are as follows:
Line width
You can use the lineWidth attribute of the 2D context to set the width of the drawn line. The following is an example:
- Context. lineWidth = 10;
In the preceding example, the width of the drawn line is set to 10 pixels.
The following example draws three straight lines whose widths are 1, 5 and 10.
When the line width you draw is greater than 1, the extended line width is evenly distributed to both sides of the Line center. For distance, if you draw a straight line from (10, 10) to (), and the width of the line is 10, it is actually drawn from (10, 5, then expand to the () Point and draw horizontally to the (, 5) and () points, just like drawing a rectangle.
LINE CAP)
When using a path to draw a line, you can set the line Header style. The lineCap attribute of the 2D context is used to set the line Header style. It has three optional values:
The butt style line header is flat and orthogonal to the line.
The line header in the round style is the line header with a rounded corner, and the radius of the circle is equal to half of the line width.
The line header of the square style draws a rectangle at the end of the line. The size of the rectangle is: the width of the line X the width of the line/2.
Below are several examples of lines with different line headers. The width of all lines is 10. The lineCap value of the most general line is butt, The lineCap value of the middle line is round, and the lineCap value of the rightmost line is square.
The value of lineCap is very similar to that of square. Sometimes it is difficult to distinguish. Here are a few small examples, from which you can see the tiny differences between them. There are three groups of lines below. The lineCap attribute of each group's left (or top) line is butt, and the lineCap attribute of the right (or bottom) line is square.
As shown above, the line of the square Line header is longer than that of the butt line header.
Line Connection
The lineJoin attribute of the 2D context is used to define how the points at the two line connections are drawn. The point connecting two lines is called a "connection point ". The lineJoin attribute has the following three values:
The following is the sample code for the three values:
- Context. lineJoin = "miter ";
- Context. lineJoin = "bevel ";
- Context. lineJoin = "round ";
The miter connection point is a triangle connection point.
The bevel connection point is a flat connection point.
The round connection point is a rounded corner connection point.
The following is an example of three line join points. The lineJoin attributes from left to right are miter, bevel, and round.
Draw a curve
The arc () function in 2D context can be used to draw a curve. The arc () function has six parameters:
- X: The X coordinate position of the center of the arc.
- Y: Y coordinate of the center of the arc.
- Radius: the radius of the arc.
- StartAngle: The angle radian of the arc.
- EndAngle: The radian of the end of an arc.
- Anticlockwise: draws an arc clockwise or clockwise if it is set to false.
The following is an example code:
- Context. lineWidth = 3;
-
- Var x = 50;
- Var y = 50;
- Var radius = 25;
- Var startAngle = (Math. PI/180) * 45;
- Var endAngle = (Math. PI/180) * 180;
- Var anticlockwise = false;
-
- Context. beginPath ();
- Context. arc (x, y, radius, startAngle, endAngle, anticlockwise );
- Context. stroke ();
- Context. closePath ();
The code above draws an arc with a center point (50, 50) coordinate point with a radius of 25 and ends from 45 degrees to 180 degrees.
The returned results of the above Code are as follows:
In the preceding example, if you set anticlockwise to true, the following result is displayed:
If you want to spend a complete circle, you can simply set startAngle to 0 and endAngle to 2 * Math. PI, which is equivalent to (Math. PI/180) * 360.
ArcTo () function
In the 2D context, there is an arcTo () function, which is used to draw a curve from the current point to the point specified by the parameter. The radius of the curve is also specified by the parameter. Its syntax is: arcTo (x1, y1, x2, y2, radius ). Note: In the parameter, x1, y1, x2, y2 indicates the control point of this point. The arcTo () function can be imitated using the lineTo () and arc functions.
QuadraticCurveTo () function
The quadraticCurveTo () function is used to draw a quadratic Beitz curve. This curve is controlled by a control point. Its syntax is quadraticCurveTo (cp1x, cp1y, x, y ). The following is an example code:
- Context. lineWidth = 3;
-
- Var fromX = 50;
- Var fromY = 50;
- Var tox= 100;
- Var toY = 50;
- Var cpX = 75;
- Var cpY = 100;
-
- Context. beginPath ();
- Context. moveTo (fromX, fromY );
- Context. quadraticCurveTo (cpX, cpY, toX, toY );
- Context. stroke ();
- Context. closePath ();
The code above draws a quadratic beam curve from (50, 50) to (75,100, 50). The control point of this curve is ). The result is as follows:
Take a closer look, there is a small point below the curve, that is the control point of this curve. (This point is for everyone to see)
For details about control points, refer to the following image:
BezierCurveTo () function
The bezierCurveTo () function is used to draw a cubic Beitz curve from one point to another. The cubic Beitz curve has two control points, while the quadratic Beitz curve has only one control point. Its syntax is: bezierCurveTo (cp1x, cp1y, cp2x, cp2y, x, y ). The following is an example:
- Context. lineWidth = 3;
-
- Var fromX = 50;
- Var fromY = 50;
- Var tox= 300;
- Var toY = 50;
- Var cp1X = 100;
- Var cp1Y = 100;
- Var cp2X = 250;
- Var cp2Y = 100;
-
- Context. beginPath ();
- Context. moveTo (fromX, fromY );
- Context. bezierCurveTo (cp1X, cp1Y, cp2X, cp2Y, toX, toY );
- Context. stroke ();
- Context. closePath ();
This Code draws a cubic beam curve from (50, 50) to (100,100, 50). The two control points are: (250,100) and ). The result is as follows:
The two dots below the curve are two control points. They are not part of the curve.
The following is a different example. Its start point and end point are the same as the previous example, but the control point is different from the previous example.
- Context. lineWidth = 3;
-
- Var fromX = 50;
- Var fromY = 50;
- Var tox= 300;
- Var toY = 50;
- Var cp1X = 100;
- Var cp1Y = 10;
- Var cp2X = 250;
- Var cp2Y = 100;
-
- Context. beginPath ();
- Context. moveTo (fromX, fromY );
- Context. bezierCurveTo (cp1X, cp1Y, cp2X, cp2Y, toX, toY );
- Context. stroke ();
- Context. closePath ();
The result of the above Code is as follows:
At the end of the article, we reference an example on MDN, which uses the Canvas path to draw a small scenario of the "douman" game:
The implementation code is as follows:
- Function draw (){
- Var canvas = document. getElementById ('canvas ');
- If (canvas. getContext ){
- Var ctx = canvas. getContext ('2d ');
-
- RoundedRect (ctx, 12,12, 150,150, 15 );
- RoundedRect (ctx, 19, 19, 150,150, 9 );
- RoundedRect (ctx, 53,53, 49,33, 10 );
- RoundedRect (ctx, 53,119, 6 );
- RoundedRect (ctx, 135,53, 49,33, 10 );
- RoundedRect (ctx, 135,119, 10 );
-
- Ctx. beginPath ();
- Ctx. arc (37,37, 13, Math. PI/7,-Math. PI/7, false );
- Ctx. lineTo (31, 37 );
- Ctx. fill ();
-
- For (var I = 0; I <8; I ++ ){
- Ctx. fillRect (51 + I * 16, 35, 4 );
- }
-
- For (I = 0; I <6; I ++ ){
- Ctx. fillRect (115,51 + I * 16,4, 4 );
- }
-
- For (I = 0; I <8; I ++ ){
- Ctx. fillRect (51 + I * 16, 99, 4 );
- }
-
- Ctx. beginPath ();
- Ctx. moveTo (83,116 );
- Ctx. lineTo (83,102 );
- Ctx. bezierCurveTo (83,94, 89,88, 97,88 );
- Ctx. bezierCurveTo (111,102, 88 );
- Ctx. lineTo (111,116 );
- Ctx. lineTo (106.333, 111.333 );
- Ctx. lineTo (101.666, 116 );
- Ctx. lineTo (97,111.333 );
- Ctx. lineTo (92.333, 116 );
- Ctx. lineTo (87.666, 111.333 );
- Ctx. lineTo (83,116 );
- Ctx. fill ();
-
- Ctx. fillStyle = "white ";
- Ctx. beginPath ();
- Ctx. moveTo (91,96 );
- Ctx. bezierCurveTo (87,101 );
- Ctx. bezierCurveTo (87,103, 88,106, 91,106 );
- Ctx. bezierCurveTo (94,106, 95,103, 95,101 );
- Ctx. bezierCurveTo (, 99 );
- Ctx. moveTo );
- Ctx. bezierCurveTo (99,101, 96 );
- Ctx. bezierCurveTo (99,103,100,106,103,106 );
- Ctx. bezierCurveTo (106,106,107,103,107,101 );
- Ctx. bezierCurveTo (107,99, 106,96, 103,96 );
- Ctx. fill ();
-
- Ctx. fillStyle = "black ";
- Ctx. beginPath ();
- Ctx. arc (101,102, Math. PI * 2, true );
- Ctx. fill ();
-
- Ctx. beginPath ();
- Ctx. arc (89,102, Math. PI * 2, true );
- Ctx. fill ();
- }
- }
-
- // A utility function to draw a rectangle with rounded corners.
-
- Function roundedRect (ctx, x, y, width, height, radius ){
- Ctx. beginPath ();
- Ctx. moveTo (x, y + radius );
- Ctx. lineTo (x, y + height-radius );
- Ctx. quadraticCurveTo (x, y + height, x + radius, y + height );
- Ctx. lineTo (x + width-radius, y + height );
- Ctx. quadraticCurveTo (x + width, y + height, x + width, y + height-radius );
- Ctx. lineTo (x + width, y + radius );
- Ctx. quadraticCurveTo (x + width, y, x + width-radius, y );
- Ctx. lineTo (x + radius, y );
- Ctx. quadraticCurveTo (x, y, x, y + radius );
- Ctx. stroke ();
- }
This article copyright belongs to the jQuery house, reproduced please indicate the source: http://www.htmleaf.com/ziliaoku/... g/201507122217.html