美文网首页
vue 签名

vue 签名

作者: 羊羊羊0703 | 来源:发表于2019-06-27 15:11 被阅读0次
<template>
  <div ref="signaturePanel" class="signaturePanel">
    <div  class="title" ref="title"  @click="clearAll">请在下方区域签字</div>
    <canvas ref="drawingBoard" class="drawingBoard" @mousedown="mousedown" @mousemove="mousemove" @mouseup="mouseup" v-show="!mySignature"></canvas>
    <img class="drawingBoard" :src="mySignature" v-show="mySignature"/>
    <div class="clear" @click="clearAll"><img src="../img/clear.png"/>{{mySignature ? '重新签名':'清除'}}</div>
    <!-- <div class="nextBtn validate" @click="clearAll">提交</div> -->
    <!-- <input type="file" ref="textDom"/> -->
  </div>
</template>
<script>
import {uploadShareImages} from '../api/api'
export default {
  props: {
    signature: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      canvas: '',
      ctx: '',
      painting: false,
      lastPoint: {
        x: undefined,
        y: undefined
      },
      lineWidth: 2, // 画笔宽度
      color: '#000', // 画笔颜色
      mySignature: this.signature
      // width: 0, // 画布宽度
      // height: 0 // 画布高度
    }
  },
  watch: {
    signature(val) {
      this.mySignature = val
    }
  },
  mounted () {
    tuhu.store.commit('shopJoin/setPageIndex', 9)
    this.init()
    $(document).off('click', 'a') // 恢复a标签的默认事件
  },
  created() {
  },
  methods: {
    onPageBack() {
      // 监听页面返回
      tuhu.store.commit('shopJoin/setPageIndex', 9)
    },
    init () {
      this.ctx = this.$refs.drawingBoard.getContext('2d')
      this.$refs.drawingBoard.width = this.$refs.drawingBoard.offsetWidth
      this.$refs.drawingBoard.height = this.$refs.drawingBoard.offsetHeight
      this.ctx.strokeStyle = this.color
      // 在canvas绘制前填充白色背景,不然保存的png图片是透明的
      this.ctx.fillStyle = '#fff'
      this.ctx.fillRect(0, 0, this.$refs.drawingBoard.offsetWidth, this.$refs.drawingBoard.offsetHeight)

      const _this = this
      // 兼容移动端
      if (document.body && document.body.ontouchstart !== undefined) {
        _this.$refs.drawingBoard.ontouchstart = function (e) {
          _this.mousedown(e, 0)
          e.preventDefault()
        }
        _this.$refs.drawingBoard.ontouchmove = function (e) {
          _this.mousemove(e, 0)
        }
        _this.$refs.drawingBoard.ontouchend = function (e) {
          _this.mouseup(e, 0)
        }
      }
    },
    mousedown (e, flag = 1) {
      this.painting = true
      this.lastPoint = this.getPoint(e, flag)
    },
    mousemove (e, flag = 1) {
      if (!this.painting) {
        return
      }
      let newPoint = this.getPoint(e, flag)
      this.drawLine(this.lastPoint.x, this.lastPoint.y, newPoint.x, newPoint.y)
      this.lastPoint = newPoint
    },
    getPoint(e, flag = 1) {
      let x
      let y
      try {
        let clientY = flag ? e.clientY : e.touches[0].clientY
        x = flag ? e.offsetX : e.touches[0].clientX
        y = clientY - this.$refs.title.offsetHeight - this.$refs.signaturePanel.offsetTop + this.$refs.signaturePanel.offsetParent.scrollTop
      } catch (error) {
        tuhu.alert(error)
      }

      return {x: x, y: y}
    },
    mouseup () {
      this.painting = false
    },
    drawLine (x1, y1, x2, y2) {
      this.ctx.lineWidth = this.lineWidth
      this.ctx.lineCap = 'round'
      this.ctx.lineJoin = 'round'
      this.ctx.moveTo(x1, y1)
      this.ctx.lineTo(x2, y2)
      this.ctx.stroke()
      this.ctx.closePath()
    },
    clearAll () {
      if (this.mySignature) {
        this.mySignature = ''
        this.$nextTick(() => {
          this.init()
        })
      } else {
          // this.ctx.height = this.$refs.drawingBoard.offsetHeight
        this.ctx.clearRect(0, 0, this.$refs.drawingBoard.offsetWidth, this.$refs.drawingBoard.offsetHeight)
        this.ctx.beginPath()   // 开启新路径,防止连笔
          // 清除以后要重新绘制背景图
        this.ctx.fillStyle = '#fff'
        this.ctx.fillRect(0, 0, this.$refs.drawingBoard.offsetWidth, this.$refs.drawingBoard.offsetHeight)
      }
    },
    convertBase64UrlToBlob(urlData) {
      let bytes = window.atob(urlData.split(',')[1])        // 去掉url的头,并转换为byte
        // 处理异常,将ascii码小于0的转换为大于0
      let ab = new ArrayBuffer(bytes.length)
      let ia = new Uint8Array(ab)
      for (let i = 0; i < bytes.length; i++) {
        ia[i] = bytes.charCodeAt(i)
      }
      return new Blob([ab], {type: 'image/png'})
    },
    save () { // 当前项目阻止了a标签的默认事件
      if (this.mySignature) {
        return this.mySignature
      }
      let imgUrl = ''
      try {
        imgUrl = this.$refs.drawingBoard.toDataURL('image/png')
      } catch (error) {
        tuhu.alert(error)
        tuhu.alert('请清除签名后,重新签名')
      }
      let formData = new FormData()
      // console.log(this.$refs.textDom.files[0])
      // formData.append('img', this.$refs.textDom.files[0])
      formData.append('imageName', this.convertBase64UrlToBlob(imgUrl))
      return uploadShareImages(formData)
        .then((res) => {
          tuhu.hidePreloader()
          return res && res.filename
        }).catch((e) => {
          tuhu.hidePreloader()
          tuhu.toast('签名上传失败,请重新上传')
          return ''
        })
    }
  }
}
</script>
<style lang="less" scoped>
  .signaturePanel {
    // overflow: hidden;
    font-size: 14px;
    background: #fff;
    padding-bottom: 2.25rem;
    position: relative;
    &::after{
      width: 100%;
      content: '签字区';
      position: absolute;
      top: 50% - 1rem;
      left: 0;
      font-size: 3rem;
      text-align: center;
      transform: translateY(-50%);
      color: #F5F5F5;
      z-index: -1;
    }
    .title{
      height: 2rem;
      padding: .6rem 0 .3rem .75rem;
      color: #999;
      font-weight: 400;
    }
    .drawingBoard {
      width: 100%;
      height: 12rem;
      border: 1px solid #eee;
    }
    .clear {
      height: 1.4rem;
      line-height: 1.4rem;
      color: #666;
      background: #F5F5F5;
      border-radius: .7rem;
      padding: 0 .5rem;
      position: absolute;
      bottom: .7rem;
      right: .7rem;
      z-index: 99;
      text-align: center;
      vertical-align: top;
      img{
        width: .7rem;
        height: .7rem;
        margin-right: .1rem;
        transform: translateY(.1rem)
      }
    }
    // .save {
    //   background-color: #f00;
    //   position: absolute;
    //   top: 0;
    //   right: 0;
    //   z-index: 1;
    // }
  }
