Draw line segments for HTML5 canvas basic plotting

Source: Internet
Author: User

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
  1. <! DOCTYPE html>
  2. <Html lang = "en">
  3. <Head>
  4. <Meta charset = "UTF-8">
  5. <Title> canvas drawing demonstration </title>
  6. <Style type = "text/css">
  7. # Canvas {
  8. Border: 1px solid # ADACB0;
  9. Display: block;
  10. Margin: 20px auto;
  11. }
  12. </Style>
  13. </Head>
  14. <Body>
  15. <Canvas id = "canvas" width = "300" height = "300">
  16. Your browser does not support canvas.
  17. </Canvas>
  18. </Body>
  19. <Script type = "text/javascript">
  20. Var canvas = document. getElementById ("canvas ");
  21. Var context = canvas. getContext ("2d ");
  22. // Set the object start point and end point
  23. Context. moveTo (10, 10 );
  24. Context. lineTo (200,200 );
  25. // Set the style
  26. Context. lineWidth = 2;
  27. Context. strokeStyle = "# F5270B ";
  28. // Draw
  29. Context. stroke ();
  30. </Script>
  31. </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
  1. <Script type = "text/javascript">
  2. Var canvas = document. getElementById ("canvas ");
  3. Var context = canvas. getContext ("2d ");
  4. // Set the object start point and end point
  5. Context. beginPath ();
  6. Context. moveTo (100,100 );
  7. Context. lineTo (700,100 );
  8. Context. lineTo (700,400 );
  9. Context. lineWidth = 2;
  10. Context. strokeStyle = "# F5270B ";
  11. // Draw
  12. Context. stroke ();
  13. Context. beginPath ();
  14. Context. moveTo (100,200); // the change of moveTo to lineTo is the same.
  15. Context. lineTo (600,200 );
  16. Context. lineTo (600,400 );
  17. // If the color of strokeStyle has a new value, it overwrites the value set above.
  18. // If no new value exists in lineWidth, it is displayed according to the value set above.
  19. Context. strokeStyle = "#0D25F6 ";
  20. // Draw
  21. Context. stroke ();
  22. </Script>

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.