微信小程序进度条cavans

1.组件

c 复制代码
<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!: string

  private ctxBg: UniApp.CanvasContext | null = null
  private ctxProgress: UniApp.CanvasContext | null = null

  mounted() {
    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) return

    const center = this.size / 2
    const radius = center - this.strokeWidth / 2

    this.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) return

    const center = this.size / 2
    const radius = center - this.strokeWidth / 2
    const endAngle = (2 * Math.PI * this.currentValue) / this.maxValue - Math.PI / 2

    this.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、使用

c 复制代码
<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 = 200
  windowHeight = 600
   onLoad() {
    var that = this
    wx.getSystemInfo({
      success: function (res: any) {
        // 屏幕宽度、高度
        // 高度,宽度 单位为px

        that.windowHeight = (res.windowHeight - 60) / 2;
        console.log(that.windowHeight + '获取宽度')
      },
    });
    this.elseScore = 504.5
  }
相关推荐
Q_Q19632884753 小时前
python+uniapp基于微信小程序的医院陪诊预约系统
开发语言·spring boot·python·微信小程序·django·flask·uni-app
2501_915909064 小时前
Python 爬虫 HTTPS 实战,requests httpx aiohttp 抓取技巧、证书问题与抓包调试全流程
爬虫·python·ios·小程序·https·uni-app·iphone
低代码布道师4 小时前
少儿舞蹈小程序(19)地址列表功能实现及默认地址逻辑
前端·低代码·小程序
00后程序员张7 小时前
iOS 开发环境搭建完整指南 Xcode 安装配置、iOS 开发工具选择、ipa 打包与 App Store 上架实战经验
android·macos·ios·小程序·uni-app·iphone·xcode
從南走到北10 小时前
JAVA露营基地预约户外露营预约下单系统小程序
java·开发语言·小程序
CChenhire11 小时前
乔拓云门店小程序快速搭建攻略
小程序
00后程序员张12 小时前
App 上架全流程指南,iOS 应用发布步骤、ipa 文件上传工具、TestFlight 分发与 App Store 审核经验分享
android·ios·小程序·https·uni-app·iphone·webview
说私域12 小时前
基于开源AI大模型AI智能名片S2B2C商城小程序的产地优势产品营销策略研究
人工智能·小程序·开源
说私域12 小时前
蒸汽机革命后工业生产方式的变革与AI智能名片S2B2C商城小程序的影响
大数据·人工智能·小程序
2501_9160137412 小时前
iOS App 上架流程详解,苹果应用发布步骤、App Store 审核规则、ipa 文件上传与测试分发实战经验
android·ios·小程序·https·uni-app·iphone·webview