HTML5 Canvas is used to draw triangles, rectangles, and other polygon. html5canvas
The main attributes and methods of the CanvasRenderingContext2D object required to draw a polygon using HTML5 Canvas (the method is "()") are as follows:
Attribute or Method |
Basic description |
StrokeStyle |
Used to set the color, gradient, and mode of the paint path. The value of this attribute can be a string that represents the css color value. If your drawing requirements are complex, the value of this attribute can also beCanvasGradient Object orCanvasPattern Object |
GlobalAlpha |
Defines the transparency of the drawn content. The value ranges from 0.0 (completely transparent) to 1.0 (completely opaque). The default value is 1.0. |
LineWidth |
Defines the width of the drawn line. The default value is 1.0, and this attribute must be greater than 0.0. A wide line is centered on the path, and each side has a half width. |
LineCap |
Specifies how to draw the line caps at both ends of a line. Valid values include butt, round, and square. The default value is "butt ". |
BeginPath () |
Start a new drawing path. Call this method before creating a new path. |
MoveTo (int x, int y) |
Define the start coordinate of a new draw path |
LineTo (int x, int y) |
Defines the coordinates of the intermediate point of a drawn path |
Stroke (int x, int y) |
Draw a straight line along the coordinate point sequence of the draw path |
ClosePath () |
If the current drawing path is open, the painting path is closed. |
Draw triangles
Copy the content to the clipboard using JavaScript Code
- <! DOCTYPE html>
- <Html>
- <Head>
- <Meta charset = "UTF-8">
- <Title> HTML5 Canvas triangle drawing example </title>
- </Head>
- <Body>
- <! -- Add a canvas label and a red border to view the canvas -->
- <Canvas id = "myCanvas" width = "400px" height = "300px" style = "border: 1px solid red;">
- Your browser does not support canvas labels.
- </Canvas>
- <Script type = "text/javascript">
- // Obtain the Canvas object (Canvas)
- Var canvas = document. getElementById ("myCanvas ");
- // Check whether the current browser supports Canvas objects to avoid syntax errors in some browsers that do not support html5.
- If (canvas. getContext ){
- // Obtain the corresponding CanvasRenderingContext2D object (paint brush)
- Var ctx = canvas. getContext ("2d ");
- // Start a new drawing path
- Ctx. beginPath ();
- // Set the line color to blue
- Ctx. strokeStyle = "blue ";
- // Set the path start Coordinate
- Ctx. moveTo (20, 50 );
- // Draw a line segment to coordinate point (60, 50)
- Ctx. lineTo (20,100 );
- // Draw a line segment to coordinate point (60, 90)
- Ctx. lineTo (70,100 );
- // Close the painting path first. Note that the current endpoint and start endpoint are connected in a straight line.
- Ctx. closePath ();
- // Finally, draw a straight line based on the drawing path
- Ctx. stroke ();
- }
- </Script>
- </Body>
- </Html>
The corresponding display effect is as follows:
Draw a rectangle
The reason for drawing a rectangle in Canvas is that the CanvasRenderingContext2D object, the paint brush tool of Canvas, provides a dedicated Method for drawing a rectangle.
Copy XML/HTML Code to clipboard
- <! DOCTYPE html>
- <Html>
- <Head>
- <Meta charset = "UTF-8">
- <Title> HTML5 Canvas getting started with rectangle painting </title>
- </Head>
- <Body>
- <! -- Add a canvas label and a red border to view the canvas -->
- <Canvas id = "myCanvas" width = "400px" height = "300px" style = "border: 1px solid red;">
- Your browser does not support canvas labels.
- </Canvas>
- <Script type = "text/javascript">
- // Obtain the Canvas object (Canvas)
- Var canvas = document. getElementById ("myCanvas ");
- // Check whether the current browser supports Canvas objects to avoid syntax errors in some browsers that do not support html5.
- If (canvas. getContext ){
- // Obtain the corresponding CanvasRenderingContext2D object (paint brush)
- Var ctx = canvas. getContext ("2d ");
- // Start a new drawing path
- Ctx. beginPath ();
- // Set the line color to blue
- Ctx. strokeStyle = "blue ";
- // Use the coordinate point (10, 10) in the canvas as the starting point to draw a rectangle with a width of 80 PX and a height of 50 PX.
- Ctx. rect (10, 10, 80, 50 );
- // Draw a straight line according to the specified path
- Ctx. stroke ();
- // Close the painting path
- Ctx. closePath ();
- }
- </Script>
- </Body>
- </Html>
The corresponding rectangle is shown as follows: