canvas圆环进度条

简述

日常工作中,有可能遇到圆环进度条的需求,此处给出圆环进度条的DEMO。

DEMO

DEMO

代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>draw-progress</title>
</head>
<body>
<canvas id="canvas" width="180" height="180"></canvas>
<script>
    const canvas = document.querySelector('#canvas')
    drawProgress(canvas, 60, '#2196f3', '#eef7e4', 'This is tips info')

    function drawProgress (canvasEle, percent, foregroundColor, backgroundColor, tips) {
        const context = canvasEle.getContext('2d')
        const circleLineWidth = 8 * getPixelRatio(context)
        // 高清屏绘制时是多倍像素,保持逻辑像素不变,增加物理像素,保证其在高清屏清晰显示
        canvasEle.style.width = canvasEle.width + 'px'
        canvasEle.style.height = canvasEle.height + 'px'
        canvasEle.width = canvasEle.width * getPixelRatio(context)
        canvasEle.height = canvasEle.height * getPixelRatio(context)
        const centerX = canvasEle.width / 2
        const centerY = canvasEle.height / 2
        const rad = Math.PI * 2 / 100
        let curProgress = 0

        function getPixelRatio (context) {
            const backingStore = context.backingStorePixelRatio ||
                context.webkitBackingStorePixelRatio ||
                context.mozBackingStorePixelRatio ||
                context.msBackingStorePixelRatio ||
                context.oBackingStorePixelRatio ||
                context.backingStorePixelRatio || 1
            return (window.devicePixelRatio || 1) / backingStore
        }

        function backgroundCircle () {
            context.save()
            context.beginPath()
            context.lineWidth = circleLineWidth
            // 线条只有 1/2 在圆内,因此除以2
            const radius = centerX - circleLineWidth / 2
            context.lineCap = 'round'
            context.strokeStyle = backgroundColor
            context.arc(centerX, centerY, radius, 0, Math.PI * 2, false)
            context.stroke()
            context.closePath()
            context.restore()
        }

        function foregroundCircle (progress) {
            context.save()
            context.strokeStyle = foregroundColor
            context.lineWidth = circleLineWidth
            context.lineCap = 'round'
            const radius = centerX - circleLineWidth / 2
            context.beginPath()
            context.arc(centerX, centerY, radius, -Math.PI / 2, -Math.PI / 2 + progress * rad, false)
            context.stroke()
            context.closePath()
            context.restore()
        }

        function drawText () {
            context.save()
            context.fillStyle = '#000'
            const fontSize = 18 * getPixelRatio(context)
            context.font = fontSize + 'px sans-serif'
            context.textBaseline = 'bottom'
            const textWidth = context.measureText(tips).width
            context.fillText(tips, centerX - textWidth / 2, centerY + fontSize / 2)
            context.restore()
        }

        (function drawFrame () {
            window.requestAnimationFrame(drawFrame)
            context.clearRect(0, 0, canvasEle.width, canvasEle.height)
            backgroundCircle()
            foregroundCircle(curProgress)
            drawText()
            if (curProgress >= percent) return
            curProgress += 10
        }())
    }
</script>
</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿祥_csdn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值