Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use cavas to draw a national flag in html5

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "how to draw a national flag with cavas in html5". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to draw a national flag with cavas in html5" can help you solve your doubts.

The specific code is as follows:

Var canvas = document.getElementById ("canvas"); / / create a cavas artboard var context = canvas.getContext ('2d'); / / set the artboard properties var width=canvas.width; var height=width*2/3; context.fillStyle= "red"; context.fillRect (0meme 0wthdepartment height); var maxR = 0.15, minR = 0.05; var maxX = 0.55, maxY = 0.35 / / the position of the big five stars var minX = [0.80,0.90,0.90,0.80 0.65 maxX 0.50, 0.40]; / / the x coordinates of each small five-pointed star var minY = [0.20,0.30,0.45,0.55 0.600.50]; / / the y coordinates of each small five-pointed star var ox = height * maxX, oy = height * maxY

Html Code:

Js Code:

Create5star (context,ox,oy,height * maxR, "# ff0", 0); / / draw the big pentagram for (var idx = 0; idx < 7; idx++) {var sx = minX [idx] * height, sy = minY [idx] * height; var theta = Math.atan ((oy-sy) / (ox-sx)); create5star (context,sx, sy, height * minR, "# ff0",-Math.PI/2+theta) / / draw small five-pointed star} / / draw five-pointed star function create5star (context,sx,sy,radius,color,rotato) {context.save (); context.fillStyle=color; context.translate (sx,sy); / / move coordinate origin context.rotate (Math.PI+rotato); / / rotate context.beginPath (); / / create path var x = Math.sin (0) Var y = Math.cos (0); var dig = Math.PI/5 * 4; for (var I = 0 Bing I < 5 var I +) {/ / draw the five sides of a pentagram var x = Math.sin (i*dig); var y = Math.cos (i*dig); context.lineTo (x = radius);} context.closePath () Context.stroke (); context.fill (); context.restore ();} read here, this article "how to draw a national flag with cavas in html5" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it before you can understand it. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report