当前位置:首页 > VUE

vue实现签名

2026-01-11 21:25:18VUE

实现签名功能的基本思路

在Vue中实现签名功能通常需要结合HTML5的Canvas元素,通过监听用户的鼠标或触摸事件来绘制签名。核心步骤包括创建Canvas画布、监听绘制事件、保存签名图片等。

安装依赖(可选)

如果需要更高级的签名功能,可以使用现成的Vue签名组件库,例如vue-signature-pad

npm install vue-signature-pad

基础Canvas实现方法

<template>
  <div>
    <canvas 
      ref="signatureCanvas"
      @mousedown="startDrawing"
      @mousemove="draw"
      @mouseup="stopDrawing"
      @mouseleave="stopDrawing"
      @touchstart="startDrawing"
      @touchmove="draw"
      @touchend="stopDrawing"
    ></canvas>
    <button @click="clearSignature">清除</button>
    <button @click="saveSignature">保存</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isDrawing: false,
      lastX: 0,
      lastY: 0,
    }
  },
  mounted() {
    this.setupCanvas()
  },
  methods: {
    setupCanvas() {
      const canvas = this.$refs.signatureCanvas
      canvas.width = canvas.offsetWidth
      canvas.height = canvas.offsetHeight
      this.ctx = canvas.getContext('2d')
      this.ctx.strokeStyle = '#000'
      this.ctx.lineWidth = 2
      this.ctx.lineCap = 'round'
    },
    startDrawing(e) {
      this.isDrawing = true
      const pos = this.getPosition(e)
      this.lastX = pos.x
      this.lastY = pos.y
    },
    draw(e) {
      if (!this.isDrawing) return
      const pos = this.getPosition(e)
      this.ctx.beginPath()
      this.ctx.moveTo(this.lastX, this.lastY)
      this.ctx.lineTo(pos.x, pos.y)
      this.ctx.stroke()
      this.lastX = pos.x
      this.lastY = pos.y
    },
    stopDrawing() {
      this.isDrawing = false
    },
    getPosition(e) {
      const canvas = this.$refs.signatureCanvas
      const rect = canvas.getBoundingClientRect()
      return {
        x: (e.clientX || e.touches[0].clientX) - rect.left,
        y: (e.clientY || e.touches[0].clientY) - rect.top
      }
    },
    clearSignature() {
      const canvas = this.$refs.signatureCanvas
      this.ctx.clearRect(0, 0, canvas.width, canvas.height)
    },
    saveSignature() {
      const canvas = this.$refs.signatureCanvas
      const image = canvas.toDataURL('image/png')
      // 可以发送到服务器或保存到本地
      console.log(image)
    }
  }
}
</script>

<style scoped>
canvas {
  border: 1px solid #000;
  background-color: #fff;
  width: 100%;
  height: 300px;
}
</style>

使用vue-signature-pad组件

<template>
  <div>
    <VueSignaturePad 
      ref="signaturePad"
      width="100%"
      height="300px"
      :options="options"
    />
    <button @click="undo">撤销</button>
    <button @click="save">保存</button>
    <button @click="clear">清除</button>
  </div>
</template>

<script>
import VueSignaturePad from 'vue-signature-pad'

export default {
  components: {
    VueSignaturePad
  },
  data() {
    return {
      options: {
        penColor: '#000',
        backgroundColor: '#fff'
      }
    }
  },
  methods: {
    undo() {
      this.$refs.signaturePad.undoSignature()
    },
    save() {
      const { isEmpty, data } = this.$refs.signaturePad.saveSignature()
      if (!isEmpty) {
        console.log(data) // base64格式图片数据
      }
    },
    clear() {
      this.$refs.signaturePad.clearSignature()
    }
  }
}
</script>

移动端适配要点

对于移动设备,需要特别处理触摸事件并添加以下CSS属性:

canvas {
  touch-action: none; /* 防止页面滚动 */
  -webkit-user-select: none; /* 防止文本选择 */
  user-select: none;
}

保存签名数据

签名可以保存为多种格式:

vue实现签名

// 获取Base64编码的PNG图片
const dataURL = canvas.toDataURL('image/png')

// 获取Blob对象
canvas.toBlob(blob => {
  const formData = new FormData()
  formData.append('signature', blob)
  // 上传到服务器
}, 'image/png')

注意事项

  1. 在组件销毁时清理事件监听器
  2. 响应式调整Canvas大小需要监听窗口变化
  3. 考虑添加撤销功能栈实现多步撤销
  4. 签名区域应有明确的边界和背景色
  5. 高DPI屏幕需要调整Canvas的显示比例

标签: vue
分享给朋友:

相关文章

vue实现排序

vue实现排序

Vue 实现排序的方法 在 Vue 中实现排序可以通过多种方式完成,以下是一些常见的方法: 使用计算属性排序数组 计算属性非常适合对数据进行排序,因为它会在依赖的数据变化时自动更新。以下是一个示例:…

vue 绑定实现

vue 绑定实现

Vue 绑定实现 Vue 提供了多种数据绑定的方式,包括文本插值、属性绑定、事件绑定、双向绑定等。以下是常见的绑定实现方法: 文本插值 使用双大括号 {{ }} 进行文本插值,将数据动态渲染到 DO…

vue实现画廊

vue实现画廊

Vue实现画廊的基本方法 使用Vue.js实现画廊功能可以通过多种方式完成,以下是一种常见的实现方案: 安装必要依赖 npm install vue-gallery --save 引入组件并注册…

vue 实现赋值

vue 实现赋值

Vue 实现赋值的方法 在 Vue 中,赋值操作通常涉及数据绑定和响应式更新。以下是几种常见的赋值方式: 直接赋值给 data 属性 在 Vue 实例的 data 对象中定义的属性可以直接通过 th…

vue 实现长按

vue 实现长按

实现长按功能的基本思路 在Vue中实现长按功能,通常需要监听元素的mousedown或touchstart事件,启动一个计时器。当用户按住元素超过设定的时间阈值时,触发长按回调函数。如果在时间阈值内触…

vue实现搜索提示

vue实现搜索提示

Vue实现搜索提示的方法 使用v-model绑定输入框 通过v-model将输入框的值与Vue实例中的数据进行双向绑定,实时获取用户输入内容。 <input v-model="searchQu…