首页 课程 师资 教程 报名

告诉你如何解决小程序canvas层级太高的问题

  • 2022-11-28 10:38:05
  • 1278次 星辉

首先是在wxml页面的处理,我的宽高设置的是自适应宽高,只需改成自己的宽高即可,关键部分是加一个判断 wx:if="{{!canvasImg}}"

<canvas style='width: {{windowWidth}}px; height: {{windowHeight}}px'  bindtouchstart="canvasTouchStart" bindtouchmove="touchMove" bindtouchend="canvasTouchEnd" canvas-id="canvas"  wx:if="{{!canvasImg}}"></canvas>
  <image wx:else src="{{canvasImg}}" style="width: {{windowWidth}}px; height: {{windowHeight}}px" />

然后是js页面,弹框结束时直接 canvasImg=null 即可

// 需要让 弹窗等 出现在canvas上的事件
  handleCanvarToImg() {
    var that=this;
    var windowWidth = that.data.windowWidth;
    var windowHeight = that.data.windowHeight;
    wx.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: windowWidth,
      height: windowHeight,
      canvasId: 'canvas',
      success: function(res) {
        that.setData({ canvasImg: res.tempFilePath});
      }
    });
  },
  // 最后弹窗等消失时,触发的事件中需将canvasImg初始化(=null)

 

选你想看

你适合学Java吗?4大专业测评方法

代码逻辑 吸收能力 技术学习能力 综合素质

先测评确定适合在学习

在线申请免费测试名额
价值1998元实验班免费学
姓名
手机
提交