Rendering shadow effect of HTML5 canvas Basic Drawing, 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.
Shadow rendering:
- ShadowColor sets or returns the color used for shadow.
- ShadowBlur sets or returns the Blur level used for shadow (the greater the value, the blurrier the value ).
- ShadowOffsetX sets or returns the horizontal distance between the shadow and the shape.
- ShadowOffsetY sets or returns the vertical distance between the shadow and the shape.
Let's add the previously drawn pentagram.Shadow
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. shadowColor = "# F7F2B4 ";
- Context. shadowOffsetX = 30;
- Context. shadowOffsetY = 30;
- Context. shadowBlur = 2;
- Context. fill ();
- Context. stroke ();
The effect is as follows:
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.