canvas元素的坑 (canvasToTempFilePath: fail canvas is empty)

前言

小程序里面的 canvas 绘图的时候老是走 fail的 回调,

canvasToTempFilePath: fail canvas is empty

简直不能忍呀

1.首先你会发现画布画不上......查看文档第二个参数要传入this。

2.canvasToTempFilePath: fail canvas is empty , 报这个错是因为调取 wx.canvasToTempFilePath 接口获取不到canvas,

同样的需要传入上下文的this

                //绘制图片
                ctx.draw(false, wx.canvasToTempFilePath({
                      canvasId: 'picture-canvas',
                      success: function (res) {
                        var tempFilePath = res.tempFilePath;
                        console.log(tempFilePath);
                        _this.isShow = false;
                        _this.$emit("createImg", tempFilePath)
                      },
                      fail: function (res) {
                        console.log(res);
                      }
                },_this))

还是要多读书 哈哈  绝了

原文地址:https://www.cnblogs.com/yf-html/p/13066249.html