微信小程序?qū)崿F(xiàn)手寫簽名的示例代碼
在微信小程序上實(shí)現(xiàn)手寫簽名,獲取canvascontext新版本和舊版本有點(diǎn)坑,新版本在獲取canvas后如果頁面有滑動,則簽名坐標(biāo)出現(xiàn)異常(在微信開發(fā)者工具上會出現(xiàn)2022-2-17),但是在真機(jī)上即使滑動也不會出現(xiàn)異常,為了防止出現(xiàn)問題,暫時(shí)使用舊版本獲取canvascontext
1.效果圖


2.相關(guān)代碼
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>
js相關(guān)
獲取新版2d canvas對象
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畫布大小,防止筆跡錯(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畫布大小,防止筆跡錯(cuò)位,這點(diǎn)和頁面滑動沒有關(guān)系,不設(shè)置也會導(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方法
/* 觸摸開始 */
handleTouchStart(e) {
this.drawStartX = e.changedTouches[0].x;
this.drawStartY = e.changedTouches[0].y;
this.canvasContext.beginPath();
},
/* 觸摸移動 */
handleTouchMove(e) {
/* 記錄當(dāng)前位置 */
const tempX = e.changedTouches[0].x;
const tempY = e.changedTouches[0].y;
/* 畫線 */
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();
},
/* 清空畫布 */
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對象,舊版本則是傳入唯一canvasId,新版本canvas取消了draw方法
以上就是微信小程序?qū)崿F(xiàn)手寫簽名的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于小程序手寫簽名的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
echarts中tooltip添加點(diǎn)擊事件代碼示例
這篇文章主要給大家介紹了關(guān)于echarts中tooltip添加點(diǎn)擊事件的相關(guān)資料,echarts tooltip點(diǎn)擊事件是指當(dāng)用戶點(diǎn)擊圖表中的提示框(tooltip)時(shí)觸發(fā)的事件,需要的朋友可以參考下2023-07-07
bootstrap實(shí)現(xiàn)動態(tài)進(jìn)度條效果
本篇文章主要介紹了bootstrap實(shí)現(xiàn)動態(tài)進(jìn)度條效果,進(jìn)度條可以加強(qiáng)應(yīng)用的用戶體驗(yàn)效果,看到數(shù)字,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-03-03
JavaScript創(chuàng)建對象的四種常用模式實(shí)例分析
這篇文章主要介紹了JavaScript創(chuàng)建對象的四種常用模式,結(jié)合實(shí)例形式分析了javascript使用工廠模式、構(gòu)造函數(shù)模式、原型模式及動態(tài)原型模式創(chuàng)建對象的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-01-01
JavaScript輸入框字?jǐn)?shù)實(shí)時(shí)統(tǒng)計(jì)更新
這篇文章主要介紹了JavaScript輸入框字?jǐn)?shù)實(shí)時(shí)統(tǒng)計(jì)更新,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06

