欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2020年01月06日 08:27:48   作者:我的小熊不見(jiàn)了  
這篇文章主要介紹了使用vue實(shí)現(xiàn)一個(gè)電子簽名組件的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

在生活中我們使用到電子簽名最多的地方可能就是銀行了,每次都會(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è)必要的步驟:

  1. 獲取canvas元素
  2. 通過(guò)canvas元素創(chuàng)建context對(duì)象
  3. 通過(guò)context對(duì)象來(lái)繪制圖形

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

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

獲取畫(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • typescript+vite項(xiàng)目配置別名的方法實(shí)現(xiàn)

    typescript+vite項(xiàng)目配置別名的方法實(shí)現(xiàn)

    我們?yōu)榱耸÷匀唛L(zhǎng)的路徑,經(jīng)常喜歡配置路徑別名,本文主要介紹了typescript+vite項(xiàng)目配置別名的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • electron打包vue項(xiàng)目的方法 步驟

    electron打包vue項(xiàng)目的方法 步驟

    本文主要介紹了electron打包vue項(xiàng)目,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • el-select自定義指令實(shí)現(xiàn)觸底加載分頁(yè)請(qǐng)求options數(shù)據(jù)(完整代碼和接口可直接用)

    el-select自定義指令實(shí)現(xiàn)觸底加載分頁(yè)請(qǐng)求options數(shù)據(jù)(完整代碼和接口可直接用)

    某些情況下,下拉框需要做觸底加載,發(fā)請(qǐng)求,獲取option的數(shù)據(jù),下面給大家分享el-select自定義指令實(shí)現(xiàn)觸底加載分頁(yè)請(qǐng)求options數(shù)據(jù)(附上完整代碼和接口可直接用),感興趣的朋友參考下吧
    2024-02-02
  • vue從使用到源碼實(shí)現(xiàn)教程詳解

    vue從使用到源碼實(shí)現(xiàn)教程詳解

    這篇文章主要介紹了vue從使用到源碼實(shí)現(xiàn)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-09-09
  • Vue一個(gè)動(dòng)態(tài)添加background-image的實(shí)現(xiàn)

    Vue一個(gè)動(dòng)態(tài)添加background-image的實(shí)現(xiàn)

    這篇文章主要介紹了Vue一個(gè)動(dòng)態(tài)添加background-image的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • SSM VUE Axios詳解

    SSM VUE Axios詳解

    Axios是在前端開(kāi)發(fā)中常用的一個(gè)發(fā)送 HTTP 請(qǐng)求的庫(kù),用過(guò)的都知道,篇文章主要給大家介紹了關(guān)于vue的相關(guān)資料,需要的朋友可以參考下
    2021-10-10
  • vue之this.$router.push頁(yè)面刷新問(wèn)題

    vue之this.$router.push頁(yè)面刷新問(wèn)題

    這篇文章主要介紹了vue之this.$router.push頁(yè)面刷新問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • vue+elementUI組件table實(shí)現(xiàn)前端分頁(yè)功能

    vue+elementUI組件table實(shí)現(xiàn)前端分頁(yè)功能

    這篇文章主要為大家詳細(xì)介紹了vue+elementUI組件table實(shí)現(xiàn)前端分頁(yè)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • 一起來(lái)做一下Vue全局提示組件

    一起來(lái)做一下Vue全局提示組件

    前端開(kāi)發(fā)的時(shí)候,在項(xiàng)目中引入組件以及使用是非常常見(jiàn)操作,下面這篇文章主要給大家介紹了關(guān)于Vue全局提示組件的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06
  • vue異步組件與組件懶加載問(wèn)題(import不能導(dǎo)入變量字符串路徑)

    vue異步組件與組件懶加載問(wèn)題(import不能導(dǎo)入變量字符串路徑)

    這篇文章主要介紹了vue異步組件與組件懶加載問(wèn)題(import不能導(dǎo)入變量字符串路徑),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04

最新評(píng)論