当前位置:   article > 正文

【微信小程序】canvas开发笔记_微信小程序 canvastotempfilepath不成功

微信小程序 canvastotempfilepath不成功

【微信小程序】canvasToTempFilePath:fail fail canvas is empty

  1. 看说明书
    最好是先看一下官方文档点此前往
    在这里插入图片描述

如果是canvas 2d 写canvas: this.canvas,,如果是旧版写canvasId: '***',

  1. 解决问题
    修改对应的代码,如下所示,然后再试试运行,会发现不报错了
    wx.canvasToTempFilePath({
      canvas: this.canvas,
      success(res) {
        console.log(res.tempFilePath);
      },
      fail(err) {
        console.log("错误", err);
      },
    });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

绘制图片问题

  1. 绘制图片无需更改情况
// 图片对象
const image = canvas.createImage()
// 图片加载完成回调
image.onload = () => {
    // 将图片绘制到 canvas 上
    ctx.drawImage(image, 0, 0)
}
// 设置图片src
image.src = 'https://open.weixin.qq.com/zh_CN/htmledition/res/assets/res-design-download/icon64_wx_logo.png'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2.图片需要变动的情况

// 使用这种方式,不要每次都重新createImage
        this._icon.bgImage =
          this._icon.bgImage ||
          (await this.getimg(this.data.bgImage, this.canvas));
        this.ctx.drawImage(
          this._icon.bgImage,
          0,
          0,
          this.toPx(this.data.width),
          this.toPx(this.data.height)
        );

    // 封装的绘制图片
    async getimg(src, canvas) {
      return new Promise((resolve, reject) => {
        let img = canvas.createImage();
        img.onload = () => {
          resolve(img);
        };
        img.src = src;
      });
    },
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

canvas层级问题

在这里插入图片描述

真机的情况下才可以看的出同层渲染的效果

绘制是否完成

如果是旧的canvas可以使用draw的箭头函数来解决

        this.ctx.draw(false, () => {
          this.process();
        });
  • 1
  • 2
  • 3
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/437402
推荐阅读
相关标签
  

闽ICP备14008679号