项目中用canvas生成图片时都为空白,原因为 wx.canvasToTempFilePath必须写在wx.draw()的回调中才能实现。
createImg ( imgPath) {//生成图片
const ctx = wx.createCanvasContext('canvasId')
ctx.drawImage(imgPath, 0, 0, w, 280)
ctx.draw(false,function() {
wx.canvasToTempFilePath({ //写在 draw的回调里面才能生成图片
x: 0,
y: 0,
width: 375,
height: 280,
destWidth: 375,
destHeight: 280,
canvasId: 'canvasId',
success: function (res) {
console.log(res.tempFilePath);
}
})
})
}
官方wx.canvasToTempFilePath说明:https://developers.weixin.qq.com/miniprogram/dev/api/canvas/wx.canvasToTempFilePath.html
网友评论