微信小程序?qū)崿F(xiàn)手寫簽名(簽字版)
更新時間:2022年07月08日 11:03:39 作者:工具人小胡
這篇文章主要為大家詳細介紹了微信小程序?qū)崿F(xiàn)手寫簽名,簽字版,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了微信小程序?qū)崿F(xiàn)手寫簽名的具體代碼,供大家參考,具體內(nèi)容如下
公司近期有個需要用戶簽名的功能,就用小程序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 ? }, ? 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 }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript實現(xiàn)帶有子菜單和控件的slider輪播圖效果
本文通過實例代碼給大家介紹了基于js實現(xiàn)帶有子菜單和控件的slider輪播圖效果,本文附有圖片和示例代碼,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-11-11一文帶你快速理解JavaScript中call()函數(shù)的使用
這篇文章主要為大家詳細介紹了JavaScript中call()函數(shù)的使用的相關(guān)知識,文中的示例代碼講解詳細,具有一定的學習價值,需要的可以參考一下2023-03-03