vue: 简单的canvas实现圆环进度条

效果如下:

代码如下:

<template>
    <div>
        <!-- 圆环背景 -->
        <canvas id="canvas" style="width: 100%;height: 100%">
        </canvas>
    </div>
       
</template>

<script>
export default {
    // 电量环形图
    name:'electricQuantity',
    props:{
        num:{
            type:Number,
            default:()=>0
        }
    },
    data(){
        return{
            defaultValue:0,
            timer:null
        }
    },
    created(){
        this.$nextTick(()=>{
            this.drawCanvas()
        })
    },
    watch:{
        num:function(){
            this.drawCanvas()
        }
    },
    methods:{
        drawCanvas(){
            var canvas=document.getElementById('canvas');
            canvas.width = 300
            canvas.height = 300
            var ctx=canvas.getContext("2d")
            this.timer = setInterval(()=>{
                this.drawMove(ctx)
            },5)
        },
        drawMove(ctx){
            ctx.clearRect(0, 0, 300, 300);
            const {defaultValue,num} = this
            // 外部圆环
            ctx.lineWidth = 2
            ctx.beginPath();
            ctx.arc(150,150,150,0,Math.PI * 2,false);
            var canvasGradient = ctx.createLinearGradient(150, 150, 40, 150);
            // ctx.strokeStyle='#fff';
            canvasGradient.addColorStop(0, "#289EFF");
            canvasGradient.addColorStop(0.12, "#00EBCE");
            //将fillStyle的属性值设为该CanvasGradient对象
            ctx.strokeStyle = canvasGradient;
            ctx.closePath()
            ctx.stroke();
            // 内部空槽
            ctx.lineWidth = 23
            ctx.beginPath();
            ctx.arc(150,150,120,0,Math.PI * 2,false);
            ctx.strokeStyle='rgb(29,39,53)';
            ctx.closePath()
            ctx.stroke();
            if(defaultValue>=num){
                this.defaultValue = num
                clearInterval(this.timer)
            }else{
                this.defaultValue = this.defaultValue + 1
            }
            ctx.lineWidth = 23
            ctx.beginPath();
            ctx.arc(150,150,120,Math.PI*1.5,this.defaultValue?Math.PI * ((this.defaultValue/100 -0.25)*2-0.0001):Math.PI*1.5,false);
            var canvasGradient = ctx.createLinearGradient(150, 0, 150, 300);
            canvasGradient.addColorStop(0, "#289EFF");
            canvasGradient.addColorStop(1, "#00EBCE");
            ctx.strokeStyle = canvasGradient;
            // ctx.closePath()
            ctx.stroke();
            // 文字部分
            if(this.defaultValue >= 100){
                ctx.font = "bold 50px arial";
            }else{
                ctx.font = "bold 70px arial";
            }
            ctx.fillStyle='#02D3FD';
            ctx.fillText(this.defaultValue,105,160);
            let numWidth = ctx.measureText(this.defaultValue).width
            ctx.font = "bold 20px arial";
            ctx.fillStyle='#02D3FD';
            ctx.fillText("%",110+numWidth,160);
            ctx.font = "23px arial";
            ctx.fillStyle='#02D3FD';
            ctx.fillText("电量剩余",105,190);
            // 起点小圆
            ctx.beginPath();
            ctx.arc(150, 10, 5, 0, Math.PI * 2, true);
            ctx.closePath();
            ctx.fillStyle = '#27A0FE';
            ctx.fill();
            // 终点小圆
            let x = 150 + Math.cos(this.defaultValue?Math.PI * ((this.defaultValue/100 -0.25)*2-0.0001):Math.PI*1.5) * 140
            let y = 150 + Math.sin(this.defaultValue?Math.PI * ((this.defaultValue/100 -0.25)*2-0.0001):Math.PI*1.5) * 140
            ctx.beginPath();
            ctx.arc(x, y, 5, 0, Math.PI * 2, true);
            ctx.closePath();
            ctx.fillStyle = '#27A0FE';
            ctx.fill();
        }

    }, 
}
</script>

感觉有很多不妥,欢迎大佬指正 

记录萌新第一步

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过计算属性和绑定样式实现圆环进度条根据参数动态变化。 首先,在 Vue3 中创建一个组件,将参数作为组件的 props 传入。然后,使用计算属性计算出进度条的样式,并将样式绑定到对应的 HTML 元素上。最后,使用 CSS 实现圆环的效果。 下面是一个示例代码: ```vue <template> <div class="progress-ring"> <div class="progress-bar" :style="progressStyle"></div> <div class="progress-label">{{ progress }}%</div> </div> </template> <script> export default { props: { progress: { type: Number, required: true, }, }, computed: { // 计算进度条的样式 progressStyle() { const progress = this.progress <= 100 ? this.progress : 100; const rotation = `rotate(${(progress / 100) * 360}deg)`; return { transform: rotation, }; }, }, }; </script> <style scoped> .progress-ring { position: relative; width: 100px; height: 100px; } .progress-bar { position: absolute; width: 100%; height: 100%; border-radius: 50%; clip: rect(0, 50px, 100px, 0); background-color: #f2f2f2; transform: rotate(0deg); z-index: -1; } .progress-bar::before { content: ""; position: absolute; width: 100%; height: 100%; border-radius: 50%; clip: rect(0, 50px, 100px, 0); background-color: #2196f3; transform-origin: center center; } .progress-label { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 24px; font-weight: bold; color: #2196f3; } </style> ``` 在上面的代码中,我们定义了一个名为 `progress-ring` 的容器,容器中包含一个圆环进度条和一个显示进度百分比的标签。通过计算属性 `progressStyle` 计算出进度条的样式,并将样式绑定到 `progress-bar` 元素上。最后,使用 CSS 实现圆环的效果。 你可以将这个组件作为子组件嵌入到父组件中,在父组件中传入进度参数即可实现根据参数动态变化的圆环进度条

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值