uni-app 圆形带刻度的进度条插件需求
uni-app 圆形带刻度的进度条插件需求
No information to display.
4 回复
QQ 583069500
承接双端(Android,iOS)原生插件开发,uni-app外包开发。欢迎咨询
QQ:1559653449
V X:fan-rising
qq417045639
针对你提出的 uni-app
圆形带刻度的进度条插件需求,可以通过自定义组件的方式来实现。以下是一个简单的示例代码,展示如何创建一个圆形带刻度的进度条组件。
1. 创建组件文件 CircleProgress.vue
<template>
<view class="container">
<canvas canvas-id="circleCanvas" class="circleCanvas"></canvas>
<view class="ticks">
<view v-for="(tick, index) in ticks" :key="index" :style="{ transform: `rotate(${tick.angle}deg)` }"></view>
</view>
</view>
</template>
<script>
export default {
props: {
progress: {
type: Number,
default: 0
},
max: {
type: Number,
default: 100
},
tickCount: {
type: Number,
default: 10
}
},
data() {
return {
ticks: []
};
},
mounted() {
this.drawCircleProgress();
this.generateTicks();
},
watch: {
progress: 'drawCircleProgress',
max: 'generateTicks'
},
methods: {
drawCircleProgress() {
const ctx = uni.createCanvasContext('circleCanvas');
const radius = 100;
const centerX = 200;
const centerY = 200;
const angle = (this.progress / this.max) * 360;
ctx.clearRect(0, 0, 400, 400);
ctx.beginPath();
ctx.arc(centerX, centerY, radius, 0, angle * Math.PI / 180, false);
ctx.setStrokeStyle('#4caf50');
ctx.setLineWidth(10);
ctx.stroke();
ctx.draw();
},
generateTicks() {
this.ticks = Array.from({ length: this.tickCount }, (_, i) => ({
angle: (i / (this.tickCount - 1)) * 360
}));
}
}
};
</script>
<style>
.container {
position: relative;
width: 400px;
height: 400px;
margin: 0 auto;
}
.circleCanvas {
width: 100%;
height: 100%;
}
.ticks {
position: absolute;
top: 50%;
left: 50%;
width: 100%;
height: 100%;
transform: translate(-50%, -50%);
pointer-events: none;
}
.ticks view {
position: absolute;
width: 4px;
height: 16px;
background: #ccc;
top: 50%;
transform: translateX(-50%) translateY(-50%);
}
</style>
2. 使用组件
在你的页面文件中,比如 index.vue
,引入并使用这个组件:
<template>
<view>
<CircleProgress :progress="progress" :max="100" :tickCount="10" />
<button @click="incrementProgress">Increment Progress</button>
</view>
</template>
<script>
import CircleProgress from '@/components/CircleProgress.vue';
export default {
components: {
CircleProgress
},
data() {
return {
progress: 0
};
},
methods: {
incrementProgress() {
this.progress = (this.progress + 10) % 110; // Loop back to 0 after 100
}
}
};
</script>
这个示例展示了如何使用 canvas
绘制圆形进度条,并通过计算角度生成刻度。你可以根据需要进一步美化和扩展这个组件。