微信小程序canvas實現(xiàn)手寫簽名
更新時間:2022年07月07日 07:06:14 作者:小白_0615
這篇文章主要為大家詳細介紹了微信小程序canvas實現(xiàn)手寫簽名,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了微信小程序canvas實現(xiàn)手寫簽名的具體代碼,供大家參考,具體內(nèi)容如下
很多時候,程序中需要用到簽名的功能,附上源碼(微信小程序)
.wxml
<view class="canvasBox"> ? ? ? <view class="canvasTitle">請簽名:</view> ? ? ? <view class="canvasContent"> ? ? ? ? <view class="singatureTag">簽名區(qū)域</view> ? ? ? ? <canvas style="width: {{canvasw}}px; height: {{canvash}}rpx;line-height:{{canvash}}rpx" disable-scroll="true" ? ? ? ? ? canvas-id="myCanvas" bindtouchstart="touchStart" bindtouchmove="touchMove" bindtouchend="touchEnd" ? ? ? ? ? touchcancel="canvasEnd" binderror="canvasIdErrorCallback"></canvas> ? ? ? </view> </view> ? <view class="next" style="padding-bottom:calc({{iphonex_height}}px + 20rpx);"> ? ? ? <view class="next-list"> ? ? ? ? <van-button round custom-style='font-size:30rpx;border-radius:20px;width:60%;' type="info" plain color="#5359a7" ? ? ? ? ? catchtap="clearDraw">清 除</van-button> ? ? ? </view> ? ? ? <view class="next-list"> ? ? ? ? <van-button round custom-style='font-size:30rpx;width:60%;' type="info" color="#1417b7" catchtap="submitDraw"> ? ? ? ? ? 提 交</van-button> ? ? ? </view> </view>
.js
data: { ? ? iphonex_height: app.globalData.iphonex_safe_area_height, //inphonex安全區(qū)高度 ? ? currentColor: '#000', ? ? canvasw: 0, ? ? canvash: 0, ? ? userId: '', ? ? signFile: '', ? ? base64: '', ? }, ? /** ? ?* 生命周期函數(shù)--監(jiān)聽頁面加載 ? ?*/ ? onLoad: function (options) { ? ? var that = this; ? ? that.setData({ ? ? ? userId: options.userId, ? ? ? signFile: options.signFile, ? ? ? name: options.name, ? ? ? drawId_: options.drawId, ? ? ? list_: JSON.parse(options.list_), ? ? ? userID: options.userID, ? ? }) ? ? console.log(that.data.list_); ? ? ? this.begin = false; ? ? this.startX = 0; ? ? this.startY = 0; ? ? this.context = wx.createCanvasContext('myCanvas') ? ? this.context.setLineWidth(4); ? ? this.context.setLineCap('round'); ? ? this.context.setLineJoin('round'); ? ? wx.getSystemInfo({ ? ? ? success: function (res) { ? ? ? ? that.setData({ ? ? ? ? ? canvasw: res.windowWidth, //設(shè)備寬度 ? ? ? ? ? canvash: 400 ? ? ? ? }); ? ? ? } ? ? }); ? }, ? ? touchStart: function (e) { ? ? this.lineBegin(e.touches[0].x, e.touches[0].y) ? }, ? ? // 繪制中 手指在屏幕上移動 ? touchMove: function (e) { ? ? if (this.begin) { ? ? ? this.lineAddPoint(e.touches[0].x, e.touches[0].y); ? ? ? this.context.draw(true); ? ? } ? }, ? ? // 繪制結(jié)束 手指抬起 ? touchEnd: function () { ? ? this.lineEnd(); ? }, ? ? // 繪制線條結(jié)束 ? lineEnd: function () { ? ? this.context.closePath(); ? ? this.begin = false; ? }, ? ? // 開始繪制線條 ? lineBegin: function (x, y) { ? ? this.begin = true; ? ? this.context.beginPath() ? ? this.startX = x; ? ? this.startY = y; ? ? this.context.moveTo(this.startX, this.startY) ? ? this.lineAddPoint(x, y); ? }, ? ? // 繪制線條中間添加點 ? lineAddPoint: function (x, y) { ? ? this.context.moveTo(this.startX, this.startY) ? ? this.context.lineTo(x, y) ? ? this.context.stroke(); ? ? this.startX = x; ? ? this.startY = y; ? },
提交:請求接口
//提交 ? submitDraw() { ? ? console.log("提交"); ? ? var that = this; ? ? console.log(that.data.name); ? ? //跳轉(zhuǎn)攜帶的參數(shù) ? ? if (that.data.name == "sponsor_drawing") { ? ? ? console.log("申請人簽字"); ? ? ? wx.canvasToTempFilePath({ ? ? ? ? canvasId: 'myCanvas', ? ? ? ? success: function (res) { ? ? ? ? ? if (that.startY == 0) { ? ? ? ? ? ? wx.showToast({ ? ? ? ? ? ? ? icon: 'none', ? ? ? ? ? ? ? title: '您還沒有簽名', ? ? ? ? ? ? }) ? ? ? ? ? } else { ? ? ? ? ? ? //整理簽名格式 ? ? ? ? ? ? that.setData({ ? ? ? ? ? ? ? //轉(zhuǎn)base64 ? ? ? ? ? ? ? base64: wx.getFileSystemManager().readFileSync(res.tempFilePath, "base64").replace(/\s/g, ""), ? ? ? ? ? ? }) ? ? ? ? ? ? console.log("base64"); ? ? ? ? ? ? console.log(that.data.base64); ? ? ? ? ? ? //請求接口 提交信息 ? ? ? ? ? ? //申請人簽字 ? ? ? ? ? ? let url = '/release/release?drawId=' + that.data.drawId_ + '&signature=' + `${encodeURIComponent(that.data.base64)}`; ? ? ? ? ? ? let data = {} ? ? ? ? ? ? wx.showLoading({ ? ? ? ? ? ? ? title: '發(fā)布中', ? ? ? ? ? ? }) ? ? ? ? ? ? app.wxRequest('POST', 1, url, data, (res) => { ? ? ? ? ? ? ? if (res.code == 200) { ? ? ? ? ? ? ? ? wx.hideLoading(); ? ? ? ? ? ? ? ? Toast('提存信息發(fā)布成功'); ? ? ? ? ? ? ? ? if (that.data.list_ != null) { ? ? ? ? ? ? ? ? ? var issuer_ = that.data.list_.issuer; //發(fā)布人信息 ? ? ? ? ? ? ? ? ? var accept_ = that.data.list_.accept; //受領(lǐng)人信息 ? ? ? ? ? ? ? ? ? if (app.globalData.note_bool) { ? ? ? ? ? ? ? ? ? ? for (var i = 0; i < accept_.length; i++) { ? ? ? ? ? ? ? ? ? ? ? if (list_ != null) { ? ? ? ? ? ? ? ? ? ? ? ? that.call_function(1, issuer_[0].name, accept_[i].name, accept_[i].phone); //短信通知 ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? setTimeout(function () { ? ? ? ? ? ? ? ? ? wx.switchTab({ ? ? ? ? ? ? ? ? ? ? url: '/pages/user/mydrawing/index', ? ? ? ? ? ? ? ? ? }); ? ? ? ? ? ? ? ? }, 500) ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? wx.hideLoading(); ? ? ? ? ? ? ? ? Toast('提存信息發(fā)布失敗'); ? ? ? ? ? ? ? } ? ? ? ? ? ? }, (err) => { ? ? ? ? ? ? ? wx.hideLoading(); ? ? ? ? ? ? ? Toast('加載失敗'); ? ? ? ? ? ? ? console.log(err); ? ? ? ? ? ? }) ? ? ? ? ? } ? ? ? ? }, ? ? ? }) ? ? } else if (that.data.name == "kuanApplyof") { ? ? ? console.log("受領(lǐng)人簽字"); ? ? ? wx.canvasToTempFilePath({ ? ? ? ? canvasId: 'myCanvas', ? ? ? ? success: function (res) { ? ? ? ? ? if (that.startY == 0) { ? ? ? ? ? ? wx.showToast({ ? ? ? ? ? ? ? icon: 'none', ? ? ? ? ? ? ? title: '您還沒有簽名', ? ? ? ? ? ? }) ? ? ? ? ? } else { ? ? ? ? ? ? //整理簽名格式 ? ? ? ? ? ? that.setData({ ? ? ? ? ? ? ? //轉(zhuǎn)base64 ? ? ? ? ? ? ? base64: wx.getFileSystemManager().readFileSync(res.tempFilePath, "base64"), ? ? ? ? ? ? }) ? ? ? ? ? ? console.log("base64"); ? ? ? ? ? ? console.log(that.data.base64); ? ? ? ? ? ? //請求接口 提交信息 ? ? ? ? ? ? // list_ = JSON.parse(list_); ? ? ? ? ? ? console.log(that.data.list_); ? ? ? ? ? ? let url = '/accept/apply?drawId=' + that.data.drawId_ + '&userId=' + that.data.userID + '&signature=' + `${encodeURIComponent(that.data.base64)}`; ? ? ? ? ? ? let data = { ? ? ? ? ? ? ? account: that.data.list_.account, //開戶名 ? ? ? ? ? ? ? mobile: that.data.list_.mobile, //手機號 ? ? ? ? ? ? ? cardNo: that.data.list_.cardNo, //身份證號 ? ? ? ? ? ? ? bankName: that.data.list_.bankName, //開戶行 ? ? ? ? ? ? ? bankNo: that.data.list_.bankNo, //卡號 ? ? ? ? ? ? ? remarks: that.data.list_.remarks, //備注信息 ? ? ? ? ? ? ? applyMaterialList: that.data.list_.applyMaterialList, //申請材料信息 ? ? ? ? ? ? } ? ? ? ? ? ? console.log(data); ? ? ? ? ? ? wx.showLoading({ ? ? ? ? ? ? ? title: '確認中', ? ? ? ? ? ? }) ? ? ? ? ? ? app.wxRequest('POST', 2, url, data, (res) => { ? ? ? ? ? ? ? console.log(res); ? ? ? ? ? ? ? if (res.code == 200) { ? ? ? ? ? ? ? ? wx.hideLoading(); ? ? ? ? ? ? ? ? Toast('確認提存信息成功'); ? ? ? ? ? ? ? ? var name_accept = null; ? ? ? ? ? ? ? ? for (var i = 0; i < that.data.list_.accept_list.length; i++) { ? ? ? ? ? ? ? ? ? if (app.globalData.user_phone == that.data.list_.accept_list[i].mobile) { ? ? ? ? ? ? ? ? ? ? name_accept = that.data.list_.accept_list[i].name; ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? let sendMobile = app.globalData.sendMobile; //公證 ? ? ? ? ? ? ? ? if (that.data.list_ != null && name_accept != null) { ? ? ? ? ? ? ? ? ? if (app.globalData.note_bool) { ? ? ? ? ? ? ? ? ? ? if (sendMobile != '') { ? ? ? ? ? ? ? ? ? ? ? that.call_function(7, that.data.list_.issuer_list.realName, name_accept, sendMobile); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? setTimeout(() => { ? ? ? ? ? ? ? ? ? wx.switchTab({ ? ? ? ? ? ? ? ? ? ? url: '/pages/user/perceptio/index', ? ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? ? ? }, 500) ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? wx.hideLoading(); ? ? ? ? ? ? ? ? Toast('確認提存信息失敗'); ? ? ? ? ? ? ? } ? ? ? ? ? ? }, (err) => { ? ? ? ? ? ? ? wx.hideLoading(); ? ? ? ? ? ? ? Toast('加載失敗'); ? ? ? ? ? ? ? console.log(err); ? ? ? ? ? ? }) ? ? ? ? ? } ? ? ? ? }, ? ? ? }) ? ? ? } else { ? ? ? Toast('提存信息發(fā)布失敗'); ? ? } ? }, ? ? clearDraw() { ? ? console.log("清除"); ? ? this.startY = 0 ? ? this.context.draw() ? ? this.context.setLineWidth(4); ? ? this.context.setLineCap('round'); ? ? this.context.setLineJoin('round'); ? }
樣式(css)
/* miniprogram/pages/common/drawing/index.wxss */ ? .contentBox { ? width: 100%; ? background:#4f58a8; } ? .title{ ? font-size: 30rpx; ? color:#fff; ? font-weight: 600; ? text-indent: 20rpx; ? padding:30rpx 0; } ? .main { ? padding-top: 40rpx; ? width: 100%; ? margin: 0 auto; ? background:#ffffff; ? border-radius: 30rpx 30rpx 0 0; } .warningTitle{ ? width: 90%; ? margin: 30rpx auto 0; ? font-weight: 600; ? line-height: 60rpx; ? font-size:40rpx; } .txtWarning { ? line-height: 20px; ? width: 90%; ? margin: 0 auto 90rpx; } ? .canvasBox { ? height: 400rpx; ? width: 100%; ? margin: 30rpx auto; } .canvasTitle{ ? height: 65rpx; ? width: 90%; ? margin: 0 auto; } .canvasContent { ? height: 400rpx; ? border-bottom: 4rpx solid #cdcdcd; ? background:#f5f5f5; ? position: relative; } .singatureTag{ ? position: absolute; ? top: 0rpx; ? left: 0rpx; ? font-size: 160rpx; ? color: #e6e6e6; ? width: 100%; ? text-align: center; ? letter-spacing: 8rpx; ? line-height: 400rpx; } .btnBox { ? width: 100%; ? margin-top: 30rpx; ? display: flex; ? justify-content: space-between; } ? .next { ? width: 100%; ? padding: 20rpx 0; ? background: #ffffff; ? border-top: 1rpx solid #ccc; ? position: fixed; ? left: 0; ? bottom: 0; ? z-index: 9; } .next-list { ? width: 50%; ? text-align: center; ? display: inline-block; ? vertical-align: middle; } /* .next-list { ? width: 30%; } */
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript 對Cookie 操作的封裝小結(jié)
通過本篇,您能了解到: 匿名函數(shù) 閉包的產(chǎn)生 JavaScript實現(xiàn)private 以及 public 訪問權(quán)限 document.cookie 的操作2009-12-12firefox和IE系列的相關(guān)區(qū)別整理 以備后用
firefox和IE系列的相關(guān)區(qū)別整理,整理相對來說還可以,但對于個別細節(jié)的處理不夠完善。具體的可以參考腳本*之家以前發(fā)布的文章。2009-12-12JavaScript圣杯布局與雙飛翼布局實現(xiàn)案例詳解
這篇文章主要介紹了JavaScript圣杯布局與雙飛翼布局實現(xiàn)案例,這是前端面試中需要掌握的知識點,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08javascript與jquery中的this關(guān)鍵字用法實例分析
這篇文章主要介紹了javascript與jquery中的this關(guān)鍵字用法,結(jié)合實例形式簡單分析了this關(guān)鍵字用于獲取當前對象的使用技巧,非常簡單易懂,需要的朋友可以參考下2015-12-12