码迷,mamicode.com
首页 > 微信 > 详细

wx小程序用canvas生成图片流程与注意事项

时间:2018-09-07 20:08:20      阅读:489      评论:0      收藏:0      [点我收藏+]

标签:bsp   set   temp   回调   load   延时   ...   pat   绘制   

1、需要画入canvas的 图片都需要先缓存到本地 

  

let ps = []
    ps.push(that.loadImageFun(this.statusInfo.avatar_url, "headImg"))
    Promise.all(ps).then(res => {
    // 画图
})


loadImageFun(url, index) {
    let _this = this
    return new Promise((resolve, reject) => {
      wx.downloadFile({
        url,
        success(res) {
          if (res.statusCode === 200) {
            _this.loadedImgs[index] = res.tempFilePath
            resolve()
          }
          else
            reject()
        },
        fail() {
          reject()
        }
      })
    })
  },

2、从canvas中导出图片时,android手机生成不了图片。(原因是,draw的回调函数在android上是假回调,因为canvas还没准备好,所以得手动加延时了...)

//绘制图片
      ctx.draw(false, () => {
        setTimeout(()=>{
          wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: size,
            height: imgHeight,
            destWidth: size,
            destHeight: imgHeight,
            canvasId: ‘shcanvas‘,
            success: function (res) {
              var tempFilePath = res.tempFilePath;
              that.share_img = tempFilePath;
              // wx.previewImage({
              //   urls: [that.share_img],
              // })
            }
          })
        },1000)
      })

 

wx小程序用canvas生成图片流程与注意事项

标签:bsp   set   temp   回调   load   延时   ...   pat   绘制   

原文地址:https://www.cnblogs.com/Mvloveyouforever/p/9606616.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!