微信小程序canvas生成图片为空白问题

项目中用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);
      }
    })
  })
}
发布了270 篇原创文章 · 获赞 102 · 访问量 50万+

猜你喜欢

转载自blog.csdn.net/hahahhahahahha123456/article/details/105500662