Example of how to draw canvas polygons
This article mainly introduces the relevant information about canvas polygon (spider diagram) drawing examples. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.
How to draw spider diagrams:
Before we start, we need to know how canvas draws images. The canvas element is used to draw graphics on web pages. HTML5's canvas element uses JavaScript to draw 2D images on a web page. On the canvas of the rectangular area, control each pixel, and use JavaScript to draw 2D graphics and render them pixel by pixel. There are many ways to use the canvas element to draw paths, rectangles, circles, characters, and add images.
* Notice! ! ! The canvas tag itself does not have drawing functions and can only use JavaScript to draw images on web pages.
The rendering is as follows:
1. Initialization js code
//初始化 (function(){ var canvas = document.createElement('canvas'); document.body.appendChild(canvas); canvas.height = mH; canvas.width = mW; mCtx = canvas.getContext('2d'); drawPolygon(mCtx); // 绘制多边形边 drawLines(mCtx); //顶点连线 drawText(mCtx); // 绘制文本 drawRegion(mCtx); // 绘制数据 drawCircle(mCtx); // 画数据圆点 })();
The above code , all settings are initialized through an immediate execution function. For how to draw a regular hexagon on canvas, see Drawing a regular hexagon on canvas
In the spider diagram, we can split it, By drawing hexagons, straight lines, and circles, separate complete individual components, and then call and draw them uniformly through methods
The source code is as shown below:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>蜘蛛图canvas</title> <style type="text/css"> canvas{ } </style> </head> <body> <script type="text/javascript"> var mW = 400; var mH = 400; var mData = [['法力', 77],['防御', 72],['生命值', 46],['物理伤害', 50],['回复值', 80],['耐力', 60]]; var mCount = mData.length; //边数 var mCenter = mW /2; //中心点 var mRadius = mCenter - 100; //半径(减去的值用于给绘制的文本留空间) var mAngle = Math.PI * 2 / mCount; //角度 var mCtx = null; var mColorPolygon = '#B8B8B8'; //多边形颜色 var mColorLines = '#B8B8B8'; //顶点连线颜色 var mColorText = '#000000'; //初始化 (function(){ var canvas = document.createElement('canvas'); document.body.appendChild(canvas); canvas.height = mH; canvas.width = mW; mCtx = canvas.getContext('2d'); drawPolygon(mCtx); drawLines(mCtx); drawText(mCtx); drawRegion(mCtx); drawCircle(mCtx); })(); // 绘制多边形边 function drawPolygon(ctx){ ctx.save(); // save the default state ctx.strokeStyle = mColorPolygon; var r = mRadius/ mCount; //单位半径 //画6个圈 for(var i = 0; i < mCount; i ++){ ctx.beginPath(); //开始路径 var currR = r * ( i + 1); //当前半径 //画6条边 for(var j = 0; j < mCount; j ++) { var x = mCenter + currR * Math.cos(mAngle * j); var y = mCenter + currR * Math.sin(mAngle * j); console.log('x:' + x, 'y:' + y); ctx.lineTo(x, y); } ctx.closePath(); //闭合路径 ctx.stroke(); } ctx.restore(); // restore to the default state } //顶点连线 function drawLines(ctx){ ctx.save(); ctx.beginPath(); ctx.strokeStyle = mColorLines; for(var i = 0; i < mCount; i ++){ var x = mCenter + mRadius * Math.cos(mAngle * i); var y = mCenter + mRadius * Math.sin(mAngle * i); ctx.moveTo(mCenter, mCenter); ctx.lineTo(x, y); } ctx.stroke(); ctx.restore(); } //绘制文本 function drawText(ctx){ ctx.save(); var fontSize = mCenter / 12; ctx.font = fontSize + 'px Microsoft Yahei'; ctx.fillStyle = mColorText; for(var i = 0; i < mCount; i ++){ var x = mCenter + mRadius * Math.cos(mAngle * i); var y = mCenter + mRadius * Math.sin(mAngle * i); if( mAngle * i >= 0 && mAngle * i <= Math.PI / 2 ){ ctx.fillText(mData[i][0], x, y + fontSize); }else if(mAngle * i > Math.PI / 2 && mAngle * i <= Math.PI){ ctx.fillText(mData[i][0], x - ctx.measureText(mData[i][0]).width, y + fontSize); }else if(mAngle * i > Math.PI && mAngle * i <= Math.PI * 3 / 2){ ctx.fillText(mData[i][0], x - ctx.measureText(mData[i][0]).width, y); }else{ ctx.fillText(mData[i][0], x, y); } } ctx.restore(); } //绘制数据区域 function drawRegion(ctx){ ctx.save(); ctx.beginPath(); for(var i = 0; i < mCount; i ++){ var x = mCenter + mRadius * Math.cos(mAngle * i) * mData[i][1] / 100; var y = mCenter + mRadius * Math.sin(mAngle * i) * mData[i][1] / 100; ctx.lineTo(x, y); } ctx.closePath(); ctx.fillStyle = 'rgba(255, 0, 0, 0.5)'; ctx.fill(); ctx.restore(); } //画点 function drawCircle(ctx){ ctx.save(); var r = mCenter / 18; for(var i = 0; i < mCount; i ++){ var x = mCenter + mRadius * Math.cos(mAngle * i) * mData[i][1] / 100; var y = mCenter + mRadius * Math.sin(mAngle * i) * mData[i][1] / 100; ctx.beginPath(); ctx.arc(x, y, r, 0, Math.PI * 2); ctx.fillStyle = 'rgba(255, 0, 0, 0.8)'; ctx.fill(); } ctx.restore(); } </script> </body> </html>
Related recommendations:
CSS3 instance of creating polygon clip property
Determine whether a point falls within a polygon in Mysql_MySQL
The above is the detailed content of Example of how to draw canvas polygons. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Introduction to Python functions: Introduction and examples of exec function Introduction: In Python, exec is a built-in function that is used to execute Python code stored in a string or file. The exec function provides a way to dynamically execute code, allowing the program to generate, modify, and execute code as needed during runtime. This article will introduce how to use the exec function and give some practical code examples. How to use the exec function: The basic syntax of the exec function is as follows: exec

