手机签名(Vue)

<template>
  <div class="signatureBox">
    <x-header
      slot="header"
      :title="$t('lang.app.my_signature')">
    </x-header>

    <div class="canvasBox" ref="canvasHW">
      <canvas @touchstart='touchStart'
              @touchmove='touchMove'
              @touchend='touchEnd'
              @mousedown="mouseDown"
              @mousemove="mouseMove"
              @mouseup="mouseUp"
              ref="canvasF">
      </canvas>

      <div class="btnBox">
        <button @click="overwrite">{{$t('lang.app.clear')}}</button>
        <button @click="commit">{{$t('lang.app.commit')}}</button>
      </div>

    </div>
  </div>
</template>

<script>
import XHeader from 'vux/src/components/x-header/index'
export default {
  name: 'signature',
  components: {XHeader},
  data () {
    return {
      points: [],
      startX: 0,
      startY: 0,
      moveY: 0,
      moveX: 0,
      endY: 0,
      endX: 0,

      w: null,
      h: null,
      imgData: '',
      isDown: false,
      canvasBoard: null,
      canvasContext: null
    }
  },

  mounted () {
    this.canvasBoard = this.$refs.canvasF
    this.canvasBoard.height = this.$refs.canvasHW.offsetHeight - 70
    this.canvasBoard.width = this.$refs.canvasHW.offsetWidth - 20
    this.canvasContext = this.canvasBoard.getContext('2d')
  },

  methods: {
    // Computer event -- Mouse down
    mouseDown (ev) {
      ev = ev || event
      ev.preventDefault()
      console.log(ev)

      let obj = {
        x: ev.offsetX,
        y: ev.offsetY
      }

      console.log(obj)
      this.startX = obj.x
      this.startY = obj.y
      this.canvasContext.beginPath()
      this.canvasContext.moveTo(this.startX, this.startY)
      this.canvasContext.lineTo(obj.x, obj.y)
      this.canvasContext.stroke()
      this.canvasContext.closePath()
      this.points.push(obj)
      this.isDown = true
    },

    // Mobile event -- Touch start
    touchStart (ev) {
      ev = ev || event
      ev.preventDefault()
      if (ev.touches.length === 1) {
        let obj = {
          x: ev.targetTouches[0].clientX,
          y: ev.targetTouches[0].clientY - 48
        }

        this.startX = obj.x
        this.startY = obj.y
        this.canvasContext.beginPath()
        this.canvasContext.moveTo(this.startX, this.startY)
        this.canvasContext.lineTo(obj.x, obj.y)
        this.canvasContext.stroke()
        this.canvasContext.closePath()
        this.points.push(obj)
      }
    },

    // Mobile -- Mouse move
    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.canvasContext.beginPath()
        this.canvasContext.moveTo(this.startX, this.startY)
        this.canvasContext.lineTo(obj.x, obj.y)
        this.canvasContext.stroke()
        this.canvasContext.closePath()
        this.startY = obj.y
        this.startX = obj.x
        this.points.push(obj)
      }
    },

    // Mobile event -- Touch move
    touchMove (ev) {
      ev = ev || event
      ev.preventDefault()
      if (ev.touches.length === 1) {
        let obj = {
          x: ev.targetTouches[0].clientX,
          y: ev.targetTouches[0].clientY - 48
        }

        this.moveY = obj.y
        this.moveX = obj.x
        this.canvasContext.beginPath()
        this.canvasContext.moveTo(this.startX, this.startY)
        this.canvasContext.lineTo(obj.x, obj.y)
        this.canvasContext.stroke()
        this.canvasContext.closePath()
        this.startY = obj.y
        this.startX = obj.x
        this.points.push(obj)
      }
    },

    // Computer event -- Mouse up
    mouseUp (ev) {
      ev = ev || event
      ev.preventDefault()

      let obj = {
        x: ev.offsetX,
        y: ev.offsetY
      }

      this.canvasContext.beginPath()
      this.canvasContext.moveTo(this.startX, this.startY)
      this.canvasContext.lineTo(obj.x, obj.y)
      this.canvasContext.stroke()
      this.canvasContext.closePath()
      this.points.push(obj)
      this.points.push({x: -1, y: -1})
      this.isDown = false
    },

    // Mobile event TouchEnd
    touchEnd (ev) {
      ev = ev || event
      ev.preventDefault()
      console.log(ev)
      if (ev.touches.length === 1) {
        let obj = {
          x: ev.targetTouches[0].clientX,
          y: ev.targetTouches[0].clientY - 48
        }

        this.canvasContext.beginPath()
        this.canvasContext.moveTo(this.startX, this.startY)
        this.canvasContext.lineTo(obj.x, obj.y)
        this.canvasContext.stroke()
        this.canvasContext.closePath()
        this.points.push(obj)
        this.points.push({x: -1, y: -1})
      }
    },

    // Over write
    overwrite () {
      this.canvasContext.clearRect(0, 0, this.$refs.canvasF.width, this.$refs.canvasF.height)
      this.points = []
    },

    // Commit
    commit () {
      // this.$refs.mySignature.src = this.$refs.canvasF.toDataURL('image/png')
      this.$store.state.currentSignatureData = this.$refs.canvasF.toDataURL('image/png')
      this.$router.back()
    }
  }
}
</script>

<style scoped>
  .signatureBox{
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    overflow: hidden;
    background: #fff;
    z-index: 100;
    display: flex;
    flex-direction: column;
  }

  .visaDetailTop p{
    margin: 0;
    text-align: center;
    color: #000;
    font-size: 1em;
    position: relative;
  }

  .visaDetailTop p span{
    color: #fff;
    font-size: 1.2em;
  }

  .visaDetailTop p:first-of-type{
    float: left;
  }

  .visaDetailTop p:nth-of-type(2){
    float: right;
  }

  .canvasBox{
    padding: 10px 10px;
    box-sizing: border-box;
    flex: 1;
  }

  canvas{
    border-radius: 5px;
    border: 1px solid lightgray;
  }

  .btnBox{
    height: 30px;
    padding: 5px;
    text-align: right;
    line-height: 30px;
  }

  .btnBox button:first-of-type{
    border: 1px solid lightgray;
    background: transparent;
    border-radius: 4px;
    padding: 5px 10px;
    width: 60px;
    outline:none;
  }

  .btnBox button:last-of-type{
    border: 1px solid lightgray;
    background: #0DBFA5;
    color: #fff;
    border-radius: 4px;
    padding: 5px 10px;
    width: 60px;
    outline:none;
  }

  @media only screen and (min-width: 750px){
    .signatureBox{
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      min-height: 100%;
      box-sizing: border-box;
      overflow: visible;
    }
  }
</style>

猜你喜欢

转载自blog.csdn.net/CDUT100/article/details/80891232