小程序用户大面积反馈海报无法生成(用canvas 2d 画的)
在自定组件里用this.createSelectorQuery去拿canvas标签的实例(而且我们也做了降级,如果取不到间隔1秒取10次),但后台日志还是大量报上来取不到?我们身边的10多台测试机都没有能复现的?
后台的异常告警:
代码如下:
index.js
/** 初始化canvas */
async init() {
if (this.ctx) {
return
}
const query = this.createSelectorQuery().in(this)
let canvas
// 降级间隔1s,最多取10次
for (let i = 0, len = 10; i < len; i++) {
const node = await this.selectCanvasNode(query)
if (node) {
canvas = node
break
}
// setTimeout Promise
await timer(1000)
}
if (canvas) {
this.canvas = canvas
this.ctx = this.canvas.getContext('2d')
} else {
throw new Error('初始化canvas实例异常')
}
}
selectCanvasNode(query) {
return new Promise((resolve) => {
query.select('#canvas')
.node((res) => {
resolve(res?.node)
})
.exec()
})
}
index.wxml
<view class="layer">
<canvas type="2d" id="canvas" />
</view>