Canvas绘制圆形进度条

Canvas绘制圆形进度条

canvas画布坐标点,如下图:

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Canvas绘制圆形进度条</title>
</head>
<body>
<h1>绘制圆形进度条</h1>
<!--画布-->
<canvas style="background: #efefef;" id="canvas" width="500" height="500"></canvas>
<script>
    var ctx = canvas.getContext('2d');//生成画笔对象
    ctx.lineWidth = 30;//线条的宽度
    ctx.font = '60px SimHei';//字体样式
    var start = -90, end = -90;//起止点
    var timer = setInterval(function () {
        ctx.clearRect(0, 0, 500, 500);//清除画布已有内容
        end += 10;
        //绘制圆弧
        ctx.beginPath();//开始绘制路径
        ctx.arc(250, 250, 100, start * Math.PI / 180, end * Math.PI / 180);//绘制一个弧线
        if (end - start < 120) {    //1/3范围内 红色
            ctx.strokeStyle = 'red';//轮廓/描边的颜色
            ctx.fillStyle = 'red';//填充颜色
        } else if (end - start < 240) {
            ctx.strokeStyle = 'orange';
            ctx.fillStyle = 'orange';
        } else {
            ctx.strokeStyle = 'green';
            ctx.fillStyle = 'green';
        }
        ctx.stroke();//对一条路径描边
        var percentage = Math.floor((end - start) / 360 * 100) + '%';//绘制文字
        var txtWidth = ctx.measureText(percentage).width; //获得文字的宽度
        ctx.fillText(percentage, 250 - txtWidth / 2, 250 + 30);  //绘制文本
        if (end >= 270) {
            clearInterval(timer);
        }
    }, 200)
</script>
</body>
</html>

运行效果:

 

转载于:https://www.cnblogs.com/doing123/p/7202689.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值