index.js
5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var component_1 = require('../common/component');
var utils_1 = require('../common/utils');
var color_1 = require('../common/color');
var canvas_1 = require('./canvas');
function format(rate) {
return Math.min(Math.max(rate, 0), 100);
}
var PERIMETER = 2 * Math.PI;
var BEGIN_ANGLE = -Math.PI / 2;
var STEP = 1;
component_1.VantComponent({
props: {
text: String,
lineCap: {
type: String,
value: 'round',
},
value: {
type: Number,
value: 0,
observer: 'reRender',
},
speed: {
type: Number,
value: 50,
},
size: {
type: Number,
value: 100,
observer: function () {
this.drawCircle(this.currentValue);
},
},
fill: String,
layerColor: {
type: String,
value: color_1.WHITE,
},
color: {
type: [String, Object],
value: color_1.BLUE,
observer: function () {
var _this = this;
this.setHoverColor().then(function () {
_this.drawCircle(_this.currentValue);
});
},
},
type: {
type: String,
value: '',
},
strokeWidth: {
type: Number,
value: 4,
},
clockwise: {
type: Boolean,
value: true,
},
},
data: {
hoverColor: color_1.BLUE,
},
methods: {
getContext: function () {
var _this = this;
var _a = this.data,
type = _a.type,
size = _a.size;
if (type === '') {
var ctx = wx.createCanvasContext('van-circle', this);
return Promise.resolve(ctx);
}
var dpr = wx.getSystemInfoSync().pixelRatio;
return new Promise(function (resolve) {
wx.createSelectorQuery()
.in(_this)
.select('#van-circle')
.node()
.exec(function (res) {
var canvas = res[0].node;
var ctx = canvas.getContext(type);
if (!_this.inited) {
_this.inited = true;
canvas.width = size * dpr;
canvas.height = size * dpr;
ctx.scale(dpr, dpr);
}
resolve(canvas_1.adaptor(ctx));
});
});
},
setHoverColor: function () {
var _this = this;
var _a = this.data,
color = _a.color,
size = _a.size;
if (utils_1.isObj(color)) {
return this.getContext().then(function (context) {
var LinearColor = context.createLinearGradient(size, 0, 0, 0);
Object.keys(color)
.sort(function (a, b) {
return parseFloat(a) - parseFloat(b);
})
.map(function (key) {
return LinearColor.addColorStop(
parseFloat(key) / 100,
color[key]
);
});
_this.hoverColor = LinearColor;
});
}
this.hoverColor = color;
return Promise.resolve();
},
presetCanvas: function (context, strokeStyle, beginAngle, endAngle, fill) {
var _a = this.data,
strokeWidth = _a.strokeWidth,
lineCap = _a.lineCap,
clockwise = _a.clockwise,
size = _a.size;
var position = size / 2;
var radius = position - strokeWidth / 2;
context.setStrokeStyle(strokeStyle);
context.setLineWidth(strokeWidth);
context.setLineCap(lineCap);
context.beginPath();
context.arc(position, position, radius, beginAngle, endAngle, !clockwise);
context.stroke();
if (fill) {
context.setFillStyle(fill);
context.fill();
}
},
renderLayerCircle: function (context) {
var _a = this.data,
layerColor = _a.layerColor,
fill = _a.fill;
this.presetCanvas(context, layerColor, 0, PERIMETER, fill);
},
renderHoverCircle: function (context, formatValue) {
var clockwise = this.data.clockwise;
// 结束角度
var progress = PERIMETER * (formatValue / 100);
var endAngle = clockwise
? BEGIN_ANGLE + progress
: 3 * Math.PI - (BEGIN_ANGLE + progress);
this.presetCanvas(context, this.hoverColor, BEGIN_ANGLE, endAngle);
},
drawCircle: function (currentValue) {
var _this = this;
var size = this.data.size;
this.getContext().then(function (context) {
context.clearRect(0, 0, size, size);
_this.renderLayerCircle(context);
var formatValue = format(currentValue);
if (formatValue !== 0) {
_this.renderHoverCircle(context, formatValue);
}
context.draw();
});
},
reRender: function () {
var _this = this;
// tofector 动画暂时没有想到好的解决方案
var _a = this.data,
value = _a.value,
speed = _a.speed;
if (speed <= 0 || speed > 1000) {
this.drawCircle(value);
return;
}
this.clearInterval();
this.currentValue = this.currentValue || 0;
this.interval = setInterval(function () {
if (_this.currentValue !== value) {
if (_this.currentValue < value) {
_this.currentValue += STEP;
} else {
_this.currentValue -= STEP;
}
_this.drawCircle(_this.currentValue);
} else {
_this.clearInterval();
}
}, 1000 / speed);
},
clearInterval: function () {
if (this.interval) {
clearInterval(this.interval);
this.interval = null;
}
},
},
mounted: function () {
var _this = this;
this.currentValue = this.data.value;
this.setHoverColor().then(function () {
_this.drawCircle(_this.currentValue);
});
},
destroyed: function () {
this.clearInterval();
},
});