判断canvas是否有内容(浙政钉小程序)

1.新建两个canvas标签,一个空白画布 一个使用的画布
2.点击确认的时候比较两个画布 data URL 数据,一致说明画布没内容

    第一个是空白的
    <view a:if="{{showCanvas}}" class="sign-box2">
      <canvas id="awesomeCanvas2"/>
    </view>
    第二个是使用的
    <view a:if="{{showCanvas}}" class="sign-box">
      <view class="canvas-box" catchTouchStart="canvasStart" catchTouchMove="canvasMove" catchTouchEnd="canvasEnd" catchTouchCancel="canvasCancel">
        <canvas id="awesomeCanvas"/>
      </view>
      <view class="canvas-btn-box">
        <view catchTap="canvasBoxCancel">取消</view>
        <view catchTap="canvasBoxBack">撤销</view>
        <view catchTap="makeSure">确认</view>
      </view>
    </view>
   // 打开canvas弹窗 初始化
   dosign() {
    if (this.data.isabled == "false") {
      return false;
    }
    dd.hideKeyboard();
    this.setData({
      showCanvas: true
    });
    this.data.ctx = dd.createCanvasContext("awesomeCanvas");
    this.data.ctx.save();
    this.data.ctx.beginPath();
    this.data.ctx.setLineJoin("bevel");
    this.data.ctx.setLineWidth(5);
    this.data.ctx.setLineCap("round");

    this.data.ctx2 = dd.createCanvasContext("awesomeCanvas2");
    this.data.ctx2.save();
    this.data.ctx2.beginPath();
    this.data.ctx2.setLineJoin("bevel");
    this.data.ctx2.setLineWidth(5);
    this.data.ctx2.setLineCap("round");
  },
  // 撤销 清空画布内容
  canvasBoxBack() {
    this.data.ctx.restore()
    this.data.ctx.draw();
    this.data.ctx.closePath();
    this.data.ctx.beginPath();
    this.data.ctx.setLineJoin("bevel");
    this.data.ctx.setLineWidth(5);
    this.data.ctx.setLineCap("round");
  },
  // 取消
  canvasBoxCancel() {
    this.setData({
      showCanvas: false
    })
  },
  canvasStart(e) {
    this.data.lineBegin = true
    let x = e.touches[0].clientX - e.target.offsetLeft,
      y = e.touches[0].clientY - e.target.offsetTop;   // 获取触摸点在画板(canvas)的坐标
    this.data.point.x = x;
    this.data.point.y = y;
  },
  canvasMove(e) {
    if (this.data.lineBegin) {
      let x = e.touches[0].clientX - e.target.offsetLeft,
        y = e.touches[0].clientY - e.target.offsetTop;   // 获取触摸点在画板(canvas)的坐标
      this.data.ctx.moveTo(this.data.point.x, this.data.point.y);
      this.data.ctx.lineTo(x, y);
      this.data.ctx.stroke();
      this.data.ctx.draw();
      this.data.point.x = x, this.data.point.y = y;   // 重置点坐标为上一个坐标
    }
  },
  canvasEnd() {
    if (this.data.lineBegin) {
      this.data.lineBegin = false
    }
  },
  canvasCancel() {
    this.data.ctx.closePath();
    this.data.lineBegin = false
  },
  //签名保存
  makeSure() {
    let that = this

    // 空白的尺寸
    this.data.ctx2.toDataURL(
      {
        destWidth: 93.75,
        destHeight: 50,
        success(res) {
          that.data.ctx.toDataURL(
            {
              destWidth: 93.75,
              destHeight: 50,
              success(res2) {
                if (res != res2) { // 用空白画布和使用的画布比较 看数据是否一致,一致说明没有写内容
                  that.data.ctx.toTempFilePath(
                    {
                      destWidth: 93.75,
                      destHeight: 50,
                      success(res) {
                        that.setData({
                          ['obj.IsGenerateJoint']: true,
                          showCanvas: false,
                          AutographImage: res.apFilePath,
                        })
                        let upObj = {
                          code: "电子签名附件",
                          url: res.apFilePath,
                        }
                        that.uploadpicImage(upObj)
                      },
                      error(err) {
                      }
                    })
                }
              },
              error(err) {
              }
            })
        },
        error(err) {
        }
      })

  },
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值