前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >微信小程序自定义组件-环形进度条

微信小程序自定义组件-环形进度条

作者头像
别盯着我的名字看
发布2022-06-09 11:19:30
1.2K0
发布2022-06-09 11:19:30
举报
文章被收录于专栏:前端专栏前端专栏

微信小程序自定义组件官方教程

环形进度条的组件已经放在github上

环形进度条效果图

在这里插入图片描述
在这里插入图片描述

创建步骤

  • 1、在根目录创建名为components的文件夹,用来放需要引用的自定义组件。
  • 2、创建名为canvas-ring的文件夹,用来放环形进度条自定义组件。
  • 3、鼠标指着canvas-ring的文件夹 鼠标右键 “新建 Component” 取名canvas-ring

结构图:

在这里插入图片描述
在这里插入图片描述

环形进度条组件的代码

canvas-ring.json
代码语言:javascript
复制
{
  "component": true, //这一定要写成true
  "usingComponents": {} //可以引入其他自定义组件
}
canvas-ring.wxml
代码语言:javascript
复制
<canvas style="width:{{canvasWidth}}px;height:{{canvasWidth}}px;" canvas-id="circleBar">
  <cover-view class="circle-bar-wrap" style="height:{{canvasWidth}}px;">
    <cover-view class="font">
      {{title}}
    </cover-view>
    <cover-view class="val" style="color: {{valueColor}}; margin-top:{{isMarginTop?'20':'0'}}rpx">
      {{value}} {{suffix}}
    </cover-view>
  </cover-view>
</canvas>
<slot></slot>
canvas-ring.wxss
代码语言:javascript
复制
.circle-bar-wrap{
  width: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
  box-sizing: border-box;
  padding: 0 20%;
}
.circle-bar-wrap .font{
  max-height: 62rpx;
  font-size: 26rpx;
  overflow:hidden;
  text-overflow:ellipsis;
  display:-webkit-box;
  -webkit-box-orient:vertical;
  -webkit-line-clamp:2;
  white-space: normal;
}
.circle-bar-wrap .val{
  margin-top: 20rpx;
  font-size: 50rpx;
  height: 65rpx;
}
canvas-ring.js
代码语言:javascript
复制
var windWidth = wx.getSystemInfoSync().windowWidth;
Component({
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  /**
   * 组件的属性列表
   */
  properties: {
    //画布的宽度 默认占屏幕宽度的0.4倍
    canvasWidth: {
      type: Number,
      value: windWidth * 0.4
    },
    //线条宽度 默认10
    lineWidth: {
      type: Number,
      value: 10
    },
    //线条颜色 默认"#393"
    lineColor: {
      type: String,
      value: "#393"
    },
    //标题 默认“完成率”
    title: {
      type: String,
      value: "完成率"
    },
    //当前的值 默认45
    value: {
      type: Number,
      value: 45
    },
    //值的颜色 默认"#ff9c07"
    valueColor:{
      type: String,
      value: "#ff9c07"
    },
    //最大值 默认100
    maxValue: {
      type: Number,
      value: 100
    },
    //最小值 默认0
    minValue: {
      type: Number,
      value: 0
    },
    //当前值的后缀名
    suffix: {
      type: null,
      value: "%"
    },
    //从什么角度开始 0~360之间 (12点方向为0,18点方向为180,0点方向为360)
    startDegree: {
      type: Number,
      value: 0
    }

  },

  /**
   * 组件的初始数据
   */
  data: {
    canvasWidth: windWidth * 0.4,
    isMarginTop: true
  },

  /**
   * 组件的方法列表
   */
  methods: {
    showCanvasRing() {
      //去掉首位空格后如果标题为空,那么当前值的区域就没有margin-top值
      if (this.data.title.replace(/(^\s*)|(\s*$)/g, "").length == 0) {
        this.setData({
          isMarginTop: false
        })
      }
      //作画

      var ctx = wx.createCanvasContext("circleBar", this); //canvas组建封装,需要后加个this
      var circle_r = this.data.canvasWidth / 2; //画布的一半,用来找中心点和半径
      var startDegree = this.data.startDegree; //从什么角度开始
      var maxValue = this.data.maxValue; //最大值
      var minValue = this.data.minValue; //最小值
      var value = this.data.value; //当前的值
      var lineColor = this.data.lineColor; //线条颜色
      var lineWidth = this.data.lineWidth; //线条宽度
      var percent = 360 * ((value - minValue) / (maxValue - minValue)); //计算结果
      //定义起始点
      ctx.translate(circle_r, circle_r);
      //灰色圆弧
      ctx.beginPath();
      ctx.setStrokeStyle("#ebebeb");
      ctx.setLineWidth(lineWidth);
      ctx.arc(0, 0, circle_r - 10, 0, 2 * Math.PI, true);
      ctx.stroke();
      ctx.closePath();
      //有色彩的圆弧
      ctx.beginPath();
      ctx.setStrokeStyle(lineColor);
      ctx.setLineWidth(lineWidth);
      ctx.arc(0, 0, circle_r - 10, startDegree * Math.PI / 180 - 0.5 * Math.PI, percent * Math.PI / 180 + startDegree * Math.PI / 180 - 0.5 * Math.PI, false);
      ctx.stroke();
      ctx.closePath();
      ctx.draw();
    }
  }
})

使用环形进度条组件

index.json
代码语言:javascript
复制
{
  "usingComponents": {
    "canvas-ring": "/components/canvas-ring/canvas-ring"
  }
}
index.wxml
代码语言:javascript
复制
<canvas-ring id="canvasRing" value="{{c_val}}"></canvas-ring>
index.js
代码语言:javascript
复制
onReady: function() {
    var that = this;
    that.canvasRing = that.selectComponent("#canvasRing");
    that.canvasRing.showCanvasRing();
},

组件的属性介绍

属性名字

类型

默认值

说明

canvasWidth

Number

屏幕宽度的0.4倍

画布宽度

title

String

“完成率”

标题,设置为""为空

value

Number

45

当前的值

maxValue

Number

100

最大值

minValue

Number

0

最小值

suffix

null

“%”

当前值的后缀名,任何类型

lineWidth

Number

10

线条宽度

lineColor

String

“#393”

线条颜色

valueColor

String

“#ff9c07”

当前值的颜色

startDegree

Number

0

从什么角度开始 0~360之间 (12点方向为0,18点方向为180,0点方向为360)

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-5-7,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 环形进度条效果图
  • 创建步骤
  • 环形进度条组件的代码
    • canvas-ring.json
      • canvas-ring.wxml
        • canvas-ring.wxss
          • canvas-ring.js
          • 使用环形进度条组件
            • index.json
              • index.wxml
                • index.js
                • 组件的属性介绍
                相关产品与服务
                云开发 CloudBase
                云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档