微信小程序?qū)崿F(xiàn)簡(jiǎn)單手寫簽名組件的方法實(shí)例
背景:
在做項(xiàng)目過程中,需要在微信小程序中實(shí)現(xiàn)手寫簽名組件。在網(wǎng)上找了微信小程序手寫簽名實(shí)現(xiàn),但是都是不太理想。在實(shí)際運(yùn)用中,會(huì)因?yàn)閷?shí)時(shí)計(jì)算較多的貝塞爾曲線而產(chǎn)生卡頓。效果不理想。所以退一步,不需要筆鋒以及筆跡模擬效果。只需要簡(jiǎn)單的手寫簽名實(shí)現(xiàn)。
需求:
可以實(shí)現(xiàn)用戶在微信小程序上手寫簽名。
需要組件化。
效果

一、思路
在微信小程序中,我們使用canvas組件實(shí)現(xiàn)。將用戶的輸入想象成為一只筆。我們要畫的簽名是由很多點(diǎn)構(gòu)成的。但是單純的點(diǎn)是不能很好地構(gòu)成線。點(diǎn)與點(diǎn)之間還要由線連接。下面是實(shí)現(xiàn)過程代碼。
二、實(shí)現(xiàn)
1. 頁面與樣式
wxml
這里的canvas組件是最新的用法。
<view class="dashbox">
<view class="btnList">
<van-button size="small" bind:click="clearCanvas">清空</van-button>
</view>
<view class="handCenter">
<canvas
class="handWriting"
disable-scroll="{{true}}"
id="handWriting"
bindtouchstart="scaleStart"
bindtouchmove="scaleMove"
bindtouchend="scaleEnd"
bindtap="mouseDown"
type="2d"
>
</canvas>
</view>
</view>
wxss
.btnList{
width: 95%;
margin:0 auto;
}
.handWriting{
background: #fff;
width: 95%;
height: 80vh;
margin:0 auto
}
2. 初始化
由于是在自定義組件中使用,所以要注意獲取canvas的時(shí)候的this指向問題。如果不調(diào)用SelectorQuery的In方法,那么就在自定義組件獲取不到canvas,因?yàn)檫@個(gè)時(shí)候指向的父組件。
Component({
/**
* 組件的初始數(shù)據(jù)
*/
data: {
canvasName:'#handWriting',
ctx:'',
canvasWidth:0,
canvasHeight:0,
startPoint:{
x:0,
y:0,
},
selectColor: 'black',
lineColor: '#1A1A1A', // 顏色
lineSize: 1.5, // 筆記倍數(shù)
radius:5,//畫圓的半徑
},
ready(){
let canvasName = this.data.canvasName;
let query = wx.createSelectorQuery().in(this);//獲取自定義組件的SelectQuery對(duì)象
query.select(canvasName)
.fields({ node: true, size: true })
.exec((res) => {
const canvas = res[0].node;
const ctx = canvas.getContext('2d');
//獲取設(shè)備像素比
const dpr = wx.getSystemInfoSync().pixelRatio;
//縮放設(shè)置canvas畫布大小,防止筆跡錯(cuò)位
canvas.width = res[0].width * dpr;
canvas.height = res[0].height * dpr;
ctx.scale(dpr, dpr);
ctx.lineJoin="round";
this.setData({ctx});
});
query.select('.handCenter').boundingClientRect(rect => {
console.log('rect', rect);
this.setData({
canvasWidth:rect.width,
canvasHeight:rect.height
});
}).exec();
},
//省略以下代碼......
});
3. 點(diǎn)擊時(shí)
Component({
//省略以上代碼...
methods: {
scaleStart(event){
if (event.type != 'touchstart') return false;
let currentPoint = {
x: event.touches[0].x,
y: event.touches[0].y
}
this.drawCircle(currentPoint);
this.setData({startPoint:currentPoint});
},
drawCircle(point){//這里負(fù)責(zé)點(diǎn)
let ctx = this.data.ctx;
ctx.beginPath();
ctx.fillStyle = this.data.lineColor;
//筆跡粗細(xì)由圓的大小決定
ctx.arc(point.x, point.y, this.data.radius, 0 , 2 * Math.PI);
ctx.fill();
ctx.closePath();
},
//省略以下代碼...
}
})
4. 簽名時(shí)
Component({
//省略以上代碼
methods:{
drawLine(sourcePoint, targetPoint){
let ctx = this.data.ctx;
this.drawCircle(targetPoint);
ctx.beginPath();
ctx.strokeStyle = this.data.lineColor;
ctx.lineWidth = this.data.radius * 2;//這里乘2是因?yàn)榫€條的粗細(xì)要和圓的直徑相等
ctx.moveTo(sourcePoint.x, sourcePoint.y);
ctx.lineTo(targetPoint.x, targetPoint.y);
ctx.stroke();
ctx.closePath();
},
clearCanvas(){//清空畫布
let ctx = this.data.ctx;
ctx.rect(0, 0, this.data.canvasWidth, this.data.canvasHeight);
ctx.fillStyle = '#FFFFFF';
ctx.fill();
}
}
})
三、總結(jié)
這個(gè)手寫簽名僅僅是為了業(yè)務(wù)應(yīng)急使用。如果要優(yōu)化的話,可以從筆鋒模擬和筆跡模擬中入手。只不過要解決在實(shí)時(shí)模擬過程中卡頓的問題。
到此這篇關(guān)于微信小程序?qū)崿F(xiàn)簡(jiǎn)單手寫簽名組件的文章就介紹到這了,更多相關(guān)微信小程序手寫簽名組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用ionic在首頁新聞中應(yīng)用到的跑馬燈效果的實(shí)現(xiàn)方法
在app中經(jīng)常會(huì)有滾動(dòng)的跑馬燈效果的運(yùn)用,接下來通過本文給大家介紹使用ionic在首頁新聞中應(yīng)用到的跑馬燈效果的實(shí)現(xiàn)方法,需要的的朋友參考下2017-02-02
微信小程序圖片選擇區(qū)域裁剪實(shí)現(xiàn)方法
本篇文章主要介紹了微信小程序圖片選擇區(qū)域屏裁剪實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12
JS在onclientclick里如何控制onclick的執(zhí)行
這篇文章主要介紹了JS在onclientclick里如何控制onclick的執(zhí)行的相關(guān)資料,需要的朋友可以參考下2016-05-05
JS 動(dòng)態(tài)加載js文件和css文件 同步/異步的兩種簡(jiǎn)單方式
下面小編就為大家?guī)硪黄狫S 動(dòng)態(tài)加載js文件和css文件 同步/異步的兩種簡(jiǎn)單方式。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09
JavaScript數(shù)據(jù)類型的存儲(chǔ)方法詳解
JavaScript中基本數(shù)據(jù)類型和引用數(shù)據(jù)類型是如何存儲(chǔ)的呢?下面通過本文給大家分享js數(shù)據(jù)類型的存儲(chǔ)方法,需要的朋友參考下吧2017-08-08

