zoukankan      html  css  js  c++  java
  • 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);
  • 相关阅读:
    Linux编程 22 shell编程(输出和输入重定向,管道,数学运算命令,退出脚本状态码)
    mysql 开发进阶篇系列 46 物理备份与恢复( xtrabackup的 选项说明,增加备份用户,完全备份案例)
    mysql 开发进阶篇系列 45 物理备份与恢复(xtrabackup 安装,用户权限,配置)
    mysql 开发进阶篇系列 44 物理备份与恢复( 热备份xtrabackup 工具介绍)
    Linux编程 21 shell编程(环境变量,用户变量,命令替换)
    Linux编程 20 shell编程(shell脚本创建,echo显示信息)
    mysql 开发进阶篇系列 43 逻辑备份与恢复(mysqldump 的基于时间和位置的不完全恢复)
    Linux编程 19 编辑器(vim 用法)
    (网页)angularjs中的interval定时执行功能(转)
    (网页)在SQL Server中为什么不建议使用Not In子查询(转)
  • 原文地址:https://www.cnblogs.com/xiaomingge/p/6382493.html
Copyright © 2011-2022 走看看