Among the choices among many progress bar shapes, everyone will choose the donut chart. So today I will teach you how to use canvas to draw a rounded ring chart and the solution to the blurred progress bar. I hope it will be helpful to you
* @param {type} radius 圆环半径 * @param {type} lineWidth 圆环宽度 * @param {type} strokeStyle 默认背景 * @param {type} fillStyleArray 数组,圆环色块颜色 * @param {type} capType 类型:round是圆角,square正方形顶帽,butt是正常 * @param {type} percentArray ,数字,每个占据的百分比 * @param {type} startAngle 开始的角度 * @param {type} criclex,cricley 圆心坐标,一般是canvas的一半,例如:canvas给的宽度是250,高度是250,那么criclex是125
How to use
var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); var ring = new Ring("80", "25", "#ccc", ["#a1b91d", "#e9636a", "#e7ba21"], "round"); ring.drawRing(ctx, 2 * Math.PI / 3, [20, 50, 30],125,125);//占据的百分比分别是20%,50%,30%
Source code
The source code is very simple, everyone is welcome to expand!
function Circle(radius, lineWidth, strokeStyle, fillStyleArray, capType) { this.radius = radius; // 圆环半径 this.lineWidth = lineWidth; // 圆环边的宽度 this.strokeStyle = strokeStyle; //边的颜色 this.fillStyle = fillStyleArray; //填充色 this.lineCap = capType;}Circle.prototype.draw = function (ctx,criclex,cricley) { ctx.beginPath(); ctx.arc(criclex, cricley, this.radius, 0, Math.PI * 2, true); // 坐标为90的圆,这里起始角度是0,结束角度是Math.PI*2 ctx.lineWidth = this.lineWidth; ctx.strokeStyle = this.strokeStyle; ctx.stroke(); // 这里用stroke画一个空心圆,想填充颜色的童鞋可以用fill方法};function Ring(radius, lineWidth, strokeStyle, fillStyleArray, capType) { Circle.call(this, radius, lineWidth, strokeStyle, fillStyleArray, capType);}Ring.prototype = Object.create(Circle.prototype);Ring.prototype.drawRing = function (ctx, startAngle, percentArray ,criclex,cricley) { startAngle = startAngle || 3 * Math.PI / 2; percentArray = percentArray || []; this.draw(ctx,criclex,cricley); // 调用Circle的draw方法画圈圈 var _this = this; // angle percentArray.forEach(function (item, index) { ctx.beginPath(); var anglePerSec = 2 * Math.PI / (100 / item); // 蓝色的弧度 ctx.arc(criclex, cricley, _this.radius, startAngle, startAngle + anglePerSec, false); //这里的圆心坐标要和cirle的保持一致 startAngle = startAngle + anglePerSec; ctx.strokeStyle = _this.fillStyle[index]; ctx.lineCap = _this.lineCap; ctx.stroke(); ctx.closePath(); }) //小圆圈覆盖 ctx.beginPath(); ctx.arc(criclex, cricley, _this.radius, startAngle, startAngle, false); //这里的圆心坐标要和cirle的保持一致 ctx.strokeStyle = _this.fillStyle[0]; ctx.lineCap = _this.lineCap; ctx.stroke(); ctx.closePath();}
I believe you have mastered the methods after reading these cases. For more exciting information, please pay attention to other related articles on the php Chinese website!
Related reading:
How to make flying butterflies in CSS3 Animation
How to realize the animation of picture cover display in css3
The above is the detailed content of How to draw a rounded circular shape. For more information, please follow other related articles on the PHP Chinese website!

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version
Recommended: Win version, supports code prompts!

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment