canvas实现圆弧、圆环进度条的案例分析-创新互联

这篇文章主要介绍了canvas实现圆弧、圆环进度条的案例分析,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获。下面让小编带着大家一起了解一下。

在金凤等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供网站建设、网站制作 网站设计制作按需定制开发,公司网站建设,企业网站建设,品牌网站制作,全网营销推广,外贸网站建设,金凤网站建设费用合理。

此方法通过canvas绘制圆形的方法来实现动态圆环进度条,直接上代码,疑问请看注释:

HTML代码如下, 在页面里创建一个画布即可:  

<canvas id="canvas" width="300" height="300">
    <p>抱歉,您的浏览器不支持canvas</p>
 </canvas>

JS分两大部分,
第一部分实现整体功能,第二部分调用:
第一部分:
第一部分功能原理大概是,画两个圆,一个是底色圆,第二个是动态加载的圆弧,进度通过定时器加载;颜色加渐变色;

function toCanvas(id ,progress){                canvas进度条
                var canvas = document.getElementById(id),
                ctx = canvas.getContext("2d"),
                percent = progress,  最终百分比                circleX = canvas.width / 2,  中心x坐标                circleY = canvas.height / 2,  中心y坐标                radius = 100, 圆环半径                lineWidth = 5,  圆形线条的宽度                fontSize = 20; 字体大小
                 两端圆点
                function smallcircle1(cx, cy, r) {
                    ctx.beginPath();                    //ctx.moveTo(cx + r, cy);                    ctx.lineWidth = 1;
                    ctx.fillStyle = '#06a8f3';
                    ctx.arc(cx, cy, r,0,Math.PI*2);
                    ctx.fill();
                }                 function smallcircle2(cx, cy, r) {
                     ctx.beginPath();                     //ctx.moveTo(cx + r, cy);                     ctx.lineWidth = 1;
                     ctx.fillStyle = '#00f8bb';
                     ctx.arc(cx, cy, r,0,Math.PI*2);
                     ctx.fill();
                 }                 画圆
                 function circle(cx, cy, r) {
                     ctx.beginPath();                     //ctx.moveTo(cx + r, cy);                     ctx.lineWidth = lineWidth;
                     ctx.strokeStyle = '#eee';
                     ctx.arc(cx, cy, r, Math.PI*2/3, Math.PI * 1/3);
                     ctx.stroke();
                 }                 画弧线
                 function sector(cx, cy, r, startAngle, endAngle, anti) {
                     ctx.beginPath();                     //ctx.moveTo(cx, cy + r); // 从圆形底部开始画                     ctx.lineWidth = lineWidth;                     // 渐变色 - 可自定义
                     var linGrad = ctx.createLinearGradient(
                         circleX-radius-lineWidth, circleY, circleX+radius+lineWidth, circleY
                     );
                     linGrad.addColorStop(0.0, '#06a8f3');                     //linGrad.addColorStop(0.5, '#9bc4eb');                     linGrad.addColorStop(1.0, '#00f8bb');
                     ctx.strokeStyle = linGrad;                      圆弧两端的样式                     ctx.lineCap = 'round';                     圆弧                     ctx.arc(
                         cx, cy, r,
                         (Math.PI*2/3),                         (Math.PI*2/3) + endAngle/100 * (Math.PI*5/3),
                         false
                     );
                     ctx.stroke();
                 }                  刷新
                 function loading() {                     if (process >= percent) {
                         clearInterval(circleLoading);
                     }                      清除canvas内容                     ctx.clearRect(0, 0, circleX * 2, circleY * 2);                    中间的字                     ctx.font = fontSize + 'px April';
                     ctx.textAlign = 'center';
                     ctx.textBaseline = 'middle';
                     ctx.fillStyle = '#999';
                     ctx.fillText(parseFloat(process).toFixed(0) + '%', circleX, circleY);    
                  
                     圆形                     circle(circleX, circleY, radius);                     
                      圆弧                     sector(circleX, circleY, radius, Math.PI*2/3, process);
                     两端圆点                     smallcircle1(150+Math.cos(2*Math.PI/360*120)*100, 150+Math.sin(2*Math.PI/360*120)*100, 5);
                     smallcircle2(150+Math.cos(2*Math.PI/360*(120+process*3))*100, 150+Math.sin(2*Math.PI/360*(120+process*3))*100, 5);                     控制结束时动画的速度
                     if (process / percent > 0.90) {
                         process += 0.30;
                     } else if (process / percent > 0.80) {
                         process += 0.55;
                     } else if (process / percent > 0.70) {
                         process += 0.75;
                     } else {
                         process += 1.0;
                     }
                 }                 var process = 0.0;  进度
                 var circleLoading = window.setInterval(function () {
                     loading();
                 }, 20);
                     
            }
第二部分,调用封装好的代码:
            toCanvas('canvas',50);

感谢你能够认真阅读完这篇文章,希望小编分享canvas实现圆弧、圆环进度条的案例分析内容对大家有帮助,同时也希望大家多多支持创新互联建站,关注创新互联网站制作公司行业资讯频道,遇到问题就找创新互联建站,详细的解决方法等着你来学习!

文章标题:canvas实现圆弧、圆环进度条的案例分析-创新互联
文章位置:https://www.cdcxhl.com/article36/dphhsg.html

成都网站建设公司_创新互联,为您提供App设计外贸网站建设微信小程序品牌网站建设网站内链网站设计

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站建设网站维护公司