赞
踩
效果图
实现原理:
1.使用canvas绘制两个半圆弧,底图灰色半圆弧和颜色进度圆弧。
2.利用setInterval计时器,逐步改变颜色进度条,达到进度条的效果。
- <!DOCTYPE html>
- <html>
-
- <head>
- <meta charset="UTF-8">
- <title></title>
- </head>
- <script type="text/javascript" src="http://cdn.bootcss.com/jquery/2.2.3/jquery.min.js"></script>
- <body>
- <canvas id="canvas" width="300" height="300">
- </canvas>
-
- <script type="text/javascript">
- function toCanvas(id, progress, progress2) {
- //canvas进度条
- var canvas = document.getElementById(id),
- ctx = canvas.getContext("2d"),
- percent = progress,
- percent2 = progress2,
- //最终百分比
- circleX = canvas.width / 2, // 中心x坐标
- circleY = canvas.height / 2, //中心y坐标
- radius = 100, //圆环半径
- lineWidth = 8, // 圆形线条的宽度
- fontSize = 10; //字体大小
-
- /*//两端圆点
- function smallcircle1(cx, cy, r) {
- ctx.beginPath();
- ctx.lineWidth = 1;
- ctx.fillStyle = '#06a8f3';
- ctx.arc(cx, cy, r, Math.PI, Math.PI * 2);
- ctx.fill();
- }
-
- function smallcircle2(cx, cy, r) {
- ctx.beginPath();
- ctx.lineWidth = 1;
- ctx.fillStyle = '#00f8bb';
- ctx.arc(cx, cy, r, Math.PI, Math.PI * 2);
- ctx.fill();
- }*/
-
- //画圆
- function circle(cx, cy, r) {
- ctx.beginPath();
- ctx.lineWidth = lineWidth;
- ctx.strokeStyle = '#eee';
- ctx.arc(cx, cy, r, Math.PI, Math.PI * 2);
- //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.lineWidth = lineWidth;
-
- // 渐变色 - 可自定义
- var linGrad = ctx.createLinearGradient(
- circleX - radius - lineWidth, circleY, circleX + radius + lineWidth, circleY
- );
- linGrad.addColorStop(0.0, '#06a8f3');
- linGrad.addColorStop(1.0, '#00f8bb');
- ctx.strokeStyle = linGrad;
- //圆弧两端的样式
- ctx.lineCap = 'round';
- ctx.arc(cx, cy, r, Math.PI, Math.PI * (1 + endAngle / 100));
- ctx.stroke();
- }
-
- //画弧线02
- function sector2(cx, cy, r, startAngle, endAngle, anti) {
- ctx.beginPath();
- ctx.lineWidth = lineWidth;
-
- // 渐变色 - 可自定义
- var linGrad = ctx.createLinearGradient(
- circleX - radius - lineWidth, circleY, circleX + radius + lineWidth, circleY
- );
- linGrad.addColorStop(0.0, '#06a8f3');
- linGrad.addColorStop(1.0, '#00f8bb');
- ctx.strokeStyle = linGrad
-
- //圆弧两端的样式
- ctx.lineCap = 'round';
- ctx.arc(cx, cy, r * 3 / 4, Math.PI, Math.PI * (1 + endAngle / 100));
- ctx.stroke();
- }
- //刷新
- function loading() {
- var percent3 = progress;
- if(percent < percent2) percent = percent2;
- if(process >= percent) clearInterval(circleLoading);
- if(process2 >= 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*4/5);
- ctx.fillText("月度完成比",circleX, circleY);
-
- //圆形
- circle(circleX, circleY, radius);
-
- //圆弧
- sector(circleX, circleY, radius, Math.PI * 2 / 3, process);
- sector2(circleX, circleY, radius, Math.PI * 2 / 3, process2);
- //两端圆点
- //smallcircle1(50 + Math.cos(2 * Math.PI / 360 * 120) * 100, 150 + Math.sin(2 * Math.PI / 360 * 120) * 100, 5);
- //smallcircle2(50 + Math.cos(2 * Math.PI / 360 * (120 + process * 3)) * 100, 150 + Math.sin(2 * Math.PI / 360 * (120 + process * 3)) * 100, 5);
- //控制结束时动画的速度
- if(process < percent3) {
- 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;
- }
- }
-
- if(process2 < percent2) {
- if(process2 / percent > 0.90) {
- process2 += 0.30;
- } else if(process2 / percent > 0.80) {
- process2 += 0.55;
- } else if(process2 / percent > 0.70) {
- process2 += 0.75;
- } else {
- process2 += 1.0;
- }
- }
-
- }
-
- var process = 0.0;
- var process2 = 0.0;
-
- var circleLoading = window.setInterval(function() {
- loading();
- }, 20);
-
- }
- toCanvas('canvas', 50, 80);
- </script>
- </body>
-
- </html>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。