</style>

相关文章

  • vue 签名

  • tool.js

    vue时间格式过滤器(今天,昨天,周几,年月日) vue金额过滤器 手机横屏签名功能(vue)

  • Vue开发新手遇坑

    Vue开发新手遇坑总结自己开发vue遇到的坑。 a里面不会传href解决:href签名加个v-bind: vue打...

  • 【第15期】vue: 电子签名

    原创文章,转载请注明原文章地址,谢谢! 这里演示如何在webapp中签名。 Autograph.vue电子签名 2...

  • vue 电子签名

    最近公司要求用h5实现电子签名,在网上搜索后,发现一章文章很实用,用vue,canvas实现。 在移动端上需要在获...

  • vue电子签名

    最近公司要求用h5实现电子签名,在网上搜索后,发现一章文章很实用,用vue,canvas实现。在移动端上需要在获取...

  • vue 电子签名

    上来就干货! autograph.vue 有空再整理成组件,拜拜!

  • VUE调用微信分享接口的坑

    1. vue项目调用微信接口一直报错invalid signature签名错误 如果签名验证正确的话,请检查ur...

  • vue项目实现STS临时授权实现上传视频到OSS

    使用JavaScript签名后直传 vue安装ali-oss: npm install ali-oss --sav...

  • VUE----电子签名

    我使用的是vue-cli2 1.下载插件: 2.全局引入: 3.示例代码: 4.文件格式: 默认canvas会生成...

网友评论

      本文标题:vue 签名

      本文链接:https://www.haomeiwen.com/subject/jzrrcctx.html