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
- Var canvas = document. getElementById ("canvas ");
- Var context = canvas. getContext ("2d ");
- Context. strokeStyle = "# F22D0D ";
- Context. lineWidth = "2 ";
- // Draw a circle
- Context. beginPath ();
- Context. arc (100,100, 40, 0, 2 * Math. PI );
- Context. stroke ();
- // Draw a semi-circle
- Context. beginPath ();
- Context. arc (200,100, Math. PI );
- Context. stroke ();
- // Draw a semi-circle, counterclockwise
- Context. beginPath ();
- Context. arc (300,100, Math. PI, true );
- Context. stroke ();
- // Draw a closed semi-circle
- Context. beginPath ();
- Context. arc (400,100, Math. PI );
- Context. closePath ();
- 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
- Function createRoundRect (context, x1, y1, width, height, radius)
- {
- // Move to the upper left corner
- Context. moveTo (x1 + radius, y1 );
- // Add a line segment connected to the upper-right corner
- Context. lineTo (x1 + width-radius, y1 );
- // Add an arc
- Context. arcTo (x1 + width, y1, x1 + width, y1 + radius, radius );
- // Add a line segment connected to the bottom right corner
- Context. lineTo (x1 + width, y1 + height-radius );
- // Add an arc
- Context. arcTo (x1 + width, y1 + height, x1 + width-radius, y1 + height, radius );
- // Add a line segment connected to the lower left corner
- Context. lineTo (x1 + radius, y1 + height );
- // Add an arc
- Context. arcTo (x1, y1 + height, x1, y1 + height-radius, radius );
- // Add a line segment connected to the upper left corner
- Context. lineTo (x1, y1 + radius );
- // Add an arc
- Context. arcTo (x1, y1, x1 + radius, y1, radius );
- Context. closePath ();
- }
- // Obtain the DOM object corresponding to the canvas Element
- Var canvas = document. getElementById ('mc ');
- // Obtain the CanvasRenderingContext2D object drawn on the canvas
- Var context = canvas. getContext ('2d ');
- Context. lineWidth = 3;
- Context. strokeStyle = "# F9230B ";
- CreateRoundRect (context, 30, 30,400,200, 50 );
- 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.