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

微信小程序进度条cavans

在这里插入图片描述

1.组件

<template><view class="circle-progress-container"><!-- 画布容器 --><view class="canvas-wrapper"><canvas canvas-id="bgCanvas" class="progress-canvas" :style="{ width: size + 'px', height: size + 'px' }"></canvas><canvas canvas-id="progressCanvas" class="progress-canvas":style="{ width: size + 'px', height: size + 'px' }"></canvas></view><!-- 修正后的文字容器 --><view class="text-wrapper" :style="{width: size + 'px',height: size + 'px'}"><view class="text-content"><view class="score-line"><text class="current-value">{{ currentValue.toFixed(1) }}</text><text class="max-value">/{{ maxValue }}</text></view><text class="progress-label">总成绩</text></view></view></view>
</template><script lang="ts">
import { Vue, Component, Prop, Watch } from 'vue-property-decorator'@Component
export default class CircleProgress extends Vue {@Prop({ default: 504.5 }) readonly currentValue!: number@Prop({ default: 750 }) readonly maxValue!: number@Prop({ default: 200 }) readonly size!: number@Prop({ default: 12 }) readonly strokeWidth!: number@Prop({ default: '#4CAF50' }) readonly progressColor!: string@Prop({ default: '#e0f7e0' }) readonly bgColor!: stringprivate ctxBg: UniApp.CanvasContext | null = nullprivate ctxProgress: UniApp.CanvasContext | null = nullmounted() {this.$nextTick(() => {this.initCanvas()})}@Watch('currentValue')onValueChange() {this.drawProgress()}private initCanvas() {this.ctxBg = uni.createCanvasContext('bgCanvas', this)this.ctxProgress = uni.createCanvasContext('progressCanvas', this)this.drawBackground()this.drawProgress()}private drawBackground() {if (!this.ctxBg) returnconst center = this.size / 2const radius = center - this.strokeWidth / 2this.ctxBg.beginPath()this.ctxBg.setLineWidth(this.strokeWidth)this.ctxBg.setStrokeStyle(this.bgColor)this.ctxBg.arc(center, center, radius, 0, 2 * Math.PI)this.ctxBg.stroke()this.ctxBg.draw()}private drawProgress() {if (!this.ctxProgress) returnconst center = this.size / 2const radius = center - this.strokeWidth / 2const endAngle = (2 * Math.PI * this.currentValue) / this.maxValue - Math.PI / 2this.ctxProgress.clearRect(0, 0, this.size, this.size)this.ctxProgress.beginPath()this.ctxProgress.setLineWidth(this.strokeWidth)this.ctxProgress.setStrokeStyle(this.progressColor)this.ctxProgress.setLineCap('round')this.ctxProgress.arc(center, center, radius, -Math.PI / 2, endAngle)this.ctxProgress.stroke()this.ctxProgress.draw()}
}
</script><style lang="less" scoped>
.circle-progress-container {position: relative;display: inline-block;/* 改为inline-block避免flex影响 */.canvas-wrapper {position: relative;width: 100%;height: 100%;.progress-canvas {position: absolute;left: 0;top: 0;}}/* 修正后的文字容器样式 */.text-wrapper {position: absolute;top: 0;left: 0;display: flex;flex-direction: column;justify-content: center;align-items: center;text-align: center;.text-content {display: flex;flex-direction: column;align-items: center;transform: translateY(-5px);/* 微调垂直居中 */.score-line {display: flex;align-items: baseline;.current-value {font-size: 24px;font-weight: bold;color: #000;}.max-value {font-size: 16px;color: #999;}}.progress-label {font-size: 16px;color: #4CAF50;margin-top: 4px;}}}
}
</style>

2、使用

<view :style="'width: ' + windowHeight + 'rpx; height: ' + windowHeight + 'rpx;'"><circle-pr :current-value="elseScore" :size="windowHeight / 2" :max-value="750"progress-color="#4CAF50"></circle-pr></view><script lang="ts">
import CirclePr from '../components/circlePro.vue';
@Component({components: {CirclePr}
})
elseScore: number = 200windowHeight = 600onLoad() {var that = thiswx.getSystemInfo({success: function (res: any) {// 屏幕宽度、高度// 高度,宽度 单位为pxthat.windowHeight = (res.windowHeight - 60) / 2;console.log(that.windowHeight + '获取宽度')},});this.elseScore = 504.5}
http://www.lryc.cn/news/588593.html

相关文章:

  • 虚拟主机CPU占用100导致打不开的一次处理
  • [数据结构]#3 循环链表/双向链表
  • 微信小程序未登录状态下的导航拦截有哪些方法可以实现
  • 暑假Python基础整理 --异常处理及程序调试
  • python原生处理properties文件
  • 电动汽车制动系统及其工作原理
  • slam中的eskf观测矩阵推导
  • LangChain智能体开发实战:从零构建企业级AI助手
  • C++ Boost Aiso TCP 网络聊天(服务端客户端一体化)
  • CMake基础:覆盖项目开发的五大配套工具
  • 【机器学习深度学习】大模型推理速度与私有化部署的价值分析
  • ELK部署与使用详解
  • Docker部署语音转文字(STT)服务并接入Home Assistant
  • Dubbo高阶难题:异步转同步调用链上全局透传参数的丢失问题
  • 设备发出、接收数据帧的工作机制
  • HarmonyOS从入门到精通:动画设计与实现之九 - 实用动画案例详解(上)
  • HarmonyOS从入门到精通:动画设计与实现之九 - 实用动画案例详解(下)
  • 暑假Python基础整理 -- 文件及目录操作
  • keepalive模拟操作部署
  • 2025-7-14-C++ 学习 排序(2)
  • IoC容器深度解析:架构、原理与实现
  • 驱动开发系列60- Vulkan 驱动实现-SPIRV到HW指令的实现过程(1)
  • 分支战略论:Git版本森林中的生存法则
  • PHP password_verify() 函数
  • 什么是微服务?-核心思想:化整为零,各自为战
  • Node.js + Express的数据库AB View切换方案设计
  • 【EM算法】三硬币模型
  • 自动微分模块
  • Class9简洁实现
  • JavaScript进阶篇——第二章 高级特性核心