使用canvas实现h5圆环加载百分比进度条

  • 效果图
    在这里插入图片描述

  • js文件

/**
 * 百分比加载 
 * const dl = new DrawLoad('canvas');   
 * canvas为canvas元素id
 * dl.setPer(1) // 设置百分比为1
 * dl.clearAnimation() //清除动画刷新
 * 元素示例 <canvas id="canvas" width="150" height="150"></canvas>
 */
class DrawLoad {
    constructor(canvas) {
        this.per = 0;
        this.canvas = document.getElementById(canvas);  //获取canvas元素
        this.context = this.canvas.getContext('2d');  //获取画图环境,指明为2d
        this.centerX = this.canvas.width/2;   //Canvas中心点x轴坐标
        this.centerY = this.canvas.height/2;  //Canvas中心点y轴坐标
        this.rad = Math.PI*2/100; //将360度分成100份,那么每一份就是this.rad度
        this.drawFrame();
    }
    /**
     * 设置百分比
     * @param 整数 0-100
     */
    setPer(per) {
        this.per = per;
    }
    // 清除动画刷新
    clearAnimation() {
        window.cancelAnimationFrame(this.raf_id);
    }

    //绘制当前百分比圈
    currentCircle(){
        this.context.save();
        this.context.strokeStyle = "#6596f2"; //设置描边样式
        this.context.lineWidth = 8; //设置线宽
        this.context.beginPath(); //路径开始
        this.context.arc(this.centerX, this.centerY, 50 , -Math.PI/2, -Math.PI/2 +this.per*this.rad, false); //用于绘制圆弧this.context.arc(x坐标,y坐标,半径,起始角度,终止角度,顺时针/逆时针)
        this.context.stroke(); //绘制
        this.context.closePath(); //路径结束
        this.context.restore();
    }
    //绘制轨迹圈
    bgCircle(){
        this.context.save();
        this.context.beginPath();
        this.context.lineWidth = 8; //设置线宽
        this.context.strokeStyle = "#ddd";
        this.context.arc(this.centerX, this.centerY, 50 , 0, Math.PI*2, false);
        this.context.stroke();
        this.context.closePath();
        this.context.restore();
    }  
    //百分比文字绘制
    text() {
        this.context.save(); //save和restore可以保证样式属性只运用于该段canvas元素
        this.context.fillStyle = "#6596f2"; //设置描边样式
        this.context.font = "20px Arial"; //设置字体大小和字体
        //绘制字体,并且指定位置
        this.context.fillText(this.per.toFixed(0)+"%", this.centerX-16, this.centerY+6);
        // this.context.stroke(); //执行绘制
        this.context.restore();
    } 
    //动画循环
    drawFrame(){
        this.raf_id = window.requestAnimationFrame(this.drawFrame.bind(this));
        this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
        this.bgCircle();
        this.text();
        this.currentCircle();
    };
}
  • html文件
<!DOCTYPE html>
<html>

<head>
    <title>test load</title>
    <meta charset="utf-8">
    <script src="load.js"></script>
</head>

<body>
    <canvas id="canvas" width="150" height="150"></canvas>
</body>
<script>
window.onload = () => {
    const dl = new DrawLoad('canvas');
    let i = 0;
    let iv = Number;
    iv = setInterval(() => {
        dl.setPer(++i);
        if (i > 100) {
            clearInterval(iv); // 清除
            dl.clearAnimation(); //清除动画刷新
        }
    }, 20);
}
</script>

</html>

end.

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值