cl-progress-circle.uvue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <template>
  2. <view class="cl-progress-circle" :class="[pt.className]">
  3. <canvas
  4. class="cl-progress-circle__canvas"
  5. :id="canvasId"
  6. :style="{
  7. height: `${props.size}px`,
  8. width: `${props.size}px`
  9. }"
  10. ></canvas>
  11. <slot name="text">
  12. <cl-text
  13. :value="`${value}${unit}`"
  14. :pt="{
  15. className: parseClass(['absolute', pt.text?.className])
  16. }"
  17. v-if="showText"
  18. ></cl-text>
  19. </slot>
  20. </view>
  21. </template>
  22. <script lang="ts" setup>
  23. import {
  24. getColor,
  25. getDevicePixelRatio,
  26. isDark,
  27. isHarmony,
  28. parseClass,
  29. parsePt,
  30. uuid
  31. } from "@/cool";
  32. import { computed, getCurrentInstance, onMounted, ref, watch, type PropType } from "vue";
  33. import type { PassThroughProps } from "../../types";
  34. defineOptions({
  35. name: "cl-progress-circle"
  36. });
  37. const props = defineProps({
  38. pt: {
  39. type: Object,
  40. default: () => ({})
  41. },
  42. // 数值 (0-100)
  43. value: {
  44. type: Number,
  45. default: 0
  46. },
  47. // 圆形大小
  48. size: {
  49. type: Number,
  50. default: 120
  51. },
  52. // 线条宽度
  53. strokeWidth: {
  54. type: Number,
  55. default: 8
  56. },
  57. // 进度条颜色
  58. color: {
  59. type: String as PropType<string | null>,
  60. default: null
  61. },
  62. // 底色
  63. unColor: {
  64. type: String as PropType<string | null>,
  65. default: null
  66. },
  67. // 是否显示文本
  68. showText: {
  69. type: Boolean,
  70. default: true
  71. },
  72. // 单位
  73. unit: {
  74. type: String,
  75. default: "%"
  76. },
  77. // 起始角度 (弧度)
  78. startAngle: {
  79. type: Number,
  80. default: -Math.PI / 2
  81. },
  82. // 是否顺时针
  83. clockwise: {
  84. type: Boolean,
  85. default: true
  86. },
  87. // 动画时长
  88. duration: {
  89. type: Number,
  90. default: 500
  91. }
  92. });
  93. const { proxy } = getCurrentInstance()!;
  94. // 获取设备像素比
  95. const dpr = getDevicePixelRatio();
  96. // 透传样式类型定义
  97. type PassThrough = {
  98. className?: string;
  99. text?: PassThroughProps;
  100. };
  101. // 解析透传样式配置
  102. const pt = computed(() => parsePt<PassThrough>(props.pt));
  103. // canvas组件上下文
  104. let canvasCtx: CanvasContext | null = null;
  105. // 绘图上下文
  106. let drawCtx: CanvasRenderingContext2D | null = null;
  107. // 生成唯一的canvas ID
  108. const canvasId = `cl-progress-circle__${uuid()}`;
  109. // 当前显示值
  110. const value = ref(0);
  111. // 绘制圆形进度条
  112. function drawProgress() {
  113. if (drawCtx == null) return;
  114. const centerX = (props.size / 2) * dpr;
  115. const centerY = (props.size / 2) * dpr;
  116. const radius = ((props.size - props.strokeWidth) / 2) * dpr;
  117. // 清除画布
  118. // #ifdef APP
  119. drawCtx!.reset();
  120. // #endif
  121. // #ifndef APP
  122. drawCtx!.clearRect(0, 0, props.size * dpr, props.size * dpr);
  123. // #endif
  124. // 优化渲染质量
  125. drawCtx!.textBaseline = "middle";
  126. drawCtx!.textAlign = "center";
  127. drawCtx!.miterLimit = 10;
  128. // 保存当前状态
  129. drawCtx!.save();
  130. // 优化的圆环绘制
  131. const drawCircle = (startAngle: number, endAngle: number, color: string) => {
  132. if (drawCtx == null) return;
  133. drawCtx!.beginPath();
  134. drawCtx!.arc(centerX, centerY, radius, startAngle, endAngle, false);
  135. drawCtx!.strokeStyle = color;
  136. drawCtx!.lineWidth = props.strokeWidth * dpr;
  137. drawCtx!.lineCap = "round";
  138. drawCtx!.lineJoin = "round";
  139. drawCtx!.stroke();
  140. };
  141. // 绘制底色圆环
  142. drawCircle(
  143. 0,
  144. 2 * Math.PI,
  145. props.unColor ?? (isDark.value ? getColor("surface-700") : getColor("surface-200"))
  146. );
  147. // 绘制进度圆弧
  148. if (value.value > 0) {
  149. const progress = Math.max(0, Math.min(100, value.value)) / 100;
  150. const endAngle = props.startAngle + (props.clockwise ? 1 : -1) * 2 * Math.PI * progress;
  151. drawCircle(props.startAngle, endAngle, props.color ?? getColor("primary-500"));
  152. }
  153. }
  154. // 动画更新数值
  155. function animate(targetValue: number) {
  156. const startValue = value.value;
  157. const startTime = Date.now();
  158. function update() {
  159. // 获取当前时间
  160. const currentTime = Date.now();
  161. // 计算动画经过的时间
  162. const elapsed = currentTime - startTime;
  163. // 计算动画进度
  164. const progress = Math.min(elapsed / props.duration, 1);
  165. // 缓动函数
  166. const easedProgress = 1 - Math.pow(1 - progress, 3);
  167. // 计算当前值
  168. value.value = Math.round(startValue + (targetValue - startValue) * easedProgress);
  169. // 绘制进度条
  170. drawProgress();
  171. if (progress < 1) {
  172. if (canvasCtx != null) {
  173. // @ts-ignore
  174. canvasCtx!.requestAnimationFrame(() => {
  175. update();
  176. });
  177. }
  178. }
  179. }
  180. update();
  181. }
  182. // 初始化画布
  183. function initCanvas() {
  184. setTimeout(
  185. () => {
  186. uni.createCanvasContextAsync({
  187. id: canvasId,
  188. component: proxy,
  189. success: (context: CanvasContext) => {
  190. // 设置canvas上下文
  191. canvasCtx = context;
  192. // 获取绘图上下文
  193. drawCtx = context.getContext("2d")!;
  194. // 设置宽高
  195. drawCtx!.canvas.width = props.size * dpr;
  196. drawCtx!.canvas.height = props.size * dpr;
  197. // 开始动画
  198. animate(props.value);
  199. }
  200. });
  201. },
  202. isHarmony() ? 100 : 0
  203. );
  204. }
  205. onMounted(() => {
  206. initCanvas();
  207. // 监听value变化
  208. watch(
  209. computed(() => props.value),
  210. (val: number) => {
  211. animate(Math.max(0, Math.min(100, val)));
  212. },
  213. {
  214. immediate: true
  215. }
  216. );
  217. watch(
  218. computed(() => [props.color, props.unColor, isDark.value]),
  219. () => {
  220. drawProgress();
  221. }
  222. );
  223. });
  224. defineExpose({
  225. animate
  226. });
  227. </script>
  228. <style lang="scss" scoped>
  229. .cl-progress-circle {
  230. @apply flex flex-col items-center justify-center relative;
  231. }
  232. </style>