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
- Var canvas = document. getElementById ("canvas ");
- Var context = canvas. getContext ("2d ");
- Context. beginPath ();
- // Set the coordinates of a vertex and specify the path based on the vertex.
- For (var I = 0; I <5; I ++ ){
- Context. lineTo (Math. cos (18 + I * 72)/180 * Math. PI) * 200 + 200,
- -Math. sin (18 + I * 72)/180 * Math. PI) * 200 + 200 );
- Context. lineTo (Math. cos (54 + I * 72)/180 * Math. PI) * 80 + 200,
- -Math. sin (54 + I * 72)/180 * Math. PI) * 80 + 200 );
- }
- Context. closePath ();
- // Set the border style and fill color
- Context. lineWidth = "3 ";
- Context. fillStyle = "# F6F152 ";
- Context. strokeStyle = "# F5270B ";
- Context. fill ();
- 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.