当前位置:首页 > VUE

vue实现签名

2026-02-25 15:22:01VUE

Vue 实现签名功能

在 Vue 中实现签名功能可以通过 HTML5 的 Canvas 元素结合触摸和鼠标事件来完成。以下是一个完整的实现方案:

安装依赖(可选)

如果需要更高级的签名功能,可以安装专门的签名库:

npm install vue-signature-pad

基础实现(使用原生 Canvas)

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

<script>
export default {
  data() {
    return {
      isDrawing: false,
      canvas: null,
      ctx: null
    }
  },
  mounted() {
    this.canvas = this.$refs.canvas
    this.ctx = this.canvas.getContext('2d')
    this.resizeCanvas()
    window.addEventListener('resize', this.resizeCanvas)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.resizeCanvas)
  },
  methods: {
    resizeCanvas() {
      this.canvas.width = this.canvas.offsetWidth
      this.canvas.height = this.canvas.offsetHeight
      this.ctx.lineWidth = 2
      this.ctx.lineCap = 'round'
      this.ctx.strokeStyle = '#000000'
    },
    startDrawing(e) {
      this.isDrawing = true
      this.draw(e)
    },
    draw(e) {
      if (!this.isDrawing) return

      const rect = this.canvas.getBoundingClientRect()
      const x = (e.clientX || e.touches[0].clientX) - rect.left
      const y = (e.clientY || e.touches[0].clientY) - rect.top

      this.ctx.lineTo(x, y)
      this.ctx.stroke()
      this.ctx.beginPath()
      this.ctx.moveTo(x, y)
    },
    stopDrawing() {
      this.isDrawing = false
      this.ctx.beginPath()
    },
    clearCanvas() {
      this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height)
    },
    saveSignature() {
      const image = this.canvas.toDataURL('image/png')
      // 可以发送到服务器或保存到本地
      console.log(image)
    }
  }
}
</script>

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

使用 vue-signature-pad 库

<template>
  <div>
    <SignaturePad ref="signaturePad" />
    <button @click="clear">清除</button>
    <button @click="save">保存</button>
  </div>
</template>

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

export default {
  components: {
    SignaturePad
  },
  methods: {
    clear() {
      this.$refs.signaturePad.clearSignature()
    },
    save() {
      const { isEmpty, data } = this.$refs.signaturePad.saveSignature()
      if (!isEmpty) {
        console.log(data) // 可以发送到服务器或保存
      }
    }
  }
}
</script>

移动端优化

  1. 添加 touch 事件支持(已在基础实现中包含)
  2. 防止触摸滚动:
    
    draw(e) {
    if (!this.isDrawing) return
    e.preventDefault() // 阻止默认行为防止页面滚动

// 其余绘制代码... }

vue实现签名


#### 保存签名
签名可以转换为 Base64 格式或 Blob 对象:
```javascript
// 获取 PNG 格式的 Base64
const pngData = canvas.toDataURL('image/png')

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

注意事项

  1. 响应式设计:确保 Canvas 在窗口大小变化时重新调整尺寸
  2. 线条样式:可根据需求调整 lineWidth、lineCap 和 strokeStyle
  3. 清除功能:提供清除画布的方法
  4. 移动端兼容:同时监听 touch 和 mouse 事件
  5. 保存格式:通常使用 PNG 格式保存签名图片

标签: vue
分享给朋友:

相关文章

vue公共列表的实现

vue公共列表的实现

实现 Vue 公共列表组件 公共列表组件通常用于展示重复结构的数据,例如商品列表、用户列表等。以下是一个典型的实现方式: 基础实现 创建可复用的列表组件 List.vue: <tem…

vue实现编辑

vue实现编辑

Vue 实现编辑功能 在 Vue 中实现编辑功能通常涉及表单绑定、状态管理和事件处理。以下是一个常见的实现方法: 双向数据绑定 使用 v-model 指令实现表单元素与数据的双向绑定: <i…

vue搜索实现

vue搜索实现

Vue 搜索功能实现方法 基于计算属性的搜索 在 Vue 中可以通过计算属性实现简单的搜索功能。这种方式适合小型数据集,利用 JavaScript 的 filter 方法进行筛选。 data() {…

vue 实现协同

vue 实现协同

Vue 实现协同编辑的方案 协同编辑指多个用户同时编辑同一文档并实时同步更改。Vue 结合相关库和技术可轻松实现该功能。 使用 WebSocket 实现实时通信 WebSocket 提供全双工通信,…

vue怎么实现 tab

vue怎么实现 tab

Vue 实现 Tab 的方法 使用动态组件和 v-if 通过动态组件或 v-if 指令切换不同 Tab 内容,结合点击事件改变当前激活的 Tab。 <template> <di…

vue实现语音播报

vue实现语音播报

实现语音播报的基本方法 在Vue中实现语音播报功能,可以通过Web Speech API中的SpeechSynthesis接口来完成。该接口允许开发者将文本转换为语音并播放。 使用SpeechSyn…