使用vue實(shí)現(xiàn)一個(gè)電子簽名組件的示例代碼
在生活中我們使用到電子簽名最多的地方可能就是銀行了,每次都會(huì)讓你留下大名。今天我們就要用vue實(shí)現(xiàn)一個(gè)電子簽名的面板
想要繪制圖形,第一步想到的就是使用canvas標(biāo)簽,在之前的文章里我們使用canvas實(shí)現(xiàn)了一個(gè)前端生成圖形驗(yàn)證碼的組件,被吐槽不夠安全,那么這個(gè)電子簽名組件想必不會(huì)被吐槽了吧~
canvas
<canvas> 標(biāo)簽是 HTML 5 中的新標(biāo)簽。
<canvas> 標(biāo)簽只是圖形容器,您必須使用腳本來(lái)繪制圖形。
canvas標(biāo)簽本身是沒(méi)有繪圖能力的,所有的繪制工作必須在 JavaScript 內(nèi)部完成。
使用canvas繪圖有幾個(gè)必要的步驟:
- 獲取canvas元素
- 通過(guò)canvas元素創(chuàng)建context對(duì)象
- 通過(guò)context對(duì)象來(lái)繪制圖形
在當(dāng)前電子簽名需求中,由于簽名其實(shí)是由一條條線組成的,因此我們會(huì)用到以下幾個(gè)方法:
- beginPath() :開(kāi)始一條路徑或重置當(dāng)前的路徑
- moveTo():把路徑移動(dòng)到畫(huà)布中的指定點(diǎn),不創(chuàng)建線條
- lineTo():添加一個(gè)新點(diǎn),然后在畫(huà)布中創(chuàng)建從該點(diǎn)到最后指定點(diǎn)的線條
- stroke():繪制已定義的路徑
- closePath():創(chuàng)建從當(dāng)前點(diǎn)回到起始點(diǎn)的路徑
事件
想要在canvas中繪圖,還需要綁定幾個(gè)特定的事件,而這些事件在pc端和手機(jī)端不盡相同
pc端事件
- mousedown
- mousemove
- mouseup
手機(jī)端事件
- touchstart
- touchmove
- touchend
核心代碼
初始化canvas標(biāo)簽并綁定事件
<canvas
@touchstart="touchStart"
@touchmove="touchMove"
@touchend="touchEnd"
ref="canvasF"
@mousedown="mouseDown"
@mousemove="mouseMove"
@mouseup="mouseUp"
></canvas>
獲取畫(huà)筆
在mounted生命周期初始化
mounted() {
let canvas = this.$refs.canvasF;
canvas.height = this.$refs.canvasHW.offsetHeight - 100;
canvas.width = this.$refs.canvasHW.offsetWidth - 10;
this.canvasTxt = canvas.getContext("2d");
this.canvasTxt.strokeStyle = this.color;
this.canvasTxt.lineWidth = this.linewidth;
}
事件處理
mouseDown
//電腦設(shè)備事件
mouseDown(ev) {
ev = ev || event;
ev.preventDefault();
let obj = {
x: ev.offsetX,
y: ev.offsetY
};
this.startX = obj.x;
this.startY = obj.y;
this.canvasTxt.beginPath();//開(kāi)始作畫(huà)
this.points.push(obj);//記錄點(diǎn)
this.isDown = true;
},
touchStart
//移動(dòng)設(shè)備事件
touchStart(ev) {
ev = ev || event;
ev.preventDefault();
if (ev.touches.length == 1) {
this.isDraw = true; //簽名標(biāo)記
let obj = {
x: ev.targetTouches[0].clientX,
y:
ev.targetTouches[0].clientY -
(document.body.offsetHeight * 0.5 +
this.$refs.canvasHW.offsetHeight * 0.1)
}; //y的計(jì)算值中:document.body.offsetHeight*0.5代表的是除了整個(gè)畫(huà)板signatureBox剩余的高,this.$refs.canvasHW.offsetHeight*0.1是畫(huà)板中標(biāo)題的高
this.startX = obj.x;
this.startY = obj.y;
this.canvasTxt.beginPath();//開(kāi)始作畫(huà)
this.points.push(obj);//記錄點(diǎn)
}
},
mouseMove
//電腦設(shè)備事件
mouseMove(ev) {
ev = ev || event;
ev.preventDefault();
if (this.isDown) {
let obj = {
x: ev.offsetX,
y: ev.offsetY
};
this.moveY = obj.y;
this.moveX = obj.x;
this.canvasTxt.moveTo(this.startX, this.startY);//移動(dòng)畫(huà)筆
this.canvasTxt.lineTo(obj.x, obj.y);//創(chuàng)建線條
this.canvasTxt.stroke();//畫(huà)線
this.startY = obj.y;
this.startX = obj.x;
this.points.push(obj);//記錄點(diǎn)
}
},
touchMove
//移動(dòng)設(shè)備事件
touchMove(ev) {
ev = ev || event;
ev.preventDefault();
if (ev.touches.length == 1) {
let obj = {
x: ev.targetTouches[0].clientX,
y:
ev.targetTouches[0].clientY -
(document.body.offsetHeight * 0.5 +
this.$refs.canvasHW.offsetHeight * 0.1)
};
this.moveY = obj.y;
this.moveX = obj.x;
this.canvasTxt.moveTo(this.startX, this.startY);//移動(dòng)畫(huà)筆
this.canvasTxt.lineTo(obj.x, obj.y);//創(chuàng)建線條
this.canvasTxt.stroke();//畫(huà)線
this.startY = obj.y;
this.startX = obj.x;
this.points.push(obj);//記錄點(diǎn)
}
},
mouseUp
//電腦設(shè)備事件
mouseUp(ev) {
ev = ev || event;
ev.preventDefault();
if (1) {
let obj = {
x: ev.offsetX,
y: ev.offsetY
};
this.canvasTxt.closePath();//收筆
this.points.push(obj);//記錄點(diǎn)
this.points.push({ x: -1, y: -1 });
this.isDown = false;
}
},
touchEnd
//移動(dòng)設(shè)備事件
touchEnd(ev) {
ev = ev || event;
ev.preventDefault();
if (ev.touches.length == 1) {
let obj = {
x: ev.targetTouches[0].clientX,
y:
ev.targetTouches[0].clientY -
(document.body.offsetHeight * 0.5 +
this.$refs.canvasHW.offsetHeight * 0.1)
};
this.canvasTxt.closePath();//收筆
this.points.push(obj);//記錄點(diǎn)
this.points.push({ x: -1, y: -1 });//記錄點(diǎn)
}
},
重寫(xiě)
發(fā)現(xiàn)自己寫(xiě)錯(cuò)字了,擦掉畫(huà)板重新寫(xiě)過(guò)
//重寫(xiě)
overwrite() {
this.canvasTxt.clearRect(
0,
0,
this.$refs.canvasF.width,
this.$refs.canvasF.height
);
this.points = [];
this.isDraw = false; //簽名標(biāo)記
},
用到的data
data() {
return {
points: [],
canvasTxt: null,
startX: 0,
startY: 0,
moveY: 0,
moveX: 0,
endY: 0,
endX: 0,
w: null,
h: null,
isDown: false,
color: "#000",
linewidth: 3,
isDraw: false //簽名標(biāo)記
};
},
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:Vue學(xué)習(xí)之a(chǎn)xios的使用方法實(shí)例分析
欄 目:JavaScript
下一篇:nodeJs的安裝與npm全局環(huán)境變量的配置詳解
本文標(biāo)題:使用vue實(shí)現(xiàn)一個(gè)電子簽名組件的示例代碼
本文地址:http://www.jygsgssxh.com/a1/JavaScript/9291.html
您可能感興趣的文章
- 04-02包含javascript舍的詞條
- 04-02java后端代碼分頁(yè) java后端實(shí)現(xiàn)分頁(yè)page
- 01-10使用webpack/gulp構(gòu)建TypeScript項(xiàng)目的方法示例
- 01-10Echarts實(shí)現(xiàn)單條折線可拖拽效果
- 01-10使用JS來(lái)動(dòng)態(tài)操作css的幾種方法
- 01-10在Vue項(xiàng)目中使用Typescript的實(shí)現(xiàn)
- 01-10js實(shí)現(xiàn)上傳圖片并顯示圖片名稱(chēng)
- 01-10Vue中使用Lodop插件實(shí)現(xiàn)打印功能的簡(jiǎn)單方法
- 01-10echarts實(shí)現(xiàn)折線圖的拖拽效果
- 01-10d3.js實(shí)現(xiàn)圖形縮放平移


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02javascript點(diǎn)線,點(diǎn)線的代碼
- 04-02javascript潛力,javascript強(qiáng)大嗎
- 04-02javascript替換字符串,js字符串的替換
- 04-02javascript移出,js 移入移出
- 04-02包含javascript舍的詞條
- 04-02javascript并行,深入理解并行編程 豆瓣
- 04-02javascript匿名,js匿名方法
- 04-02javascript警報(bào),JavaScript警告
- 04-02javascript遮蓋,JavaScript遮蓋PC端頁(yè)面
- 04-02javascript前身,javascript的前身
隨機(jī)閱讀
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10delphi制作wav文件的方法
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10C#中split用法實(shí)例總結(jié)


