HTML5 canvas Basic Drawing: Drawing five-star, html5canvas

Source: Internet
Author: User

HTML5 canvas Basic Drawing: Drawing five-star, 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.

Through the analysis of the five-pointed star, we can determine the coordinates of each vertex. Note that in the canvas, the Y axis is downward.

The Code is as follows:

Copy the content to the clipboard using JavaScript Code
  1. Var canvas = document. getElementById ("canvas ");
  2. Var context = canvas. getContext ("2d ");
  3. Context. beginPath ();
  4. // Set the coordinates of a vertex and specify the path based on the vertex.
  5. For (var I = 0; I <5; I ++ ){
  6. Context. lineTo (Math. cos (18 + I * 72)/180 * Math. PI) * 200 + 200,
  7. -Math. sin (18 + I * 72)/180 * Math. PI) * 200 + 200 );
  8. Context. lineTo (Math. cos (54 + I * 72)/180 * Math. PI) * 80 + 200,
  9. -Math. sin (54 + I * 72)/180 * Math. PI) * 80 + 200 );
  10. }
  11. Context. closePath ();
  12. // Set the border style and fill color
  13. Context. lineWidth = "3 ";
  14. Context. fillStyle = "# F6F152 ";
  15. Context. strokeStyle = "# F5270B ";
  16. Context. fill ();
  17. Context. stroke ();

Final effect:

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.