【第15期】vue: 電子簽名

原創(chuàng)文章,轉(zhuǎn)載請注明原文章地址逮诲,謝謝幽告!

這里演示如何在webapp中簽名。

  1. Autograph.vue電子簽名
<template>
  <div class="hello">
    <canvas id ="canvas" style="background-color: #fff" @touchmove.prevent>Canvas畫板</canvas>
    <div class="btnBox">
      <button @click="clear">清除</button>
      <button @click="save">確定</button>
    </div>
  </div>
</template>
<script>
  import {mapState} from 'vuex'
  var draw
  var isDraw = 0
  var preHandler = function (e) {
    e.preventDefault()
  }
class Draw {
  constructor (el) {
    this.el = el
    this.canvas = document.getElementById('canvas')
    this.cxt = this.canvas.getContext('2d')
    this.stage_info = this.canvas.getBoundingClientRect()
    this.path = {
      beginX: 0,
      beginY: 0,
      endX: 0,
      endY: 0
    }
    isDraw = 0
    this.canvas.width = document.body.clientWidth
    this.canvas.height = document.body.clientHeight
    alert('請用真實(shí)姓名簽名')
  }
  init (btn) {
    var that = this
    this.canvas.addEventListener('touchstart', function (event) {
      document.addEventListener('touchstart', preHandler, false)
      that.drawBegin(event)
    })
    this.canvas.addEventListener('touchend', function (event) {
      document.addEventListener('touchend', preHandler, false)
      that.drawEnd()
    })
    this.clear(btn)
  }
  drawBegin (e) {
    var that = this
    window.getSelection() ? window.getSelection().removeAllRanges() : document.selection.empty()
    this.cxt.fillStyle = '#FFF'
    this.cxt.strokeStyle = '#000'
    this.cxt.beginPath()
    this.cxt.moveTo(
      e.changedTouches[0].clientX - this.stage_info.left,
      e.changedTouches[0].clientY - this.stage_info.top
    )
    this.path.beginX = e.changedTouches[0].clientX - this.stage_info.left
    this.path.beginY = e.changedTouches[0].clientY - this.stage_info.top
    this.canvas.addEventListener('touchmove', function () {
      that.drawing(event)
    })
  }
  drawing (e) {
    this.cxt.lineTo(
      e.changedTouches[0].clientX - this.stage_info.left,
      e.changedTouches[0].clientY - this.stage_info.top
    )
    this.path.endX = e.changedTouches[0].clientX - this.stage_info.left
    this.path.endY = e.changedTouches[0].clientY - this.stage_info.top
    this.cxt.stroke()
    isDraw = isDraw + 1
  }
  drawEnd () {
    document.removeEventListener('touchstart', preHandler, false)
    document.removeEventListener('touchend', preHandler, false)
    document.removeEventListener('touchmove', preHandler, false)
  }
  clear (btn) {
    this.cxt.clearRect(0, 0, this.canvas.width, this.canvas.height)
  }
  save () {
    let imgBase64 = this.canvas.toDataURL()
    return imgBase64
  }
}
?
export default {
  data () {
    return {
      msg: '',
      val: true,
      url: ''
    }
  },
  mounted () {
    draw = new Draw('canvas')
    draw.init()
  },
  computed: {
    ...mapState(['autographButton', 'autographImg'])
  },
  methods: {
    clear: function () {
      draw.clear()
      isDraw = 0
    },
    save: function () {
      if (isDraw > 50) {
        let data = draw.save()
        this.url = data
        this.$router.go(-1)
        let value = 'true'
        sessionStorage.setItem('true', value)
        sessionStorage.setItem('autoimg', data)
        this.$store.state.autographButton = '簽名完成'
        this.$store.state.autographImg = '簽名完成'
        console.log(this.$store.state.autographButton + '-----' + '簽名已完成')
      } else {
        alert('請簽名后在提交')
      }
    }
  }
}
</script>
?
<style scoped>
#canvas {
  cursor: default;
}
.btnBox{
    width: 100%;
    height: 120px;
    background: #EEEEEE;
    position: fixed;
    bottom: 0;
  }
  .btnBox button:first-of-type{
    border: none;
    background: transparent;
    border-radius: 4px;
    padding: 5px 10px;
    font-size: 32px;
    outline: none;
    color: #F00000;
    float: left;
    margin-left: 98px;
    line-height: 120px;
    transform: rotate(90deg);
  }
  .btnBox button:last-of-type{
    border: none;
    background: transparent;
    border-radius: 4px;
    padding: 5px 10px;
    font-size: 32px;
    outline: none;
    color: #1CAC3E;
    float: right;
    margin-right: 98px;
    line-height: 120px;
    transform: rotate(90deg);
  }
</style>
?

2. 簽名演示

image
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末冻河,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子芋绸,更是在濱河造成了極大的恐慌,老刑警劉巖摔敛,帶你破解...
    沈念sama閱讀 222,807評論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件马昙,死亡現(xiàn)場離奇詭異桃犬,居然都是意外死亡行楞,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,284評論 3 399
  • 文/潘曉璐 我一進(jìn)店門形用,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人田度,你說我怎么就攤上這事≌蚪龋” “怎么了?”我有些...
    開封第一講書人閱讀 169,589評論 0 363
  • 文/不壞的土叔 我叫張陵惋啃,是天一觀的道長监右。 經(jīng)常有香客問我肥橙,道長秸侣,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,188評論 1 300
  • 正文 為了忘掉前任椭坚,我火速辦了婚禮搏色,結(jié)果婚禮上善茎,老公的妹妹穿的比我還像新娘频轿。我一直安慰自己,他們只是感情好航邢,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,185評論 6 398
  • 文/花漫 我一把揭開白布膳殷。 她就那樣靜靜地躺著操骡,像睡著了一般赚窃。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上勒极,一...
    開封第一講書人閱讀 52,785評論 1 314
  • 那天辱匿,我揣著相機(jī)與錄音键痛,去河邊找鬼。 笑死散休,一個胖子當(dāng)著我的面吹牛乐尊,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播扔嵌,決...
    沈念sama閱讀 41,220評論 3 423
  • 文/蒼蘭香墨 我猛地睜開眼痢缎,長吁一口氣:“原來是場噩夢啊……” “哼胁勺!你這毒婦竟也來了独旷?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,167評論 0 277
  • 序言:老撾萬榮一對情侶失蹤案疲,失蹤者是張志新(化名)和其女友劉穎麻养,沒想到半個月后褐啡,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體鳖昌,經(jīng)...
    沈念sama閱讀 46,698評論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,767評論 3 343
  • 正文 我和宋清朗相戀三年懂盐,在試婚紗的時候發(fā)現(xiàn)自己被綠了糕档。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片允粤。...
    茶點(diǎn)故事閱讀 40,912評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡类垫,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出悉患,到底是詐尸還是另有隱情,我是刑警寧澤售躁,帶...
    沈念sama閱讀 36,572評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站陪捷,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏市袖。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,254評論 3 336
  • 文/蒙蒙 一酒觅、第九天 我趴在偏房一處隱蔽的房頂上張望微峰。 院中可真熱鬧舷丹,春花似錦蜓肆、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,746評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽穴豫。三九已至,卻和暖如春精肃,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背司抱。 一陣腳步聲響...
    開封第一講書人閱讀 33,859評論 1 274
  • 我被黑心中介騙來泰國打工黎烈, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人照棋。 一個月前我還...
    沈念sama閱讀 49,359評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像烈炭,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子符隙,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,922評論 2 361

推薦閱讀更多精彩內(nèi)容