收藏
回答

wx.canvasToTempFilePath 到底怎么用?

一直报这个错:anvasToTempFilePath: fail canvas is empty

<view class="Canvas_box" wx:if="{{canvasHidden}}">
  <canvas class="Canvas" canvas-id="myCanvas" />
  <view class="save" catch:tap="handSaveImg">保存图片</view>
</view>
createNewImg() {
  var that = this
  var context = wx.createCanvasContext('myCanvas')
  context.setFillStyle('#fff')
  context.fillRect(0, 0, 375, 667)
  var path = '/images/bannerbj.png'
  console.log(path)
  context.drawImage(path, 0, 0, 340, 550)
  context.setFontSize(14)
  context.setFillStyle('#333333')
  context.setTextAlign('center')
  let name = this.data.name
  context.fillText(name, 175, 235)
  context.stroke()
 
  context.setFontSize(50)
  context.setFillStyle('#333333')
  context.setTextAlign('center')
  let score = this.data.score
  context.fillText(score, 162, 180)
  context.stroke()
  // context.draw(false, this.saveCanvasToFile())
  context.draw(false, e => {
    wx.canvasToTempFilePath(
      {
        canvasId: 'myCanvas',
        quality: 1,
        success: res => {
          this.setData({
            canvasTemImg: res.tempFilePath,
            canvasHidden: true
          })
        },
        fail: res => {
          console.log(res)
        }
      },
      this
    )
  })
},
saveCanvasToFile() {
  let that = this
  setTimeout(() => {
    console.log('123123123')
 
    wx.canvasToTempFilePath(
      {
        canvasId: 'myCanvas',
        quality: 1,
        success: res => {
          this.setData({
            canvasTemImg: res.tempFilePath,
            canvasHidden: true
          })
        },
        fail: res => {
          console.log(res)
        }
      },
      this
    )
  }, 2000)
},


回答关注问题邀请回答
收藏

1 个回答

  • sheng
    sheng
    2019-12-11

    你的canvas都不存在,怎么绘制

    wx:if='{{canvasHidden}}'这里的canvasHidden一开始是false吧

    2019-12-11
    赞同
    回复 2
    • JIANGYUNPU
      JIANGYUNPU
      2019-12-11
      点击生成图片就会显示的。我已经解决了,看了一个大神的写法。
      2019-12-11
      回复
    • Chen
      Chen
      01-02回复JIANGYUNPU
      请问是怎么实现的,为啥我的没反应


      canvasCtx.draw(
                  false,
                  wx.canvasToTempFilePath({
                    canvasId: canvasId,
                    success: (ctx) => {
                      console.error('save', ctx);
                      // 获得图片临时路径,用来保存到本地
                    },
                  }),
                );
      01-02
      回复
登录 后发表内容
问题标签