How to draw a rounded circular shape

php中世界最好的语言
Release: 2017-11-27 10:33:45
Original
2310 people have browsed it

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
Copy after login

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%
Copy after login


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();}
Copy after login

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 convert CSS encoding

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template