在自定义组件中调用canvasToTempFilePath,提示canvasToTempFilePath: fail canvas is empty
wx.canvasToTempFilePath(Object object, Object this)
wx.createCanvasContext(string canvasId, Object this)
这两个方法第二个参数已经写了this,
const ctx = wx.createCanvasContext('sku-bill-canvas', that);
....
...
ctx.draw(false, setTimeout(() => {
console.log(3)
//生成分享图
wx.canvasToTempFilePath({
canvasId: 'sku-bill-canvas',
success: function (res) {
console.info(res.tempFilePath)
that.setData({
skuBillPreUrl: res.tempFilePath,
showCanvas: false
})
//海报预览图
app.globalData.skuBillPreUrl = res.tempFilePath
console.info('skuBillPreUrl:' + that.data.skuBillPreUrl)
}, fail: function (res) {
console.info(res);
}
}, this)
}, 300))
https://github.com/qq674785876/wxa-comp-canvas-drag-master 给你个实例,有问题来一起讨论
画布标识,传入 canvas 组件的 canvas-id。要不你就设置width,height为0或者不显示了
你忘记设置<canvas canvas-id="sku-bill-canvas" 了。而是设置 id="sku-bill-canvas"了?
画的图出来了么 先不管生成那一步
const ctx = wx.createCanvasContext(3sku-bill-canvas, that); 这里 3sku-bill-canvas 这个canvas-id是否存在
图片链接:
https://mmbiz.qpic.cn/mmbiz_png/WcgUoX8D9LB2o7pLYydVDzpdcJb3SbgDPwYrBficjp0skricrQZWP1cHMLUxtmCn1VZHcut6RQA824644ccpJfVw/0?wx_fmt=png