uniapp+canvas绘制环形进度条

创建一个progress-circle.vue文件

<template>
  <div>
    <canvas
      :canvas-id="'progress' + id"
      :style="{ width: size + 'rpx', height: size + 'rpx' }"
    ></canvas>
    <slot></slot>
  </div>
</template>

<script>
export default {
  props: ["id", "percent", "progressColor"],
  data() {
    return {
      sizeNum: this.size, //最终的整体尺寸
      strokeWidthNum: this.strokeWidth, //最终的轨道宽度数值,
      bgColor: "#F0F3FA",
      timeBgColor: "#e39214",
      size: 125,
      strokeWidth: 10,
    };
  },
  mounted() {
    this.$cu.log("canvas");
    this.drawProgress();
    // this.drawTimeProgress();
  },
  methods: {
    drawProgress() {
      //尺寸与轨道宽度自适应处理
      const screenWidth = uni.getSystemInfoSync().screenWidth;
      this.sizeNum = (this.size / 750) * screenWidth;
      this.strokeWidthNum = (this.strokeWidth / 750) * screenWidth;

      const ctx = uni.createCanvasContext(`progress${this.id}`, this);
      const centerX = this.sizeNum / 2;
      const centerY = this.sizeNum / 2;
      const radius = (this.sizeNum - this.strokeWidthNum) / 2;
      const startAngle = 1.5 * Math.PI;
      const endAngle = startAngle - (this.percent / 100) * 2 * Math.PI;
      // 绘制背景圆
      ctx.beginPath();
      ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI);
      ctx.setFillStyle("#FFFFFF");
      ctx.fill();
      // 绘制轨道圆环
      ctx.beginPath();
      ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI);
      ctx.setLineWidth(this.strokeWidthNum);
      ctx.setStrokeStyle(this.bgColor);
      ctx.setLineCap("round");
      ctx.stroke();
      // 绘制进度圆环
      ctx.beginPath();
      ctx.arc(centerX, centerY, radius, startAngle, endAngle, true);
      ctx.setLineWidth(this.strokeWidthNum);
      ctx.setStrokeStyle(this.progressColor);
      ctx.setLineCap("round");
      ctx.stroke();
      ctx.draw(true);
    },
    drawTimeProgress() {
      //尺寸与轨道宽度自适应处理
      const screenWidth = uni.getSystemInfoSync().screenWidth;
      this.sizeNum = (this.size / 750) * screenWidth;
      this.strokeWidthNum = (this.strokeWidth / 750) * screenWidth;

      const ctx = uni.createCanvasContext(`progress${this.id}`, this);
      const centerX = this.sizeNum / 2;
      const centerY = this.sizeNum / 2;
      const radius = (this.sizeNum - this.strokeWidthNum) / 2;
      const startAngle = 1.5 * Math.PI;
      const endAngle = startAngle - (this.percent / 100) * 2 * Math.PI;
      // 绘制轨道圆环
      ctx.beginPath();
      ctx.arc(centerX, centerY, radius - 12, 0, 2 * Math.PI);
      ctx.setLineWidth(this.strokeWidthNum);
      ctx.setStrokeStyle(this.timeBgColor);
      ctx.setLineCap("round");
      ctx.stroke();
      // 绘制进度圆环
      ctx.beginPath();
      ctx.arc(centerX, centerY, radius - 12, startAngle, endAngle, true);
      ctx.setLineWidth(this.strokeWidthNum);
      ctx.setStrokeStyle(this.timeBgColor);
      ctx.setLineCap("round");
      ctx.stroke();
      ctx.draw(true);
    },
  },
  watch: {
    percent: {
      handler() {
        this.drawProgress();
        // this.drawTimeProgress();
      },
    },
  },
};
</script>

在其他页面使用

<progressCircle
          :percent="percent"
          :progressColor="col"
          :id="id"
        >
          <div class="content">
             <!-- 插槽位置放置需要的内容 -->
          </div>
</progressCircle>

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
uniapp中使用canvas绘制水印,可以通过以下步骤实现: 1. 首先,在uniapp项目中创建一个新的页面或组件,用于显示canvas画布。 2. 在该页面或组件中,使用`<canvas>`标签创建一个画布元素,并设置其宽度和高度。 3. 使用`uni.createCanvasContext()`方法创建一个绘图上下文对象,用于操作画布。 4. 使用绘图上下文对象的相关方法,如`fillText()`、`fillRect()`等来绘制水印内容。 5. 通过调用绘图上下文对象的`draw()`方法将绘制的内容显示在画布上。 下面是一个简单的示例代码,演示了如何在uniapp中使用canvas绘制水印: ``` <template> <view> <canvas id="canvas" style="width: 300px; height: 200px;"></canvas> </view> </template> <script> export default { onReady() { const ctx = uni.createCanvasContext('canvas', this); ctx.setFillStyle('rgba(0, 0, 0, 0.5)'); // 设置水印颜色和透明度 ctx.setFontSize(16); // 设置水印字体大小 ctx.fillText('水印内容', 10, 20); // 绘制水印文本 ctx.draw(); // 绘制到画布上 } } </script> ``` 在上述代码中,我们使用了uniapp提供的`uni.createCanvasContext()`方法创建了一个绘图上下文对象,并通过该对象的`setFillStyle()`、`setFontSize()`以及`fillText()`方法来设置水印的样式和内容。最后,通过调用`draw()`方法将绘制的水印显示在画布上。 需要注意的是,在uniapp中使用canvas绘制水印时,需要确保在`onReady()`生命周期函数中进行绘制操作,以确保画布已经完全加载。另外,还需要注意设置画布的宽度和高度,以及调整水印的位置和样式,以满足具体需求。 希望以上内容对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值