微信小程序手写签名canvas画布

 <view class="paper">
    <canvas class="handWriting" disable-scroll="true" 
      bindtouchstart="touchstart1" 
      bindtouchmove="touchmove1"  
      canvas-id="handWriting1">
    </canvas>
  </view>
此段为html片段
.paper{
    height: 400rpx;
}
.handWriting{
    height: 400rpx;
    width: 662rpx;
}
此段位css片段
Page({

    /**
     * 页面的初始数据
     */
    data: {
        context1: null,
        hasDraw:false, //默认没有画
        src:null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function() {
        var context1 = wx.createCanvasContext('handWriting1');
        context1.setStrokeStyle("#000000")
        context1.setLineWidth(3);
        this.setData({
          context1: context1,
        })
      },
      touchstart1: function(e) {
        var context1 = this.data.context1;
        context1.moveTo(e.touches[0].x, e.touches[0].y);
        this.setData({
          context1: context1,
          hasDraw : true, //要签字了
        });
      },
      touchmove1: function(e) {
        var x = e.touches[0].x;
        var y = e.touches[0].y;
        var context1 = this.data.context1;
        context1.setLineWidth(3);
        context1.lineTo(x, y);
        context1.stroke();
        context1.setLineCap('round');
        context1.draw(true);
        context1.moveTo(x, y);
      },
      reSign1: function() { //重新画
        var that = this;
        var context1 = that.data.context1;
        context1.draw(); //清空画布
        that.setData({
          hasDraw: false, //没有画
          src: null
        });
      },
      sign1ok: function () {
        var that = this;
        if(!that.data.hasDraw){
          console.log("签字是空白的 没有签字")
        }else{
          var context1 = that.data.context1;
          context1.draw(true, wx.canvasToTempFilePath({
            canvasId: 'handWriting1',
            success(res) {
              console.log(res.tempFilePath) //得到了图片下面自己写上传吧
              that.setData({
                src: res.tempFilePath
              })
              
            }
          }))
        }
      },
      signAgain(){
          this.reSign1()
      },
      //上面这个方法这是调用重签清空画布
      signConfirm(){
          this.sign1ok()
      },
      //上面这个方法这是点击确认更新src图片
)}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值