Draw lines 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.
Line attributes
In addition to the lineWidth attribute used above, the line also has the following attributes:
• LineCapSet the property or return the style of the end line cap. You can set the following values:
"Butt" adds straight edges to each end of the line (default );
"Round" adds a Circle Line cap to each end of the line;
Square adds a square Line cap to each end of the line.
• LineJoinSet or return the type of the created edge when two lines converge. You can set the following values:
"Miter" creates a tip (default );
"Bevel" creates an angle;
"Round" creates a rounded corner.
• MiterLimitSet the property or return the maximum length (10 by default ). The oblique length refers to the distance between the inner and outer corners of the interchange of two wires. MiterLimit is valid only when the lineJoin attribute is "miter.
Copy the content to the clipboard using JavaScript Code
- Var canvas = document. getElementById ("canvas ");
- Var context = canvas. getContext ("2d ");
- // Test lineCap attributes
- // Set the baseline for easy observation
- Context. moveTo (10, 10 );
- Context. lineTo (10,200 );
- Context. moveTo (200,10 );
- Context. lineTo (200,200 );
- Context. lineWidth = "1 ";
- Context. stroke ();
- // Butt
- Context. beginPath ();
- Context. moveTo (10, 50 );
- Context. lineTo (200,50 );
- Context. lineCap = "butt ";
- Context. lineWidth = "10 ";
- Context. stroke ();
- // Round
- Context. beginPath ();
- Context. moveTo (10,100 );
- Context. lineTo (200,100 );
- Context. lineCap = "round ";
- Context. lineWidth = "10 ";
- Context. stroke ();
- // Square
- Context. beginPath ();
- Context. moveTo (10,150 );
- Context. lineTo (200,150 );
- Context. lineCap = "square ";
- Context. lineWidth = "10 ";
- Context. stroke ();
- // Test the linJoin attribute
- // Miter
- Context. beginPath ();
- Context. moveTo );
- Context. lineTo (450,100 );
- Context. lineTo (300,150 );
- Context. lineJoin = "miter ";
- Context. lineWidth = "10 ";
- Context. stroke ();
- // Round
- Context. beginPath ();
- Context. moveTo (400,50 );
- Context. lineTo (550,100 );
- Context. lineTo (400,150 );
- Context. lineJoin = "round ";
- Context. lineWidth = "10 ";
- Context. stroke ();
- // Square
- Context. beginPath ();
- Context. moveTo (, 50 );
- Context. lineTo (650,100 );
- Context. lineTo (500,150 );
- Context. lineJoin = "bevel ";
- Context. lineWidth = "10 ";
- Context. stroke ();
- // Test the miterLimit attribute
- Context. beginPath ();
- Context. moveTo (700,50 );
- Context. lineTo (850,100 );
- Context. lineTo (700,150 );
- Context. lineJoin = "miter ";
- Context. miterLimit = "2 ";
- Context. lineWidth = "10 ";
- Context. strokeStyle = "# 2913EC ";
- Context. stroke ();
The effects of different values of each attribute are as follows:
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.