HTML5 Canvas is used to draw triangles, rectangles, and other polygon. html5canvas

Source: Internet
Author: User

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 beCanvasGradientObject orCanvasPatternObject
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
  1. <! DOCTYPE html>
  2. <Html>
  3. <Head>
  4. <Meta charset = "UTF-8">
  5. <Title> HTML5 Canvas triangle drawing example </title>
  6. </Head>
  7. <Body>
  8. <! -- Add a canvas label and a red border to view the canvas -->
  9. <Canvas id = "myCanvas" width = "400px" height = "300px" style = "border: 1px solid red;">
  10. Your browser does not support canvas labels.
  11. </Canvas>
  12. <Script type = "text/javascript">
  13. // Obtain the Canvas object (Canvas)
  14. Var canvas = document. getElementById ("myCanvas ");
  15. // Check whether the current browser supports Canvas objects to avoid syntax errors in some browsers that do not support html5.
  16. If (canvas. getContext ){
  17. // Obtain the corresponding CanvasRenderingContext2D object (paint brush)
  18. Var ctx = canvas. getContext ("2d ");
  19. // Start a new drawing path
  20. Ctx. beginPath ();
  21. // Set the line color to blue
  22. Ctx. strokeStyle = "blue ";
  23. // Set the path start Coordinate
  24. Ctx. moveTo (20, 50 );
  25. // Draw a line segment to coordinate point (60, 50)
  26. Ctx. lineTo (20,100 );
  27. // Draw a line segment to coordinate point (60, 90)
  28. Ctx. lineTo (70,100 );
  29. // Close the painting path first. Note that the current endpoint and start endpoint are connected in a straight line.
  30. Ctx. closePath ();
  31. // Finally, draw a straight line based on the drawing path
  32. Ctx. stroke ();
  33. }
  34. </Script>
  35. </Body>
  36. </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
  1. <! DOCTYPE html>
  2. <Html>
  3. <Head>
  4. <Meta charset = "UTF-8">
  5. <Title> HTML5 Canvas getting started with rectangle painting </title>
  6. </Head>
  7. <Body>
  8. <! -- Add a canvas label and a red border to view the canvas -->
  9. <Canvas id = "myCanvas" width = "400px" height = "300px" style = "border: 1px solid red;">
  10. Your browser does not support canvas labels.
  11. </Canvas>
  12. <Script type = "text/javascript">
  13. // Obtain the Canvas object (Canvas)
  14. Var canvas = document. getElementById ("myCanvas ");
  15. // Check whether the current browser supports Canvas objects to avoid syntax errors in some browsers that do not support html5.
  16. If (canvas. getContext ){
  17. // Obtain the corresponding CanvasRenderingContext2D object (paint brush)
  18. Var ctx = canvas. getContext ("2d ");
  19. // Start a new drawing path
  20. Ctx. beginPath ();
  21. // Set the line color to blue
  22. Ctx. strokeStyle = "blue ";
  23. // 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.
  24. Ctx. rect (10, 10, 80, 50 );
  25. // Draw a straight line according to the specified path
  26. Ctx. stroke ();
  27. // Close the painting path
  28. Ctx. closePath ();
  29. }
  30. </Script>
  31. </Body>
  32. </Html>

The corresponding rectangle is shown as follows:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.