【微信小程序canvas】实现小程序手写板用户签名(附代码)
生活随笔
收集整理的這篇文章主要介紹了
【微信小程序canvas】实现小程序手写板用户签名(附代码)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
【微信小程序canvas】實(shí)現(xiàn)小程序手寫板用戶簽名(附代碼)
工作中公司業(yè)務(wù)需要的微信小程序用戶簽字功能
先看效果圖:
wxml
<view class="wrapper"><view class="handBtn"><button catchtap="retDraw" class="delBtn">重寫</button><button catchtap="subCanvas" class="subBtn">完成</button> </view><view class="handCenter"><canvas class="handWriting" disable-scroll="true" bindtouchstart="uploadScaleStart" bindtouchmove="uploadScaleMove"bindtouchend="uploadScaleEnd" bindtap="mouseDown" canvas-id="handWriting"></canvas></view><view class="handRight"><view class="handTitle">手寫板</view></view> </view> 復(fù)制代碼js
data
page({data: {canvasName: 'handWriting',ctx: '',canvasWidth: 0,canvasHeight: 0,transparent: 1, // 透明度selectColor: 'black',lineColor: '#1A1A1A', // 顏色lineSize: 1.5, // 筆記倍數(shù)lineMin: 0.5, // 最小筆畫半徑lineMax: 4, // 最大筆畫半徑pressure: 1, // 默認(rèn)壓力smoothness: 60, //順滑度,用60的距離來計(jì)算速度currentPoint: {},currentLine: [], // 當(dāng)前線條firstTouch: true, // 第一次觸發(fā)radius: 1, //畫圓的半徑cutArea: { top: 0, right: 0, bottom: 0, left: 0 }, //裁剪區(qū)域bethelPoint: [], //保存所有線條 生成的貝塞爾點(diǎn);lastPoint: 0,chirography: [], //筆跡currentChirography: {}, //當(dāng)前筆跡linePrack: [], //劃線軌跡 , 生成線條的實(shí)際點(diǎn)} }) 復(fù)制代碼初始化
page({onLoad () {let canvasName = this.data.canvasNamelet ctx = wx.createCanvasContext(canvasName)this.setData({ctx: ctx})var query = wx.createSelectorQuery();query.select('.handCenter').boundingClientRect(rect => {query.select('.handCenter').boundingClientRect(rect => {this.setData({this.setData({canvasWidth: rect.width,canvasWidth: rect.width,canvasHeight: rect.heightcanvasHeight: rect.height})})}).exec(); }).exec();}, }, }) 復(fù)制代碼事件函數(shù)
筆跡開始
// 筆跡開始uploadScaleStart (e) {if (e.type != 'touchstart') return false;let ctx = this.data.ctx;ctx.setFillStyle(this.data.lineColor); // 初始線條設(shè)置顏色ctx.setGlobalAlpha(this.data.transparent); // 設(shè)置半透明let currentPoint = {x: e.touches[0].x,y: e.touches[0].y}let currentLine = this.data.currentLine;currentLine.unshift({time: new Date().getTime(),dis: 0,x: currentPoint.x,y: currentPoint.y})this.setData({currentPoint,// currentLine})if (this.data.firstTouch) {this.setData({cutArea: { top: currentPoint.y, right: currentPoint.x, bottom: currentPoint.y, left: currentPoint.x },firstTouch: false})}this.pointToLine(currentLine);}, 復(fù)制代碼筆跡移動
// 筆跡移動uploadScaleMove (e) {if (e.type != 'touchmove') return false;if (e.cancelable) {// 判斷默認(rèn)行為是否已經(jīng)被禁用if (!e.defaultPrevented) {e.preventDefault();}}let point = {x: e.touches[0].x,y: e.touches[0].y}//測試裁剪if (point.y < this.data.cutArea.top) {this.data.cutArea.top = point.y;}if (point.y < 0) this.data.cutArea.top = 0;if (point.x > this.data.cutArea.right) {this.data.cutArea.right = point.x;}if (this.data.canvasWidth - point.x <= 0) {this.data.cutArea.right = this.data.canvasWidth;}if (point.y > this.data.cutArea.bottom) {this.data.cutArea.bottom = point.y;}if (this.data.canvasHeight - point.y <= 0) {this.data.cutArea.bottom = this.data.canvasHeight;}if (point.x < this.data.cutArea.left) {this.data.cutArea.left = point.x;}if (point.x < 0) this.data.cutArea.left = 0;this.setData({lastPoint: this.data.currentPoint,currentPoint: point})let currentLine = this.data.currentLinecurrentLine.unshift({time: new Date().getTime(),dis: this.distance(this.data.currentPoint, this.data.lastPoint),x: point.x,y: point.y})// this.setData({// currentLine// })this.pointToLine(currentLine);}, 復(fù)制代碼筆跡結(jié)束
// 筆跡結(jié)束uploadScaleEnd (e) {if (e.type != 'touchend') return 0;let point = {x: e.changedTouches[0].x,y: e.changedTouches[0].y}this.setData({lastPoint: this.data.currentPoint,currentPoint: point})let currentLine = this.data.currentLinecurrentLine.unshift({time: new Date().getTime(),dis: this.distance(this.data.currentPoint, this.data.lastPoint),x: point.x,y: point.y})// this.setData({// currentLine// })if (currentLine.length > 2) {var info = (currentLine[0].time - currentLine[currentLine.length - 1].time) / currentLine.length;//$("#info").text(info.toFixed(2));}//一筆結(jié)束,保存筆跡的坐標(biāo)點(diǎn),清空,當(dāng)前筆跡//增加判斷是否在手寫區(qū)域;this.pointToLine(currentLine);var currentChirography = {lineSize: this.data.lineSize,lineColor: this.data.lineColor};var chirography = this.data.chirographychirography.unshift(currentChirography);this.setData({chirography})var linePrack = this.data.linePracklinePrack.unshift(this.data.currentLine);this.setData({linePrack,currentLine: []})}, 復(fù)制代碼wxss
page {background: #fbfbfb;height: auto;overflow: hidden; }.wrapper {width: 100%;height: 95vh;margin: 30rpx 0;overflow: hidden;display: flex;align-content: center;flex-direction: row;justify-content: center;font-size: 28rpx; }.handWriting {background: #fff;width: 100%;height: 95vh; }.handRight {display: inline-flex;align-items: center; }.handCenter {border: 4rpx dashed #e9e9e9;flex: 5;overflow: hidden;box-sizing: border-box; }.handTitle {transform: rotate(90deg);flex: 1;color: #666; }.handBtn button {font-size: 28rpx; }.handBtn {height: 95vh;display: inline-flex;flex-direction: column;justify-content: space-between;align-content: space-between;flex: 1; }.delBtn {position: absolute;top: 550rpx;left: 0rpx;transform: rotate(90deg);color: #666; }.delBtn image {position: absolute;top: 13rpx;left: 25rpx; } 復(fù)制代碼結(jié)語
詳細(xì)項(xiàng)目代碼handwriting-weapp(微信小程序原生canvas用戶簽字手寫板,后續(xù)更新計(jì)劃組件化、優(yōu)化渲染邏輯、增加功能,歡迎start 和 PR)
總結(jié)
以上是生活随笔為你收集整理的【微信小程序canvas】实现小程序手写板用户签名(附代码)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构与算法基础-试题
- 下一篇: BIM技术在地铁中应用