Get the App
SLTechnology News&Howtos  ›  Development  › 

How to draw polygons with Canvas in html5

Shulou Source: shulou.com Published: 2022-06-01 13:34:49 11月15日 Update

Today Xiaobian to share with you how to use Canvas to draw polygons in html5 related knowledge points, detailed content, clear logic, I believe that most people are too aware of this knowledge, so share this article for your reference, I hope you will learn something after reading this article, let's take a look at it.

Preface

Canvas drawing multi-deformation is very simple, as long as understand the Canvas path + simple junior high school mathematics knowledge can be completed.

Analysis

Calculate the position of each point and draw the path by lineTo ().

The core code is parsed as follows (or viewed in CodePen):

Function drawPolygonPath (sideNum, radius, originX, originY, ctx) {ctx.beginPath (); const unitAngle = Math.PI * 2 / sideNum; / / Cell Angle let angle = 0; / / initial Angle let xLength, yLength; / / ctx.moveTo (originX, originY); for (let I = 0; I < sideNum; iTunes +) {/ / traverse the calculation point and lineTo () draws the path xLength = radius * Math.cos (angle); yLength = radius * Math.sin (angle) Ctx.lineTo (originX + xLength, originY-yLength); / / draw the path angle + = unitAngle;} ctx.closePath (); / / close the path, or cycle lineTo () to the starting point again in the for loop. Above is all the content of the article "how to draw polygons with Canvas in html5". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Tags: Knowledge path article polygon content angle loop different big code location junior high school preface unit available most that is math more core Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information vpn MariaDB Docker MySQL