当前位置: 首页 > 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.dtcms.com/a/280028.html

相关文章:

  • 【电脑】显卡(GPU)的基础知识
  • Golang Channel与协程的完美配合指南
  • CAU数据挖掘 第五章 聚类问题
  • vscode里面怎么配置ssh步骤
  • Python+Selenium自动化爬取携程动态加载游记
  • python实现自动化sql布尔盲注(二分查找)
  • js最简单的解密分析
  • 分支战略论:Git版本森林中的生存法则
  • document.documentElement详解
  • Webshell连接工具原理
  • 渗透笔记1-4
  • html js express 连接数据库mysql
  • 【算法训练营Day12】二叉树part2
  • 进程---基础知识+命令+函数(fork+getpid+exit+wait+exec)
  • 100道K8S面试题
  • LVS初步学习
  • google浏览器::-webkit-scrollbar-thumb设置容器滚动条滑块不生效
  • langflow搭建带记忆功能的机器人
  • 【React Native】环境变量和封装 fetch
  • Knife4j快速入门
  • 【深度学习:进阶篇】--4.4.集束搜索(Beam Search)
  • 深入探索ZYNQ网络通信:四大实现方案与创新应用
  • VMWare 使用 U 盘 PE 系统安装 Win 11 ESD 镜像
  • 日常--PyCharm清除attach记录
  • Linux进程优先级机制深度解析:从Nice值到实时调度
  • 详解从零开始实现循环神经网络(RNN)
  • 实现高效、可靠的基于骨骼的人体姿态建模(第二章 基于三维人体姿态回归的语义图卷积网络)
  • 智慧城市建设关键支撑,楼宇自控系统的战略性技术价值解读
  • 用Joern执行CPGQL找到C语言中不安全函数调用的流程
  • Pythonic:Python 语言习惯和哲学的代码风格