1. 程式人生 > 實用技巧 >移動端簽名確定不瞭解一下?

移動端簽名確定不瞭解一下?

原理:signature_pad外掛,canvas

<template>
  <div id="app">
    <button @click="handleClick">點選簽名</button>
    <img :src="imgUrl" v-if="showImg" style="border:1px dashed #ccc;"/>
    <div class="sign-box" v-show="showSignature">
      <div class="bgc"></div>
      <div class="main-box">
        <span @click="cancelBgc" class="cancel">取消</span>
        <p class="title">簽名</p>
        <p class="sub-title">請用正楷簽下您的名字</p>
        <div :style="{width:w,height:h}" class="canvass">
          <canvas :id="uid" class="canvas" :data-uid="uid"></canvas>
        </div>
        <div id='reset' @click="clear">
          <span>清除</span>
        </div>
        <div @click="saveSign" class="next">確認</div>
      </div>
    </div>
  </div>
</template>

<script>
  /*
eslint-disable */ import SignaturePad from 'signature_pad'; export default { data() { return { showSignature: false, showImg:false, sig: () => { }, uid: 'canvas', option: { backgroundColor: 'rgb(255,255,255)', penColor: 'rgb(0, 0, 0)', minWidth:
0.8, maxWidth: 4 }, w: '94.6%', h: "240px", } }, methods: { handleClick() { this.showSignature = true; this.$nextTick(() => { this.draw(); }) }, // 簽名 draw() { var that = this; var
canvas = document.getElementById(that.uid); that.sig = new SignaturePad(canvas, that.option); window.addEventListener('resize', that.resizeCanvas); that.resizeCanvas(); }, resizeCanvas() { var that = this var canvas = document.getElementById(that.uid); var url; var ratio = Math.max(window.devicePixelRatio || 1, 1); canvas.width = canvas.offsetWidth * ratio; canvas.height = canvas.offsetHeight * ratio; canvas.getContext('2d').scale(ratio, ratio); that.clear() !that.clearOnResize && url !== undefined && that.fromDataURL(url); }, fromDataURL(url) { var that = this; that.sig.fromDataURL(url); }, // 取消簽名 cancelBgc() { this.showSignature = false; }, // 清除簽名 clear() { var that = this; that.sig.clear(); }, isEmpty() { var that = this; return that.sig.isEmpty(); }, // 儲存簽名 saveSign() { var that = this; if (that.isEmpty()) { alert('請用正楷簽下您的名字'); return } var sign = { signature: that.sig.toDataURL().split(',')[1] } that.imgUrl = "data:image/png;base64," + sign.signature; that.showImg = true; that.showSignature = false; } } } /* eslint-disable */ </script> <style> #app { font-family: Avenir, Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; color: #2c3e50; margin-top: 60px; } .canvass { margin: auto; background: #fff; border: 1px dashed #e5e5e5; border-radius: 8px; margin-top: 17px; position: relative; } #reset { text-align: center; font-size: 14px; color: #666666; margin-top: 26px; margin-bottom: 47px; } #reset img { width: 13px; height: 13px; } canvas { width: 100%; height: 100%; } .sign-box { position: fixed; top: 0; left: 0; width: 100%; min-height: 100vh; } .bgc { opacity: 0.7; background: #000000; width: 100%; height: 100%; position: absolute; top: 0; z-index: 5; } .main-box { height: 500px; width: 100%; background-color: #fff; position: absolute; bottom: 0; z-index: 10; } .cancel { position: absolute; font-size: 18px; color: #31A4FF; left: 15px; top: 16px; } .title { text-align: center; font-size: 18px; color: #333333; padding-top: 16px; padding-bottom: 4px; } .sub-title { font-size: 12px; color: #999999; text-align: center; } .next { border: 1px solid #31A4FF; height: 44px; text-align: center; line-height: 44px; font-size: 18px; color: #31A4FF; background: #fff; margin: 0 15px; } .footer { position: fixed; left: 0; bottom: 0; display: flex; justify-content: space-between; width: 100%; } .pre { width: 28%; margin-right: 1px; border-top: 1px solid #d6d6d6; color: #454545; background: #fff; } .share { width: 44%; border-top: 1px solid #d6d6d6; background: #66b9ff; color: #fff; } .nextOne { width: 28%; color: #fff; background: #31A4FF; } .commoncss { height: calc(110rem / 24); line-height: calc(110rem / 24); font-size: 1.6rem; text-align: center; box-sizing: border-box; margin-top: 1rem; } .hasLinkpre { margin-top: 1rem; background: #fff; width: 50%; height: calc(110rem / 24); line-height: calc(110rem / 24); font-size: 1.6rem; text-align: center; box-sizing: border-box; color: #454545; } .hasLinknextone { margin-top: 1rem; background: #31A4FF; width: 50%; height: calc(110rem / 24); line-height: calc(110rem / 24); font-size: 1.6rem; text-align: center; box-sizing: border-box; color: #fff; } </style>

效果圖: