当前位置:首页 > 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() // 阻止默认行为防止页面滚动

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


#### 保存签名
签名可以转换为 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 实现在线预览

Vue 实现在线预览的常见方法 使用 iframe 嵌入 通过 iframe 标签可以嵌入多种类型的文件,如 PDF、图片、网页等。需要确保文件地址可访问。 <template> &…

vue 实现菜单

vue 实现菜单

Vue 实现菜单的方法 使用 Vue 实现菜单可以通过多种方式,以下是几种常见的方法: 使用 Vue Router 实现动态路由菜单 通过 Vue Router 可以动态生成菜单,根据路由配置自动渲…

vue实现主页

vue实现主页

Vue 实现主页的基本步骤 创建一个 Vue 主页通常涉及以下几个关键步骤,从项目初始化到页面布局和功能实现。 初始化 Vue 项目 使用 Vue CLI 或 Vite 初始化项目。Vue CLI…

简单实现vue

简单实现vue

Vue 的基本实现 创建一个简单的 Vue 应用需要引入 Vue 库,并编写基本的模板和数据绑定逻辑。 <!DOCTYPE html> <html lang="en"> &…

vue实现动态

vue实现动态

Vue 实现动态路由的方法 在 Vue 中实现动态路由通常涉及以下几个关键步骤: 使用 Vue Router 的动态路由匹配 通过 Vue Router 的 :param 语法可以实现动态路由匹配。…

vue实现toggle

vue实现toggle

Vue 实现 Toggle 功能 在 Vue 中实现 toggle(切换)功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 和 v-on 通过 v-model 绑定数据,结合…