微信小程序---手写签名(签字版)
2021/11/8 14:09:43
本文主要是介绍微信小程序---手写签名(签字版),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
公司近期有个需要用户签名的功能,就用小程序canvas写了个,
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 }, onl oad: 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 }
这篇关于微信小程序---手写签名(签字版)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-03-30微信小程序的网络设置,及网络请求:wx.request(OBJECT)
- 2024-01-22基于taro搭建小程序多项目框架
- 2024-01-13小程序开发:在插件市场寻找步骤条组件并二开
- 2024-01-05钉钉小程序生态—企业机器人加互动卡片,改善用户体验的开始!
- 2023-12-29【UniApp】-uni-app-打包成小程序
- 2023-12-26性能翻倍!京东亿级体量小程序优化实践
- 2023-12-25小程序优化:第三方SDK过大解决方案
- 2023-11-26微信小程序文件预览和下载-文件系统
- 2023-11-2652天学习微信小程序计划No.2:注册小程序账号&安装开发者工具
- 2023-11-1952天学习微信小程序计划No.1:小程序简介