微信小程序?qū)崿F(xiàn)電子簽名功能
本文實(shí)例為大家分享了微信小程序?qū)崿F(xiàn)電子簽名的具體代碼,供大家參考,具體內(nèi)容如下
// XXXX.wxml <view class="signName"> <canvas class="canvas" id="canvas" canvas-id="canvas" disable-scroll="true" bindtouchstart="canvasStart" bindtouchmove="canvasMove" bindtouchend="canvasEnd" touchcancel="canvasEnd" binderror="canvasIdErrorCallback"></canvas> </view> <view class="CList flex"> <view class="flex_item marginR20" > <view class="saveBtn" bindtap="uploadImg">上傳簽名</view> </view> <view class="flex_item"> <view class="clearBtn" bindtap="cleardraw">清除簽名</view> </view>
js:
var context = null;// 使用 wx.createContext 獲取繪圖上下文 context var isButtonDown = false;//是否在繪制中 var arrx = [];//動(dòng)作橫坐標(biāo) var arry = [];//動(dòng)作縱坐標(biāo) var arrz = [];//總做狀態(tài),標(biāo)識(shí)按下到抬起的一個(gè)組合 var canvasw = 0;//畫布寬度 var canvash = 0;//畫布高度 Page({ data: { }, onLoad: function () { this.startCanvas(); }, /** * 以下 - 手寫簽名 / 上傳簽名 */ startCanvas: function () {//畫布初始化執(zhí)行 var that = this; //獲取系統(tǒng)信息 wx.getSystemInfo({ success: function (res) { canvasw = res.windowWidth; canvash = res.windowHeight; that.setData({ canvasw: canvasw }); that.setData({ canvash: canvash }); } }); this.initCanvas(); this.cleardraw(); }, //初始化函數(shù) initCanvas: function () { context = wx.createCanvasContext('canvas'); context.beginPath() context.fillStyle = 'rgba(255, 255, 255, 0)'; context.setStrokeStyle('#000000'); context.setLineWidth(4); context.setLineCap('round'); context.setLineJoin('round'); }, canvasStart: function (event) { isButtonDown = true; arrz.push(0); arrx.push(event.changedTouches[0].x); arry.push(event.changedTouches[0].y); }, canvasMove: function (event) { if (isButtonDown) { arrz.push(1); arrx.push(event.changedTouches[0].x); arry.push(event.changedTouches[0].y); } for (var i = 0; i < arrx.length; i++) { if (arrz[i] == 0) { context.moveTo(arrx[i], arry[i]) } else { context.lineTo(arrx[i], arry[i]) } } context.clearRect(0, 0, canvasw, canvash); context.setStrokeStyle('#000000'); context.setLineWidth(4); context.setLineCap('round'); context.setLineJoin('round'); context.stroke(); context.draw(false); }, canvasEnd: function (event) { isButtonDown = false; }, //清除畫布 cleardraw: function () { arrx = []; arry = []; arrz = []; context.clearRect(0, 0, canvasw, canvash); context.draw(true); }, uploadImg(){ var that = this //生成圖片 wx.canvasToTempFilePath({ canvasId: 'canvas', //設(shè)置輸出圖片的寬高 // destWidth:150, // destHeight:150, // fileType:'jpg', quality:1.0, success: function (res) { console.log(res) // canvas圖片地址 res.tempFilePath }, fail: function () { wx.showModal({ title: '提示', content: 'canvas生成圖片失敗。微信當(dāng)前版本不支持,請(qǐng)更新到最新版本!', showCancel: false }); }, complete: function () {} }) }, })
為大家推薦現(xiàn)在關(guān)注度比較高的微信小程序教程一篇:《微信小程序開發(fā)教程》小編為大家精心整理的,希望喜歡。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
分享JavaScript與Java中MD5使用兩個(gè)例子
這篇文章主要為大家分享了JavaScript與Java中MD5使用兩個(gè)例子,2015-12-12ZeroClipboard.js使用一個(gè)flash復(fù)制多個(gè)文本框
這篇文章主要為大家詳細(xì)介紹了ZeroClipboard.js使用一個(gè)flash復(fù)制多個(gè)文本框,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06js實(shí)現(xiàn)一個(gè)逐步遞增的數(shù)字動(dòng)畫
可視化大屏項(xiàng)目使用最多的組件就是數(shù)字組件,本文主要介紹了js實(shí)現(xiàn)一個(gè)逐步遞增的數(shù)字動(dòng)畫,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12javascript中的replace函數(shù)(帶注釋demo)
在js中有兩個(gè)replace函數(shù) 一個(gè)是location.replace(url) 跳轉(zhuǎn)到一個(gè)新的url.一個(gè)string.replace("xx","yy") 替換字符串 返回一個(gè)新的字符串,該方法并不改變字符串本身。下面通過(guò)本文給大家介紹javascript中的replace函數(shù)2018-01-01js遍歷json對(duì)象所有key及根據(jù)動(dòng)態(tài)key獲取值的方法(必看)
下面小編就為大家?guī)?lái)一篇js遍歷json對(duì)象所有key及根據(jù)動(dòng)態(tài)key獲取值的方法(必看)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03鼠標(biāo)拖動(dòng)改變DIV等網(wǎng)頁(yè)元素的大小的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇鼠標(biāo)拖動(dòng)改變DIV等網(wǎng)頁(yè)元素的大小的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07小程序如何寫動(dòng)態(tài)標(biāo)簽的實(shí)現(xiàn)方法
這篇文章主要介紹了小程序如何寫動(dòng)態(tài)標(biāo)簽的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02