const app = getApp()
Page({
data: {},
onLoad: function () {
// 通过 SelectorQuery 获取 Canvas 节点
wx.createSelectorQuery()
.select('#canvas')
.fields({
node: true,
size: true,
})
.exec(this.init.bind(this))
},
init(res) {
const ctx = canvas.getContext('2d')
this.drawCar(ctx)
const img = canvas.createImage()
img.onload = () => {
this._img = img
}
img.src = './car.png'
},
drawCar(ctx) {
ctx.drawImage(this._img, 0, 0, 100, 50)
ctx.restore()
}
})
遇到了同样的问题,然后我是在img.onload的函数里使用的ctx.drawImage