Draw line segments for HTML5 canvas basic plotting
<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 line lines moveTo () and lineTo ()
The following is a simple <canvas> drawing example:
Copy XML/HTML Code to clipboard
- <! DOCTYPE html>
- <Html lang = "en">
- <Head>
- <Meta charset = "UTF-8">
- <Title> canvas drawing demonstration </title>
- <Style type = "text/css">
- # Canvas {
- Border: 1px solid # ADACB0;
- Display: block;
- Margin: 20px auto;
- }
- </Style>
- </Head>
- <Body>
- <Canvas id = "canvas" width = "300" height = "300">
- Your browser does not support canvas.
- </Canvas>
- </Body>
- <Script type = "text/javascript">
- Var canvas = document. getElementById ("canvas ");
- Var context = canvas. getContext ("2d ");
- // Set the object start point and end point
- Context. moveTo (10, 10 );
- Context. lineTo (200,200 );
- // Set the style
- Context. lineWidth = 2;
- Context. strokeStyle = "# F5270B ";
- // Draw
- Context. stroke ();
- </Script>
- </Html>
If it is not specified through moveTo (), the starting point of lineTo () is based on the previous vertex. Therefore, if you need to reselect the start point, you need to use the moveTo () method. If you need to set styles for different line segments, you need to re-enable a path through context. beginPath (). The following is an example:
Copy the content to the clipboard using JavaScript Code
- <Script type = "text/javascript">
- Var canvas = document. getElementById ("canvas ");
- Var context = canvas. getContext ("2d ");
- // Set the object start point and end point
- Context. beginPath ();
- Context. moveTo (100,100 );
- Context. lineTo (700,100 );
- Context. lineTo (700,400 );
- Context. lineWidth = 2;
- Context. strokeStyle = "# F5270B ";
- // Draw
- Context. stroke ();
- Context. beginPath ();
- Context. moveTo (100,200); // the change of moveTo to lineTo is the same.
- Context. lineTo (600,200 );
- Context. lineTo (600,400 );
- // If the color of strokeStyle has a new value, it overwrites the value set above.
- // If no new value exists in lineWidth, it is displayed according to the value set above.
- Context. strokeStyle = "#0D25F6 ";
- // Draw
- Context. stroke ();
- </Script>
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.