Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use cavas to draw a national flag in html5

Shulou Source: shulou.com Published: 2022-06-01 13:13:50 09月17日 Update

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.

Tags: Pentagram pentagram one side national flag code coordinates article content artboard proper location origin attributes ideas new knowledge more steps knowledge knowledge points articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Xiaomi Redmi Apple Microsoft