知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X(jué)表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷(xiāo)的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷(xiāo)工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
您當(dāng)前位置>首頁(yè) » 新聞資訊 » 小程序相關(guān) >
小程序?qū)崿F(xiàn)手寫(xiě)簽名
發(fā)表時(shí)間:2022-7-26
發(fā)布人:葵宇科技
瀏覽次數(shù):65
在微信小程序上實(shí)現(xiàn)手寫(xiě)簽名,獲取canvascontext新版本和舊版本有點(diǎn)坑,新版本在獲取canvas后如果頁(yè)面有滑動(dòng),則簽名坐標(biāo)出現(xiàn)異常(在微信開(kāi)發(fā)者工具上會(huì)出現(xiàn)2022-2-17),但是在真機(jī)上即使滑動(dòng)也不會(huì)出現(xiàn)異常,為了防止出現(xiàn)問(wèn)題,暫時(shí)使用舊版本獲取canvascontext
1.效果圖
2.相關(guān)代碼
1.canvas代碼
- 新版2d canvas
<canvas id="canvas" class="canvas" canvas-id="canvas" type="2d" :disable-scroll="true" @touchstart="handleTouchStart" @touchmove="handleTouchMove" @touchend="handleTouchEnd" @touchcancel="handleTouchCancel" ></canvas>
- 舊版canvas
<canvas class="canvas" canvas-id="canvas" :disable-scroll="true" @touchstart="handleTouchStart" @touchmove="handleTouchMove" @touchend="handleTouchEnd" @touchcancel="handleTouchCancel" ></canvas>
2.js相關(guān)
- 獲取新版2d canvas對(duì)象
const query = uni.createSelectorQuery().in(this); query.select('.canvas').node(res => { const { _width, _height } = res.node; /* 獲取canvas wxml節(jié)點(diǎn) */ this.canvas = res.node; this.canvasWidth = _width; this.canvasHeight = _height; /* 獲取canvas 2dcontext */ this.canvasContext= this.canvas.getContext('2d'); /* 縮放設(shè)置canvas畫(huà)布大小,防止筆跡錯(cuò)位 */ const ratio = wx.getSystemInfoSync().pixelRatio; this.canvas.width = this.canvasWidth * ratio; this.canvas.height = this.canvasHeight * ratio; this.canvasContext.scale(ratio, ratio); /* 設(shè)置線條顏色 */ this.canvasContext.strokeStyle = '#2A2A2A'; /* 設(shè)置線條粗細(xì) */ this.canvasContext.lineWidth = 4; /* 設(shè)置線條的結(jié)束端點(diǎn)樣式 */ this.canvasContext.lineCap = 'round'; }).exec()
- 縮放設(shè)置canvas畫(huà)布大小,防止筆跡錯(cuò)位,這點(diǎn)和頁(yè)面滑動(dòng)沒(méi)有關(guān)系,不設(shè)置也會(huì)導(dǎo)致坐標(biāo)錯(cuò)位
const ratio = wx.getSystemInfoSync().pixelRatio; this.canvas.width = this.canvasWidth * ratio; this.canvas.height = this.canvasHeight * ratio; this.canvasContext.scale(ratio, ratio);
舊版本獲取canvas
this.canvasContext = uni.createCanvasContext('canvas', this); /* 設(shè)置線條顏色 */ this.canvasContext.setStrokeStyle('#2A2A2A'); /* 設(shè)置線條粗細(xì) */ this.canvasContext.setLineWidth(4); /* 設(shè)置線條的結(jié)束端點(diǎn)樣式 */ this.canvasContext.setLineCap('round');
簽名js方法,新版本和舊版本只有一個(gè)draw的區(qū)別,新版本不需要使用draw方法
/* 觸摸開(kāi)始 */ handleTouchStart(e) { this.drawStartX = e.changedTouches[0].x; this.drawStartY = e.changedTouches[0].y; this.canvasContext.beginPath(); }, /* 觸摸移動(dòng) */ handleTouchMove(e) { /* 記錄當(dāng)前位置 */ const tempX = e.changedTouches[0].x; const tempY = e.changedTouches[0].y; /* 畫(huà)線 */ this.canvasContext.moveTo(this.drawStartX, this.drawStartY); this.canvasContext.lineTo(tempX, tempY); this.canvasContext.stroke(); /* 舊版draw方法,新版本不需要draw */ this.canvasContext.draw(true); /* 重新記錄起始位置 */ this.drawStartX = tempX; this.drawStartY = tempY; }, /* 觸摸結(jié)束 */ handleTouchEnd(e) { this.canvasContext.save(); }, /* 觸摸取消 */ handleTouchCancel(e) { this.canvasContext.save(); }, /* 清空畫(huà)布 */ clearCanvas() { this.canvasContext.clearRect(0, 0, this.canvasWidth, this.canvasHeight); },
canvas生成本地圖片(我這里封裝了組件,需要傳入this防止this指向異常)
/* 生成簽名圖片 */ generateSignImage() { return new Promise((resolve, reject) => { uni.canvasToTempFilePath({ x: 0, y: 0, // canvas: this.canvas, // 新版 canvasId: 'canvas', // 舊版使用id width: this.canvasWidth, height: this.canvasHeight, destWidth: this.canvasWidth, destHeight: this.canvasHeight, fileType: 'png', quality: 1, success: res => { resolve(res.tempFilePath) }, fail: err => { reject(err); } }, this) }) },
新版本的canvas主要是canvas wxml節(jié)點(diǎn)和canvas context中做了區(qū)分,舊版則只有一個(gè)canvas context就可以做全部的操作,在生成圖片時(shí),新版本是傳入wxml對(duì)象,舊版本則是傳入唯一canvasId,新版本canvas取消了draw方法
正在努力學(xué)習(xí)中,若對(duì)你的學(xué)習(xí)有幫助,留下你的印記唄(點(diǎn)個(gè)贊咯^_^)
相關(guān)案例查看更多
相關(guān)閱讀
- 霸屏推廣
- 大理小程序開(kāi)發(fā)
- 云南小程序開(kāi)發(fā)首選品牌
- 保險(xiǎn)網(wǎng)站建設(shè)公司
- 軟件定制公司
- 云南衛(wèi)視小程序
- 云南企業(yè)網(wǎng)站
- 云南網(wǎng)站建設(shè)方法
- 云南etc小程序
- 昆明小程序定制開(kāi)發(fā)
- 昆明小程序公司
- 開(kāi)通微信小程序被騙
- 云南網(wǎng)絡(luò)營(yíng)銷(xiāo)顧問(wèn)
- 小程序開(kāi)發(fā)公司
- 小程序開(kāi)發(fā)
- 報(bào)廢車(chē)拆解回收管理系統(tǒng)
- 專(zhuān)業(yè)網(wǎng)站建設(shè)公司
- 云南網(wǎng)站建設(shè)制作
- 網(wǎng)絡(luò)公司哪家好
- 汽車(chē)報(bào)廢
- 云南小程序開(kāi)發(fā)公司推薦
- 云南網(wǎng)站建設(shè)服務(wù)
- 云南小程序公司
- .net網(wǎng)站
- 開(kāi)發(fā)微信小程序
- 網(wǎng)站上首頁(yè)
- 云南小程序開(kāi)發(fā)費(fèi)用
- 云南網(wǎng)站建設(shè)百度官方
- SEO
- APP