收藏
回答

canvasToTempFilePath: fail canvas is empty"

框架类型 问题类型 API/组件名称 终端类型 微信版本 基础库版本
小程序 Bug wx.canvasToTempFilePath canvas 微信安卓客户端 6.5.3 2.0.0

data: {

// 图标数据

doubleColumnCanvasData: {

canvasId: 'doubleColumn',

},

doubleColumnTitle: "近半年已获得认可",

doubleColumnUnit: [{

color: "#94df87",

title: "认可我的"

},

{

color: "#81bdfd",

title: "认可币数"

}

],

// 图标数据

corpId: '',

authCode: '',

userId: '',

// 悬赏点击按钮的参考值

tankk: 1,

// 点击页面切换

qiehuan: 1,

// 弹出框

qqt: '',

// 轮播图配置

imgUrls: [],

indicatorDots: true,

autoplay: true,

interval: 2000,

duration: 1000,

// 轮播图配置

user:'',

ownBadge: 0,

ownCoin: 0,

ownPoint: 0,

starUsers: [],

stDatas: '',

notices: [],

sessionId:''

},


/**

  * 组件的方法列表

  */

methods: {

// 图表转图片

zhuantu(){

setTimeout(function () {

wx.canvasToTempFilePath({

canvasId: 'doubleColumn',

success: function (res) {

var tempFilePath = res.tempFilePath;

console.log(tempFilePath);

},

fail: function (res) {

console.log(res);

}

});

}, 500);


},

onload直接调用的

页面图表显示正常就转不了图片

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

11 个回答

  • 禾店短剧系统
    禾店短剧系统
    2021-06-03

    调取 wx.canvasToTempFilePath 接口获取不到canvas,同样的需要传入上下文的this

    wx.canvasToTempFilePath({

              height: '430',

              canvasId: 'my',

              success: (res) => {

              },

              fail: (res) => {

                console.log(res);

              }

            },this);

    2021-06-03
    有用 5
    回复 2
    • 浣花公子
      浣花公子
      2022-07-19
      就是 没有传入this 的问题
      2022-07-19
      回复
    • 迟暮青男
      迟暮青男
      2022-12-05
      我传this了,还是报错
      2022-12-05
      2
      回复
  • 风定奏
    风定奏
    2020-11-10

    canvasToTempFilePath: fail canvas is empty

    文档:canvas 文档说canvas 组件的唯一标识符,若指定了 type 则无需再指定该属性canvas-id

    心得:所以把type属性去掉,保留canvas-id就可以获取到canvas

    实例:<canvas  style="width:300px;height:300px;border:1px solid red;"  canvas-id="myCanvas"></canvas>

    效果:可以获取到

    代码: wx.canvasToTempFilePath(

            {

              x: 0,

              y: 0,

              width: 300,

              height: 300,

              destWidth: 100,

              destHeight: 100,

              canvasId: "myCanvas",

              success: res => {

                console.log("wx.canvasToTempFilePath-----res:", res);

                console.log(res.tempFilePath);

              },

              fail: res => {

                console.log("wx.canvasToTempFilePath-----res:", res);

              }

            },

            this

          );

    2020-11-10
    有用 2
    回复 3
    • 林深时见鹿
      林深时见鹿
      2021-12-23
      亲测有用
      2021-12-23
      1
      回复
    • 来时路
      来时路
      2023-10-12
      我删掉type直接报错了
      2023-10-12
      回复
    • 余杰
      余杰
      02-23
      删掉type就报Cannot read property 'getContext' of null
      02-23
      回复
  • YC
    YC
    2020-01-20
    自制组件里得用this    canvasToTempFilePath({},this) 加上this
    wx.canvasToTempFilePath({
              x0,
              y0,
              width: clipW / ratio,
              height: clipH / ratio,
              destWidth: clipW,
              destHeight: clipH,
              canvasId'clip',
              success(res) => {
              },
              fail(err) => {
                wx.showToast({ title'网络错误,请稍后再试' })
              },
            }, this)
    
    2020-01-20
    有用 2
    回复 8
    • MagxL
      MagxL
      2020-01-25
      有用有用,这块,this是经验盲区
      2020-01-25
      回复
    • 偷停寂寞
      偷停寂寞
      2020-02-19
      具体 怎么用呢?可以贴代码出来看看吗?
      2020-02-19
      回复
    • Fi.fi..🍥🍬🍡
      Fi.fi..🍥🍬🍡
      2020-03-25
      加上this了没用啊,还是报错啊
      2020-03-25
      回复
    • YC
      YC
      2020-03-26回复偷停寂寞
      不要看我的参数。。只看this加的位置。是可以的
      2020-03-26
      回复
    • YC
      YC
      2020-03-26回复Fi.fi..🍥🍬🍡
      那应该是其他问题了吧。。我加了可以。你再排查排查。。
      2020-03-26
      回复
    查看更多(3)
  • 陈存胜
    陈存胜
    2020-03-22

    我也遇到这个问题, 怎么办要失业了!@腾讯

    2020-03-22
    有用 1
    回复
  • 满心欢喜!
    满心欢喜!
    2022-12-26
    <template>
    	<view style="position: fixed;top:0; left: 100%;" :style="canvasStyle">
    		<canvas :style="canvasStyle" canvas-id="confirmBillDetail" ></canvas>
    	</view>
    </template>
    <script>
    	export default {
    		data() {
    			return {
    				serial_number: '',
    				custom_name: '',
    				goodsList: [],
    				total_num: 0,
    				total_piece: 0,
    				userName: '',
    				userMobile: '',
    				useHeight: 3508,
    				billTitle: ''
    			}
    		},
    		computed: {
    			// useHeight() {
    			// 	return 300 + (this.goodsList.length - 1) * 50
    			// },
    			canvasStyle() {
    				return 'width:2479px; height:' + this.useHeight + 'px;'
    			}
    		},
    		methods: {
    			init(option) {
    				let param = option
    				console.log("param", param)
    				this.dataInit(param)
    			},
    			createPic() {
    				console.log('方法执行了')
    				let that = this
    				let cxt = null;
    				let ctx = uni.createCanvasContext('confirmBillDetail', that)
    				// uni.createSelectorQuery()
    				// 	.select("#confirmBillDetail")
    				// 	.context(function(res){
    				// 		ctx = res.context
    				// 	})
    				// 	.exec();
    				console.log('cxt',ctx)
    				let l0 = 5let l1 = 30let l2 = 930let l3 = 1280let l4 = 1680let l5 = 2030let l6 = 2474let lc = 600let t1 = 100let t2 = 180let t3 = 280let t4 = 330let t5 = 380;
    				let t6 = 530;
    				ctx.font = "50px sans-serif"
    				ctx.fillText(this.billTitle, lc, t1)
    				ctx.font = "40px sans-serif"
    				
    				ctx.fillText('订单编号:' + this.serial_number, l1, t2)
    				ctx.fillText('客户名称:' + this.custom_name, l1, t3)
    				
    					ctx.moveTo(l0, t4)
    					ctx.lineTo(l6, t4)	
    					ctx.stroke()
    					
    				ctx.fillText('商品编码/名称', l1, t5)	
    				ctx.fillText('主单位', l2, t5)	
    				ctx.fillText('主数量', l3, t5)	
    				ctx.fillText('辅单位', l4, t5)	
    				ctx.fillText('辅数量', l5, t5)	
    				console.log('2222')
    				this.goodsList.map((item, index) => {
    					// ctx.font = "12px sans-serif"
    					let list_t = (index * 150 + t6);
    					let goods_t1 = list_t - 20, goods_t2 = list_t + 20
    					ctx.fillText(item.goods_code, l1, goods_t1) 	
    					ctx.fillText(item.goods_name, l1, goods_t2)	
    					// ctx.font = "20px sans-serif"
    					ctx.fillText(item.goods_unit, l2, list_t)	
    					ctx.fillText(item.good_num, l3, list_t)	
    					ctx.fillText(item.goods_sale_unit, l4, list_t)	
    					ctx.fillText(item.goods_piece, l5, list_t)	
    				})
    				console.log('3333')
    				let goodsLength = this.goodsList.length * 150 + t6 - 85
    				
    					ctx.moveTo(5, goodsLength)	
    					ctx.lineTo(2474, goodsLength)	
    					ctx.stroke()
    				let t7 = goodsLength + 50;
    				let t8 = t7 + 100
    				let lt1 = t1 + 160, lt2 = lt1 + 200;
    				ctx.fillText('合计:', l1, t7)	
    				ctx.fillText('  ', l2, t7)	
    				ctx.fillText(this.total_num, l3, t7)	
    				ctx.fillText(' ', l4, t7)	
    				ctx.fillText(this.total_piece, l5, t7)	
    
    
    				ctx.fillText('业务员:', l1, t8)	
    				ctx.fillText(this.userName, lt1, t8)	
    				ctx.fillText(this.userMobile, lt2, t8)	
    				// this.useHeight = this.useHeight + goodsLength
    				console.log('4444')
    				ctx.draw(false, (()=> {
    					console.log("this.canvasStyle", this.canvasStyle)
    					uni.canvasToTempFilePath({
    						canvasId: 'confirmBillDetail',
    						destWidth: 2479,
    						destHeight: 3508,
    						success: function(res) {
    							// 在H5平台下,tempFilePath 为 base64
    							console.log('生成的路径', res.tempFilePath)
    							uni.uploadFile({
    								url: that.$url.upload,
    								filePath: res.tempFilePath,
    								name: 'file',
    								header: {
    									token: that.$store.state.token,
    									version: '10.00.0'
    								},
    								success: function(res){
    									console.log("resshangchuanchenggong", JSON.parse(res.data).url)
    									console.log("that.useHeight", that.useHeight)
    									that.$emit('detailUrl', {url:JSON.parse(res.data).url, goodsLength: that.goodsList.length})
    								},
    								fail: function(res) {
    									console.log("shangchuanshibai", res)
    								}
    							})
    						},
    						fail: function(res) {
    							console.log("失败了", res)
    						},
    						complete: function(res) {
    							console.log("res", res)
    						}
    					}, that)
    				})())
    
    
    			},
    			dataInit(e) {
    				console.log("初始化待处理数据", e)
    				//合同标题
    				this.billTitle = e.detailData.custom_name + '客户收货确认函'
    				console.log("this.billTitle", this.billTitle)
    				//订单编号
    				this.serial_number = e.detailData.serial_number
    				//客户名称
    				this.custom_name = e.detailData.custom_name
    				//商品列表
    				this.goodsList = e.detailData.goodsList
    				//总主数量
    				this.total_num = e.detailData.total_number
    				this.total_piece=0;
    				//总辅数量
    				e.detailData.goodsList.map(item => {
    					this.total_piece+=+item.goods_piece||0
    				})
    				//业务员名称
    				this.userName = e.detailData.user_name
    				//业务员手机号
    				this.userMobile = e.detailData.mobile
    				
    				return this.createPic()
    			}
    		}
    	}
    </script>
    
    
    
    2022-12-26
    有用
    回复 1
    • 满心欢喜!
      满心欢喜!
      2022-12-26
      canvasToTempFilePath:fail fail canvas is empty,报这个错,求大佬帮忙
      2022-12-26
      回复
  • 嘉禾生
    嘉禾生
    2020-03-25

    @官方,我用的Taro框架,使用了async/await,不可能移除的,什么时候解决这个bug

    2020-03-25
    有用
    回复 1
    • 阿步
      阿步
      2021-05-24
      解决了吗 同是taro
      2021-05-24
      回复
  • 天心
    天心
    2020-03-07

    刚解决这个问题。在所有其他代码都检查无误之后,最终定位到是因为使用了api promise化,也就是使用了下面这些代码:

    import {
        promisifyAll,
        promisify
    } from 'miniprogram-api-promise';
    const wxp = {}
    promisifyAll(wx, wxp)
    

    将上面代码注释掉就好了。有问题的同学可以检查下是否是这个原因。

    2020-03-07
    有用
    回复 4
    • 左右对称
      左右对称
      2020-03-17
      我这边也是因为api promise化 但我又必须要用到这个 不能注释掉怎么办
      2020-03-17
      回复
    • 天心
      天心
      2020-03-18回复左右对称
      这是全部api方法promise化之后,wx.wx.canvasToTempFilePath()方法出现了bug。所以我后面是对单独需要promise化的方法单独使用promisify,而不是promisifyAll。
      2020-03-18
      回复
    • 左右对称
      左右对称
      2020-03-19回复天心
      好的,谢谢你,问题已经解决了
      2020-03-19
      回复
    • 少广
      少广
      2020-12-14
      解决了,谢谢啊,  promise单独再封一次没问题了 return new Promise((resolve, reject) => {
              wx.canvasToTempFilePath({
                canvasId: ‘id’,
                success(res) {
                  resolve(res)
                },
                fail(err) {
                  reject(err)
                },
              }, this)
            })
      2020-12-14
      回复
  • 张佳乐
    张佳乐
    2019-12-06

    遇到同样的问题了,onReady里用canvas画图,然后调用也会报这个错误。

    2019-12-06
    有用
    回复 14
    • 九里
      九里
      2019-12-06
      你的图能出来吗
      2019-12-06
      回复
    • 九里
      九里
      2019-12-06
      canvas 引用wxcharts表格 能在组件页面里显示出来吗?
      2019-12-06
      回复
    • 九里
      九里
      2019-12-06回复张佳乐
      这是问题的代码片段  我想引用wxcharts表格引用的图表显示出来 麻烦能帮我看一下吗  https://developers.weixin.qq.com/s/MWeM0omL7Gdf
      2019-12-06
      回复
    • 九里
      九里
      2019-12-06
      在page1 组件页面里显示出来
      2019-12-06
      回复
    • 九里
      九里
      2019-12-06回复张佳乐
      ?大佬 能把你实现后的代码片段发我一下吗 感谢
      2019-12-06
      回复
    查看更多(9)
  • 你喜欢沉默而我姓李
    你喜欢沉默而我姓李
    2019-12-05

    我也是这个问题 , 页面也展示出来了 就是报错

    2019-12-05
    有用
    回复 1
    • 季风吹向大海
      季风吹向大海
      2020-04-20
      麻烦问一下,问题解决了吗,我也遇到这个问题了,而且加了this也没有用
      2020-04-20
      回复
  • 九里
    九里
    2019-12-03

    链接: https://pan.baidu.com/s/1EJ_gjgGLjj7gyXBXgroOOw 提取码: tj2k  这是完整的 问题抽出来看 麻烦看一下

    2019-12-03
    有用
    回复

正在加载...

登录 后发表内容
问题标签