gpt4 book ai didi

javascript - 如何在 Vue 组件内创建圆形进度条

转载 作者:太空狗 更新时间:2023-10-29 12:22:25 26 4
gpt4 key购买 nike

我正在使用 Vue 构建的应用程序中创建一个组件。此组件是一个倒计时,范围从 X 分钟到 00:00

我知道可以对 svg 进行动画处理以获得所需的结果,但我没有必要的知识。我从未使用过任何 svg 库。

我需要在我的进度组件中创建以下动画:

Progress bar animation sprites

动画需要根据天气平滑地跟随路径。路径节点应根据时间插入/更新。

这是我实际的倒计时组件:

var app = new Vue({
el: '#app',
data: {
date: moment(2 * 60 * 1000)
},
computed: {
time: function(){
return this.date.format('mm:ss');
}
},
mounted: function(){
var timer = setInterval(() => {
this.date = moment(this.date.subtract(1, 'seconds'));

if(this.date.diff(moment(0)) === 0){
clearInterval(timer);

alert('Done!');
}
}, 1000);
}
});
<script src="https://momentjs.com/downloads/moment.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script>

<div id="app">{{ time }}</div>

这是进度圈的 svg:

<svg x="0px" y="0px" viewBox="0 0 90 90">
<style type="text/css">
.st0{fill:#FFFFFF;}
.st1{fill:none;stroke:#B5B5B5;stroke-miterlimit:10;}
.st2{fill:none;stroke:#408EFF;stroke-linecap:round;stroke-miterlimit:10;}
.st3{fill:#408EFF;}
</style>
<rect class="st0" width="90" height="90"/>
<circle class="st1" cx="45" cy="45" r="40"/>
<path class="st2" d="M45,5c22.1,0,40,17.9,40,40S67.1,85,45,85S5,67.1,5,45S22.9,5,45,5"/>
<circle class="st3" cx="45" cy="5" r="3"/>
</svg>

我怎样才能达到预期的效果?

欢迎所有帮助。

最佳答案

您需要熟悉 SVG 形状,尤其是 <path> 为了制作圆弧。

这是一个例子:

Vue.component('progress-ring', {
template: '#progress-ring',
props: {
value: {
type: Number,
default: 0,
},
min: {
type: Number,
default: 0,
},
max: {
type: Number,
default: 1,
},
text: {
type: null,
default: '',
},
},
computed: {
theta() {
const frac = (this.value - this.min) / (this.max - this.min) || 0;
return frac * 2 * Math.PI;
},
path() {
const large = this.theta > Math.PI;
return `M0,-46 A46,46,0,${large ? 1 : 0},1,${this.endX},${this.endY}`;
},
endX() {
return Math.cos(this.theta - Math.PI * 0.5) * 46;
},
endY() {
return Math.sin(this.theta - Math.PI * 0.5) * 46;
},
},
});

new Vue({
el: '#app',
});
body {
font-family: sans-serif;
}

.progress-ring {
width: 100px;
height: 100px;
}

.progress-ring-circle {
stroke: rgba(0, 0, 0, 0.1);
stroke-width: 1;
fill: none;
}

.progress-ring-ring {
stroke: #007fff;
stroke-width: 2;
fill: none;
}

.progress-ring-end {
fill: #007fff;
}
<script src="https://rawgit.com/vuejs/vue/dev/dist/vue.js"></script>

<div id="app">
<progress-ring :min="0" :max="100" :value="40" text="12:34"></progress-ring>
</div>

<template id="progress-ring">
<svg class="progress-ring" viewBox="-50,-50,100,100">
<circle class="progress-ring-circle" r="46"/>
<path class="progress-ring-ring" :d="path"/>
<circle class="progress-ring-end" :cx="endX" :cy="endY" r="4"/>
<text alignment-baseline="middle" text-anchor="middle">{{ text }}</text>
</svg>
</template>

至于动画,你只需要使用 JavaScript 来改变 value Prop 通过使用,例如, setInterval 或其他一些方式。

关于javascript - 如何在 Vue 组件内创建圆形进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52067363/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com