vue3半圆进度条,尾部有个环形状结束块

<template>

    <div class="circle-progress">

        <canvas id="canvas" width="150" height="150"></canvas>

        <div class="btns">

            <button @click="toCanvas('canvas', '#ffbf00', 0)">0</button>

            <button @click="toCanvas('canvas', '#ffbf00', 10)">10</button>

            <button @click="toCanvas('canvas', '#ffbf00', 20)">20</button>

            <button @click="toCanvas('canvas', '#ffbf00', 30)">30</button>

            <button @click="toCanvas('canvas', '#ffbf00', 40)">40</button>

            <button @click="toCanvas('canvas', '#ffbf00', 50)">50</button>

        </div>

        <div class="btns">

            <button @click="toCanvas('canvas', '#ffbf00', 60)">60</button>

            <button @click="toCanvas('canvas', '#ffbf00', 70)">70</button>

            <button @click="toCanvas('canvas', '#ffbf00', 80)">80</button>

            <button @click="toCanvas('canvas', '#ffbf00', 90)">90</button>

            <button @click="toCanvas('canvas', '#ffbf00', 100)">100</button>

        </div>

    </div>

</template>

<script setup>

import { onMounted } from 'vue'

const obj = reactive({

    canvas: '',

    percent: '',

    ctx: '',

    circleX: '',

    circleY: '',

    radius: '',

    cradius: '',

    lineWidth: '',

    fontSize: '',

    color: '',

    process: '',

    circleLoading: null,

})

const circle = (cx, cy, r) => {

    obj.ctx.beginPath();

    //ctx.moveTo(cx + r, cy);

    obj.ctx.lineWidth = obj.lineWidth;

    obj.ctx.strokeStyle = "#000";

    obj.ctx.arc(cx, cy, r, 0, Math.PI, true);

    obj.ctx.stroke();

}

const sector = (cx, cy, r, startAngle, endAngle, anti) => {

    obj.ctx.beginPath();

    //ctx.moveTo(cx, cy + r); // 从圆形底部开始画

    obj.ctx.lineWidth = obj.lineWidth;

    // 进度条颜色

    obj.ctx.strokeStyle = obj.color;

    //圆弧两端的样式

    obj.ctx.lineCap = "round";

    //圆弧

    obj.ctx.arc(cx, cy, r, Math.PI, Math.PI + (endAngle / 100) * Math.PI, false);

    obj.ctx.stroke();

}

const smallcircle2 = (cx, cy, r, fillStyle) => {

    obj.ctx.beginPath();

    //ctx.moveTo(cx + r, cy);

    obj.ctx.lineWidth = 1;

    obj.ctx.fillStyle = fillStyle;

    obj.ctx.arc(cx, cy, r, 0, Math.PI * 2);

    obj.ctx.fill();

}

const loading = (progress) => {

    if (obj.process >= obj.percent) {

        clearInterval(obj.circleLoading);

    }

    //清除canvas内容

    obj.ctx.clearRect(0, 0, obj.circleX * 2, obj.circleY * 2);

    //中间的字

    obj.ctx.font = "normal bold 40px April";

    obj.ctx.textAlign = "center";

    obj.ctx.textBaseline = "middle";

    obj.ctx.fillStyle = "#ffbf00";

    obj.ctx.fillText(parseFloat(obj.process).toFixed(0), obj.circleX, obj.circleY - 5);

    obj.ctx.font = "normal bold " + obj.fontSize + "px April";

    obj.ctx.fillStyle = "#ffbf00";

    //圆形

    circle(obj.circleX, obj.circleY, obj.radius);

    //圆弧

    sector(obj.circleX, obj.circleY, obj.radius, (Math.PI * 2) / 3, obj.process);

    smallcircle2(

        obj.cradius +

        Math.cos(Math.PI + (obj.process / 100) * Math.PI) * obj.radius,

        obj.cradius +

        Math.sin(Math.PI + (obj.process / 100) * Math.PI) * obj.radius,

        8,

        "green"

    );

    smallcircle2(

        obj.cradius +

        Math.cos(Math.PI + (obj.process / 100) * Math.PI) * obj.radius,

        obj.cradius +

        Math.sin(Math.PI + (obj.process / 100) * Math.PI) * obj.radius,

        5,

        "red"

    );

    //控制结束时动画的速度

    if (obj.process / obj.percent > 0.9) {

        obj.process += 0.3;

    } else if (obj.process / obj.percent > 0.8) {

        obj.process += 0.55;

    } else if (obj.process / obj.percent > 0.7) {

        obj.process += 0.75;

    } else {

        obj.process += 1.0;

    }

}

