日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

【微信小程序canvas】实现小程序手写板用户签名(附代码)

發(fā)布時(shí)間:2025/5/22 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【微信小程序canvas】实现小程序手写板用户签名(附代码) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

【微信小程序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的距離來(lái)計(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ù)制代碼

筆跡移動(dòng)

// 筆跡移動(dòng)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}//測(cè)試裁剪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é)語(yǔ)

詳細(xì)項(xiàng)目代碼handwriting-weapp(微信小程序原生canvas用戶簽字手寫板,后續(xù)更新計(jì)劃組件化、優(yōu)化渲染邏輯、增加功能,歡迎start 和 PR)

總結(jié)

以上是生活随笔為你收集整理的【微信小程序canvas】实现小程序手写板用户签名(附代码)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。