当前位置: 首页 > 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)"/><!-- 中心文本 --><!-- <textclass="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 emit = defineEmits(['progress-change']);const progress = ref(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>

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.dtcms.com/a/284539.html

相关文章:

  • 石子入水波纹效果:顶点扰动着色器实现
  • 【44】MFC入门到精通——MFC 通过Button按钮添加控件变量实现:按下 按钮变色 (比如开关 打开关闭状态) MFC更改button控颜色
  • Git简介与特点:从Linux到分布式版本控制的革命
  • 找不到或无法加载主类 org.gradle.wrapper.GradleWrapperMain
  • Linux Swap区深度解析:为何禁用?何时需要?
  • 【Java EE初阶 --- 网络原理】网络编程
  • Vue3 + WebSocket
  • 基于现代R语言【Tidyverse、Tidymodel】的机器学习方法
  • 3.2 函数参数与返回值
  • .vscode 扩展配置
  • 浅析网络安全面临的主要威胁类型及对应防护措施
  • 【C++指南】C++ list容器完全解读(四):反向迭代器的巧妙实现
  • 如何做好DNA-SIP?
  • 【41】MFC入门到精通——MFC中 GetLBText()、GetWindowText()、SetWindowText区别
  • 扭蛋机小程序开发:开启线上娱乐新风尚
  • 分布式光伏发电系统中的“四可”指的是什么?
  • 教资科三【信息技术】— 学科知识: 第一章(信息技术基础)
  • 基于springboot+vue+mysql技术的实验室管理系统(源码+论文)
  • 044_设计模式入门(创建型 / 结构型 / 行为型)
  • 【解决方案】鸿蒙 / 矿鸿系统 Shell 无故退出问题(息屏导致)详解
  • Spatial Frequency Modulation for Semantic Segmentation。针对图像下采样造成信息丢失问题的解决思路
  • 深入理解 Spring Boot Starter 的生成机制
  • tcp/udp调试工具
  • Linux内核网络栈深度剖析:inet_connection_sock.c的服务器端套接字管理
  • LINUX例行性工作(计划任务)实验操作 ---at和crontab以及系统级别的计划任务
  • springboot跨域问题 和 401
  • 当下主流摄像头及其核心参数详解
  • 不同场景下git指令的搭配
  • pycharm回车、删除、方向键和快捷键等不能使用原因
  • TRAE Agent 在 SWE-bench Verified 上得分 75.2%,并已开源