当前位置: 首页 > news >正文

vue svg实现一个环形进度条组件

svg实现一个环形进度条

设计初衷:本来想直接使用element的进度条组件的,但是好多属性都没有办法控制。
UI设计的图如下,需要控制未完成和已完成的颜色,端点的形状改为普通的butt
所以使用svg实现了一个环形进度条组件

element组件
在这里插入图片描述

设计图实现效果

在这里插入图片描述
可以交互的svg 环形进度条组件
在这里插入图片描述

<template><div class="circular-progress-container"><svg :width="size" :height="size" viewBox="0 0 100 100" class="progress-svg"><!-- 背景圆(未完成部分) --><circleclass="progress-background"cx="50"cy="50":r="radius"fill="none":stroke="backgroundColor":stroke-width="strokeWidth" /><!-- 前景圆(已完成部分) --><circleclass="progress-foreground"cx="50"cy="50":r="radius"fill="none":stroke="progressColor":stroke-width="strokeWidth":stroke-dasharray="circumference":stroke-dashoffset="dashOffset"stroke-linecap="butt"transform="rotate(-90 50 50)" /><!-- 中心文本 --><!-- <text class="progress-text" x="50" y="50" text-anchor="middle" dominant-baseline="middle">{{ Math.round(progress) }}%</text> --></svg><!-- 控制滑块 --><!-- <div class="progress-controls"><inputtype="range"min="0"max="100"step="1"v-model.number="progress"class="progress-slider"@input="onProgressChange" /></div> --></div>
</template><script setup>
import { ref, computed } from 'vue';const props = defineProps({size: {type: Number,default: 48,},strokeWidth: {type: Number,default: 12,},backgroundColor: {type: String,default: '#D5DDEB', // 灰色背景},progressColor: {type: String,default: '#367AFB', // 蓝色进度},// initialProgress: {//     type: Number,//     default: 0,//     validator: (value) => value >= 0 && value <= 100,// },
});
const progress = defineModel({ required: true, validator: (value) => value >= 0 && value <= 100 });// const emit = defineEmits(['progress-change']);
// const progress = ref(props.initialProgress);
// watch(
//     () => props.initialProgress,
//     () => {
//         progress.value = props.initialProgress;
//     }
// );const radius = computed(() => 50 - props.strokeWidth / 2);
const circumference = computed(() => 2 * Math.PI * radius.value);
const dashOffset = computed(() => circumference.value * (1 - progress.value / 100));// const onProgressChange = () => {
//     emit('progress-change', progress.value);
// };
</script><style scoped>
.circular-progress-container {display: flex;flex-direction: column;align-items: center;gap: 20px;width: fit-content;
}.progress-svg {display: block;
}.progress-background {transition: stroke 0.3s ease;
}.progress-foreground {transition: stroke-dashoffset 0.5s ease, stroke 0.3s ease;
}.progress-text {font-size: 20px;font-weight: bold;fill: #333;user-select: none;
}.progress-controls {width: 80%;max-width: 300px;
}.progress-slider {width: 100%;cursor: pointer;
}.progress-slider::-webkit-slider-thumb {-webkit-appearance: none;width: 18px;height: 18px;border-radius: 50%;background: v-bind('props.progressColor');cursor: pointer;
}.progress-slider::-moz-range-thumb {width: 18px;height: 18px;border-radius: 50%;background: v-bind('props.progressColor');cursor: pointer;
}
</style>

用法

 <Circle v-model="dataState.finishRate"></Circle>

radius 计算半径

const radius = computed(() => 50 - props.strokeWidth / 2);

因为设置了viewBox="0 0 100 100",所以是绘制在一个100*100的画布上

stroke-dashoffset属性解释

stroke-dashoffset 控制虚线模式的起始位置偏移量。结合 stroke-dasharray,我们可以实现进度条效果:

  • stroke-dasharray:定义虚线的模式(实线长度, 间隔长度)
  • stroke-dashoffset:定义虚线模式的起始偏移量

对于环形进度条:

  1. 设置 stroke-dasharray 为圆的周长(表示100%进度)
  2. 通过 stroke-dashoffset 控制显示比例
const radius = 50 - strokeWidth / 2; // 半径
const circumference = 2 * Math.PI * radius; // 圆周长// 计算偏移量(0%进度时偏移量=周长,100%进度时偏移量=0)
const dashOffset = circumference * (1 - progress / 100);
  • 初始状态下,我们希望进度为0,也就是没有显示进度,那么我们应该将整个长划偏移出去,即偏移量等于周长(dashoffset = circumference)。
  • 随着进度增加,我们希望显示的部分越来越多,那么偏移量应该逐渐减少。当进度为100%时,偏移量为0(即没有偏移,整个圆环显示出来)。
    因此,偏移量的计算公式为:
    dashoffset = circumference * (1 - progress / 100)

stroke-linecap 控制线段端点的形状

  • butt (默认值)

    线段以方形结束,不超出端点

    进度条看起来像被"切断"的效果

    适合需要精确对齐的场景

  • round

    线段末端添加半圆形帽,半径等于线宽的一半

    使进度条两端呈现圆润效果

    适合大多数进度条场景,视觉效果更柔和

  • square

    线段末端添加方形帽,延伸长度等于线宽的一半

    类似于 butt 但会稍微超出端点

    适合需要整齐但略微延伸的效果

使用svg实现的优点

轻量、精确控制、动画支持、矢量图形、交互性

http://www.lryc.cn/news/591834.html

相关文章:

  • 进程终止机制详解:退出场景、退出码与退出方式全解析
  • STM32 IAR 生成工程后配置
  • 时序数据库选型指南︰为什么IoTDB成为物联网场景首选?
  • UML用例规范,use case diagram
  • halcon 检测直线
  • OpenCV学习笔记二(色彩空间:RGB、HSV、Lab、mask)
  • DocsGPT:您的智能知识助手,解锁高效信息检索
  • 前端之HTML学习
  • 项目实战(18)-POE分离器
  • 渗透总结一
  • 手机兼容测试服务提供商对比分析:如何选择最合适的测试平台
  • 学习软件测试掌握什么基本知识?
  • 在VsCode上使用开发容器devcontainer
  • windows内核研究(驱动开发 第一个驱动程序和调试环境搭建)
  • VSCODE常规设置
  • 删除百度同步空间桌面图标
  • Elasticsearch+Logstash+Filebeat+Kibana部署【7.1.1版本】
  • 全桥LLC 分立电感变压器计算
  • Docker实战:使用Docker部署IT工具箱Team·IDE
  • vuex的理解以及应用
  • Spring中事务失效的情况深度分析
  • 深入理解 SemaphoreSlim 在.NET Core API 开发中的应用
  • ROS1/Linux——Launch文件使用
  • 三、CV_VGGnet
  • 从零开始学 Linux 系统安全:基础防护与实战应用
  • git merge 和 git rebase 的区别
  • Python获取网页乱码问题终极解决方案 | Python爬虫编码处理指南
  • C++中,不能声明为虚函数的函数类型
  • Redis红锁中的看门狗机制
  • FreeRTOS—中断管理