let mytime = "";
let n = 0;
var w = "";
var h = "";
Component({
properties: {
},
data: {
score: 100,
},
attached() {
console.log(this,211)
const ctx = wx.createCanvasContext("bgCanvas", this);
const ctx2 = wx.createCanvasContext("runCanvas", this);
this.setData({
ctx,
ctx2
})
let that = this;
let allSrc = 0.015 * that.data.score;
let src = allSrc / 100
this.setData({
src: src,
allSrc: allSrc
}, res => {
})
},
ready() {
let that = this
let query = wx.createSelectorQuery().in(this)
query.select('#canvas-one').boundingClientRect(function (rect) {
w = parseInt(rect.width / 2);
h = parseInt(rect.height / 2);
let ctx = that.data.ctx
ctx.arc(w, h, w - 8, 0.75 * Math.PI, 2.25 * Math.PI);
ctx.setStrokeStyle("#dddddd");
ctx.setLineWidth("8");
ctx.setLineCap("round");
ctx.stroke();
ctx.draw();
}).exec()
},
methods: {
run(e) {
let that = this;
let src = that.data.src;
let allSrc = that.data.allSrc;
n++;
if (src * n > allSrc) {
clearInterval(mytime);
n = 0;
return;
}
let ctx2 = this.data.ctx2
let grade = Math.round(src * n / 1.5 * 100);
ctx2.arc(w, h, w - 8, 0.75 * Math.PI, (0.75 + src * n) * Math.PI);
ctx2.setStrokeStyle("#84D944");
ctx2.setLineWidth("8");
ctx2.setLineCap("round");
ctx2.stroke();
ctx2.beginPath();
ctx2.setFontSize(40);
ctx2.setFillStyle("#84D944");
ctx2.setTextAlign("center");
ctx2.setTextBaseline("middle");
ctx2.fillText(grade + "%", w, h);
ctx2.draw();
},
canvasTap() {
let that = this;
clearInterval(mytime);
mytime = setInterval(that.run.bind(this), 50)
}
}
})
<!--components/arc.wxml-->
<view class='content'>
<canvas canvas-id='bgCanvas' type = "2d" id='canvas-one' class='canvasI'></canvas>
<canvas canvas-id="runCanvas" type ="2d" class='canvasII'></canvas>
<button size='mini' type='default' class='btn' style='padding:0;' catchtap='canvasTap'>点击绘制</button>
</view>
canvas宽高设置了吗 打印看下
query.select('#canvas-one').boundingClientRect(function (rect) {}