您好,欢迎访问一九零五行业门户网

详解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);
【相关推荐】
1. canvas实现圆形进度条并显示数字百分比
2. 利用css clip 实现音频播放圆环进度条教程实例
3. 分享h5 canvas圆圈进度条的实例代码
4. h5 canvas实现圆形动态加载进度实例
以上就是详解canvas实现圆弧、圆环进度条的实例方法的详细内容。
其它类似信息

推荐信息