const toCanvas = (id, color, progress) => {

    clearInterval(obj.circleLoading);

    //canvas进度条

    obj.canvas = document.getElementById(id);

    obj.percent = progress //最终百分比

    obj.ctx = canvas.getContext("2d");

    obj.circleX = canvas.width / 2; //中心x坐标

    obj.circleY = canvas.height / 2; //中心y坐标

    obj.radius = 60; //圆环半径

    obj.cradius = 75; // canvas半径

    obj.lineWidth = 6; //圆形线条的宽度

    obj.fontSize = 16; //字体大小

    obj.process = 0.0; //进度

    obj.color = color;

    if (progress < 50) {

        obj.circleLoading = window.setInterval(() => {

            loading(progress);

        }, 20);

    }

    if (progress >= 50 && progress < 70) {

        obj.circleLoading = window.setInterval(() => {

            loading(progress);

        }, 10);

    }

    if (progress >= 70) {

        obj.circleLoading = window.setInterval(() => {

            loading(progress);

        }, 6);

    }

}

onMounted(() => {

    toCanvas("canvas", "#ffbf00", 40);

})

</script>

<style lang="less">

.circle-progress {

    h2 {

        color: #ffbf00;

        margin: 160px 0 60px 0;

    }

    .btns {

        margin: 20px 0;

        .van-button--default {

            background-color: #ffbf00;

            color: #fff;

        }

    }

}

</style>

Vue中实现半圆进度条可以通过以下步骤来完成: 1. 首先,在Vue组件中创建一个变量来存储进度(0-100之间的数字),例如:`progress: 50`。 2. 在模板中使用SVG元素创建一个半圆形,可以使用`<path>`元素来绘制半圆形路径。 3. 使用`stroke-dasharray`和`stroke-dashoffset`属性来控制路径的显示范围和偏移量。例如,将`stroke-dasharray`设置为半圆的周长,并根据进度计算`stroke-dashoffset`的值。 4. 使用样式来调整路径的颜色,线宽等。 下面是一个简单的示例代码: ```vue <template> <div class="progress-bar"> <svg class="circle"> <path :d="circlePath" :stroke-dasharray="circleLength" :stroke-dashoffset="circleOffset" fill="transparent" stroke="#00ff00" stroke-width="10" /> </svg> </div> </template> <script> export default { data() { return { progress: 50, radius: 50, }; }, computed: { circlePath() { const x = this.radius; const y = this.radius; const startAngle = -Math.PI / 2; // 半圆的起始角度 const endAngle = (this.progress / 100) * Math.PI + startAngle; // 根据进度计算终止角度 const largeArcFlag = endAngle - startAngle <= Math.PI ? 0 : 1; // 是否大于半圆 const sweepFlag = 1; // 绘制方向,时针为1,逆时针为0 // 生成SVG路径字符串 return `M ${x} ${y} L ${x} 0 A ${this.radius} ${this.radius} 0 ${largeArcFlag} ${sweepFlag} ${x + this.radius * Math.cos(endAngle)} ${y + this.radius * Math.sin(endAngle)} Z`; }, circleLength() { return Math.PI * this.radius; }, circleOffset() { const circumference = this.circleLength; const progressOffset = (100 - this.progress) * circumference / 100; return progressOffset; }, }, }; </script> <style scoped> .progress-bar { width: 100px; /* 进度条容器的宽度 */ height: 100px; /* 进度条容器的高度 */ } .circle { transform: rotate(-90deg); /* 将半圆旋转到水平位置 */ } </style> ``` 这个示例代码会根据`progress`变量的值来显示相应的进度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值