The DECODE function in Oracle is a conditional expression that is often used to return different results based on different conditions in query statements. This article will introduce the syntax, usage and sample code of the DECODE function in detail. 1. DECODE function syntax DECODE(expr,search1,result1[,search2,result2,...,default]) expr: the expression or field to be compared. search1,

Introduction to Python functions: Usage and examples of the isinstance function Python is a powerful programming language that provides many built-in functions to make programming more convenient and efficient. One of the very useful built-in functions is the isinstance() function. This article will introduce the usage and examples of the isinstance function and provide specific code examples. The isinstance() function is used to determine whether an object is an instance of a specified class or type. The syntax of this function is as follows

Indentation specifications and examples of Go language Go language is a programming language developed by Google. It is known for its concise and clear syntax, in which indentation specifications play a crucial role in the readability and beauty of the code. effect. This article will introduce the indentation specifications of the Go language and explain in detail through specific code examples. Indentation specifications In the Go language, tabs are used for indentation instead of spaces. Each level of indentation is one tab, usually set to a width of 4 spaces. Such specifications unify the coding style and enable teams to work together to compile

Explore the Canvas framework: To understand what are the commonly used Canvas frameworks, specific code examples are required. Introduction: Canvas is a drawing API provided in HTML5, through which we can achieve rich graphics and animation effects. In order to improve the efficiency and convenience of drawing, many developers have developed different Canvas frameworks. This article will introduce some commonly used Canvas frameworks and provide specific code examples to help readers gain a deeper understanding of how to use these frameworks. 1. EaselJS framework Ea

Introduction to Python functions: functions and examples of the eval function In Python programming, the eval function is a very useful function. The eval function can execute a string as program code, and its function is very powerful. In this article, we will introduce the detailed functions of the eval function, as well as some usage examples. 1. Function of eval function The function of eval function is very simple. It can execute a string as Python code. This means that we can convert a string

Application and example analysis of PHP dot operator In PHP, the dot operator (".") is an operator used to connect two strings. It is very commonly used and very flexible when concatenating strings. By using the dot operator, we can easily concatenate multiple strings to form a new string. The following will introduce the use of PHP dot operators through example analysis. 1. Basic usage First, let’s look at a basic usage example. Suppose there are two variables $str1 and $str2, which store two words respectively.

Introduction to Python functions: functions and examples of sorted functions Python is a very powerful programming language with a wealth of built-in functions and modules. In this series of articles, we will introduce the commonly used functions of Python one by one and provide corresponding examples to help readers better understand and apply these functions. This article will introduce the functions and examples of the sorted function in detail. The sorted function is used to sort an iterable object and return a new sorted list. Can be used for numbers and words
