canvas圆环百分比进度

原文地址:http://www.qdfuns.com/notes/14070/0f51b3417c8afd96c10f0c141ae26544.html






canvas圆环进度


*{padding: 0; margin: 0; }
.circle{width: 200px;height: 200px;margin: 20em auto;position: relative;}
canvas{display: block;margin: 0;position: absolute;background: white;left: 0;top: 0;}
#canvas_1{z-index: 1 }
#canvas_2{z-index: 2; background: transparent;transform:rotate(-90deg); }








function inte(percent) {
var canvas_1 = document.querySelector('#canvas_1');
var canvas_2 = document.querySelector('#canvas_2');
var ctx_1 = canvas_1.getContext('2d');
var ctx_2 = canvas_2.getContext('2d');
ctx_1.lineWidth = 10;
ctx_1.strokeStyle = "#ccc";
//画底部的灰色圆环
ctx_1.beginPath();
ctx_1.arc(canvas_1.width / 2, canvas_1.height / 2, canvas_1.width / 2 - ctx_1.lineWidth / 2, 0, Math.PI * 2, false);
ctx_1.closePath();
ctx_1.stroke();
if (percent < 0 || percent > 100) {
throw new Error('percent must be between 0 and 100');
return
}
ctx_2.lineWidth = 10;
ctx_2.strokeStyle = "#f90";
var angle = 0;
var timer;
(function draw() {
timer = requestAnimationFrame(draw);
ctx_2.clearRect(0, 0, canvas_2.width, canvas_2.height)
//百分比圆环
ctx_2.beginPath();
ctx_2.arc(canvas_2.width / 2, canvas_2.height / 2, canvas_2.width / 2 - ctx_2.lineWidth / 2, 0, angle * Math.PI / 180, false);
angle++;
var percentAge = parseInt((angle / 360) * 100)
if (angle > (percent / 100 * 360)) {
percentAge = percent
window.cancelAnimationFrame(timer);
};
ctx_2.stroke();
ctx_2.closePath();
ctx_2.save();
ctx_2.beginPath();
ctx_2.rotate(90 * Math.PI / 180)
ctx_2.font = '30px Arial';
ctx_2.fillStyle = 'red';
var text = percentAge + '%';
ctx_2.fillText(text, 80, -90);
ctx_2.closePath();
ctx_2.restore();
})()
}
window.onload = inte(60);


重点内容

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值