欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

JavaScript

當(dāng)前位置:主頁 > 網(wǎng)絡(luò)編程 > JavaScript >

使用vue實(shí)現(xiàn)一個(gè)電子簽名組件的示例代碼

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:JavaScript|點(diǎn)擊: 次

在生活中我們使用到電子簽名最多的地方可能就是銀行了,每次都會讓你留下大名。今天我們就要用vue實(shí)現(xiàn)一個(gè)電子簽名的面板

想要繪制圖形,第一步想到的就是使用canvas標(biāo)簽,在之前的文章里我們使用canvas實(shí)現(xiàn)了一個(gè)前端生成圖形驗(yàn)證碼的組件,被吐槽不夠安全,那么這個(gè)電子簽名組件想必不會被吐槽了吧~

canvas

<canvas> 標(biāo)簽是 HTML 5 中的新標(biāo)簽。
<canvas> 標(biāo)簽只是圖形容器,您必須使用腳本來繪制圖形。

canvas標(biāo)簽本身是沒有繪圖能力的,所有的繪制工作必須在 JavaScript 內(nèi)部完成。

使用canvas繪圖有幾個(gè)必要的步驟:

  1. 獲取canvas元素
  2. 通過canvas元素創(chuàng)建context對象
  3. 通過context對象來繪制圖形

在當(dāng)前電子簽名需求中,由于簽名其實(shí)是由一條條線組成的,因此我們會用到以下幾個(gè)方法:

  1. beginPath() :開始一條路徑或重置當(dāng)前的路徑
  2. moveTo():把路徑移動到畫布中的指定點(diǎn),不創(chuàng)建線條
  3. lineTo():添加一個(gè)新點(diǎn),然后在畫布中創(chuàng)建從該點(diǎn)到最后指定點(diǎn)的線條
  4. stroke():繪制已定義的路徑
  5. 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>

獲取畫筆

在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();//開始作畫
   this.points.push(obj);//記錄點(diǎn)
   this.isDown = true;
  },

touchStart

//移動設(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è)畫板signatureBox剩余的高,this.$refs.canvasHW.offsetHeight*0.1是畫板中標(biāo)題的高
    this.startX = obj.x;
    this.startY = obj.y;
    this.canvasTxt.beginPath();//開始作畫
    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);//移動畫筆
    this.canvasTxt.lineTo(obj.x, obj.y);//創(chuàng)建線條
    this.canvasTxt.stroke();//畫線
    this.startY = obj.y;
    this.startX = obj.x;
    this.points.push(obj);//記錄點(diǎn)
   }
  },

touchMove

//移動設(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);//移動畫筆
    this.canvasTxt.lineTo(obj.x, obj.y);//創(chuàng)建線條
    this.canvasTxt.stroke();//畫線
    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

//移動設(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)
   }
  },

重寫

發(fā)現(xiàn)自己寫錯(cuò)字了,擦掉畫板重新寫過

//重寫
  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)記
  };
 },

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

上一篇:Vue學(xué)習(xí)之a(chǎn)xios的使用方法實(shí)例分析

欄    目:JavaScript

下一篇:nodeJs的安裝與npm全局環(huán)境變量的配置詳解

本文標(biāo)題:使用vue實(shí)現(xiàn)一個(gè)電子簽名組件的示例代碼

本文地址:http://mengdiqiu.com.cn/a1/JavaScript/9291.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有