微信小程序自定义组件官方教程
环形进度条的组件已经放在github上
环形进度条效果图
创建步骤
- 1、在根目录创建名为
components 的文件夹,用来放需要引用的自定义组件。
- 2、创建名为
canvas-ring 的文件夹,用来放环形进度条自定义组件。
- 3、鼠标指着
canvas-ring 的文件夹 鼠标右键 “新建 Component” 取名canvas-ring 。
结构图:
环形进度条组件的代码
canvas-ring.json
{
"component": 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
},
properties: {
canvasWidth: {
type: Number,
value: windWidth * 0.4
},
lineWidth: {
type: Number,
value: 10
},
lineColor: {
type: String,
value: "#393"
},
title: {
type: String,
value: "完成率"
},
value: {
type: Number,
value: 45
},
valueColor:{
type: String,
value: "#ff9c07"
},
maxValue: {
type: Number,
value: 100
},
minValue: {
type: Number,
value: 0
},
suffix: {
type: null,
value: "%"
},
startDegree: {
type: Number,
value: 0
}
},
data: {
canvasWidth: windWidth * 0.4,
isMarginTop: true
},
methods: {
showCanvasRing() {
if (this.data.title.replace(/(^\s*)|(\s*$)/g, "").length == 0) {
this.setData({
isMarginTop: false
})
}
var ctx = wx.createCanvasContext("circleBar", 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();
},
组件的属性介绍
属性名字 |
类型 |
默认值 |
说明 |
canvasWidth |
Number |
屏幕宽度的0.4倍 |
画布宽度 |
title |
String |
“完成率” |
标题,设置为""为空 |
value |
Number |
45 |
当前的值 |
maxValue |
Number |
100 |
最大值 |
minValue |
Number |
0 |
最小值 |
suffix |
|
“%” |
当前值的后缀名,任何类型 |
lineWidth |
Number |
10 |
线条宽度 |
lineColor |
String |
“#393” |
线条颜色 |
valueColor |
String |
“#ff9c07” |
当前值的颜色 |
startDegree |
Number |
0 |
从什么角度开始 0~360之间 (12点方向为0,18点方向为180,0点方向为360) |
环形进度条的组件已经放在github上
|
请发表评论