温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

微信小程序如何实现手写签名

发布时间:2022-07-08 10:10:44 来源:亿速云 阅读:1813 作者:iii 栏目:开发技术

这篇文章主要介绍了微信小程序如何实现手写签名的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇微信小程序如何实现手写签名文章都会有所收获,下面我们一起来看看吧。

效果

微信小程序如何实现手写签名

wxml

<view class="sign">
  <view class="paper">
    <canvas class="handWriting" disable-scroll="true" bindtouchstart="touchstart1" bindtouchmove="touchmove1"  canvas-id="handWriting1">
    </canvas>
  </view>
  <view class="signBtn">
    <button size="" type="primary" bindtap="sign1ok">完成签字</button> 
    <button size="" type="warn" bindtap="reSign1">重新签字</button>
  </view>
</view>
<view class="image" hidden="{{src?false:true}}">
<image src="{{src}}" ></image>
</view>

js

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
          })
          
        }
      }))
    }
  },
});

wxss

.paper{border:1px solid #dedede; margin: 10px; height:160px }
.image{border:1px solid #dedede; margin: 10px; height:160px }
.signBtn{display: flex; margin-top:20px;}
.signTitle{ text-align: center; font-size:1.2em;margin:10px auto;}
.handWriting{width:100%}
.image image{width:100%; height:160px }

关于“微信小程序如何实现手写签名”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“微信小程序如何实现手写签名”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI