微信小程序自定義組件實現環形進度條

這篇文章主要爲大家詳細介紹了微信小程序自定義組件實現環形進度條,具有一定的參考價值,感興趣的小夥伴們可以參考一下

本文實例爲大家分享了微信小程序實現環形進度條的具體代碼,供大家參考,具體內容如下

微信小程序自定義組件官方教程

環形進度條的組件已經放在github

環形進度條效果圖

創建步驟

1、在根目錄創建名爲components的文件夾,用來放需要引用的自定義組件。
2、創建名爲canvas-ring的文件夾,用來放環形進度條自定義組件。
3、鼠標指着canvas-ring的文件夾 鼠標右鍵 “新建 Component” 取名canvas-ring。

結構圖:

環形進度條組件的代碼

canvas-ring.json

{
 "component": true, //這一定要寫成true
 "usingComponents": {} //可以引入其他自定義組件
}

canvas-ring.wxml

<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

.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

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

{
 "usingComponents": {
 "canvas-ring": "/components/canvas-ring/canvas-ring"
 }
}

index.wxml

<canvas-ring id="canvasRing" value="{{c_val}}"></canvas-ring>

index.js

onReady: function() {
 var that = this;
 that.canvasRing = that.selectComponent("#canvasRing");
 that.canvasRing.showCanvasRing();
},

組件的屬性介紹

環形進度條的組件已經放在github

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持神馬文庫。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章