在生活中咱們使用到電子簽名最多的地方可能就是銀行了,每次都會讓你留下大名。今天咱們就要用vue實現一個電子簽名的面板
想要繪製圖形,第一步想到的就是使用canvas
標籤,在以前的文章裏咱們使用canvas
實現了一個前端生成圖形驗證碼的組件,被吐槽不夠安全,那麼這個電子簽名組件想必不會被吐槽了吧~javascript
<canvas> 標籤是 HTML 5 中的新標籤。 <canvas> 標籤只是圖形容器,您必須使用腳原本繪製圖形。
canvas
標籤自己是沒有繪圖能力的,全部的繪製工做必須在 JavaScript 內部完成。html
使用canvas
繪圖有幾個必要的步驟:前端
在當前電子簽名需求中,因爲簽名實際上是由一條條線組成的,所以咱們會用到如下幾個方法:vue
想要在canvas
中繪圖,還須要綁定幾個特定的事件,而這些事件在pc端和手機端不盡相同java
canvas
標籤並綁定事件<canvas @touchstart="touchStart" @touchmove="touchMove" @touchend="touchEnd" ref="canvasF" @mousedown="mouseDown" @mousemove="mouseMove" @mouseup="mouseUp" ></canvas>
在mounted
生命週期初始化canvas
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(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();//開始做畫 this.points.push(obj);//記錄點 this.isDown = true; },
//移動設備事件 touchStart(ev) { ev = ev || event; ev.preventDefault(); if (ev.touches.length == 1) { this.isDraw = true; //簽名標記 let obj = { x: ev.targetTouches[0].clientX, y: ev.targetTouches[0].clientY - (document.body.offsetHeight * 0.5 + this.$refs.canvasHW.offsetHeight * 0.1) }; //y的計算值中:document.body.offsetHeight*0.5表明的是除了整個畫板signatureBox剩餘的高,this.$refs.canvasHW.offsetHeight*0.1是畫板中標題的高 this.startX = obj.x; this.startY = obj.y; this.canvasTxt.beginPath();//開始做畫 this.points.push(obj);//記錄點 } },
//電腦設備事件 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);//移動畫筆 this.canvasTxt.lineTo(obj.x, obj.y);//建立線條 this.canvasTxt.stroke();//畫線 this.startY = obj.y; this.startX = obj.x; this.points.push(obj);//記錄點 } },
//移動設備事件 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);//移動畫筆 this.canvasTxt.lineTo(obj.x, obj.y);//建立線條 this.canvasTxt.stroke();//畫線 this.startY = obj.y; this.startX = obj.x; this.points.push(obj);//記錄點 } },
//電腦設備事件 mouseUp(ev) { ev = ev || event; ev.preventDefault(); if (1) { let obj = { x: ev.offsetX, y: ev.offsetY }; this.canvasTxt.closePath();//收筆 this.points.push(obj);//記錄點 this.points.push({ x: -1, y: -1 }); this.isDown = false; } },
//移動設備事件 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);//記錄點 this.points.push({ x: -1, y: -1 });//記錄點 } },
發現本身寫錯字了,擦掉畫板從新寫過segmentfault
//重寫 overwrite() { this.canvasTxt.clearRect( 0, 0, this.$refs.canvasF.width, this.$refs.canvasF.height ); this.points = []; this.isDraw = false; //簽名標記 },
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 //簽名標記 }; },
轉評贊就是最大的鼓勵安全