当前位置:首页 > VUE

vue实现电子签名

2026-01-15 06:57:25VUE

实现电子签名功能

在Vue中实现电子签名功能通常需要借助Canvas API进行绘制,并支持导出签名图片。以下是具体实现方法:

核心代码实现

创建一个名为SignaturePad.vue的组件:

<template>
  <div class="signature-container">
    <canvas 
      ref="canvas"
      @mousedown="startDrawing"
      @mousemove="draw"
      @mouseup="stopDrawing"
      @mouseleave="stopDrawing"
      @touchstart="handleTouchStart"
      @touchmove="handleTouchMove"
      @touchend="handleTouchEnd">
    </canvas>
    <div class="button-group">
      <button @click="clearSignature">清除</button>
      <button @click="saveSignature">保存</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isDrawing: false,
      lastX: 0,
      lastY: 0,
      canvas: null,
      ctx: null
    }
  },
  mounted() {
    this.initCanvas()
  },
  methods: {
    initCanvas() {
      this.canvas = this.$refs.canvas
      this.ctx = this.canvas.getContext('2d')

      // 设置canvas尺寸与实际显示尺寸一致
      const rect = this.canvas.getBoundingClientRect()
      this.canvas.width = rect.width
      this.canvas.height = rect.height

      // 初始化样式
      this.ctx.strokeStyle = '#000'
      this.ctx.lineWidth = 2
      this.ctx.lineCap = 'round'
      this.ctx.lineJoin = '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 rect = this.canvas.getBoundingClientRect()
      return {
        x: (e.clientX || e.touches[0].clientX) - rect.left,
        y: (e.clientY || e.touches[0].clientY) - rect.top
      }
    },
    clearSignature() {
      this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height)
    },
    saveSignature() {
      const image = this.canvas.toDataURL('image/png')
      this.$emit('save', image)
    },
    handleTouchStart(e) {
      e.preventDefault()
      this.startDrawing(e)
    },
    handleTouchMove(e) {
      e.preventDefault()
      this.draw(e)
    },
    handleTouchEnd(e) {
      e.preventDefault()
      this.stopDrawing()
    }
  }
}
</script>

<style scoped>
.signature-container {
  border: 1px solid #ddd;
  position: relative;
}
canvas {
  width: 100%;
  height: 200px;
  background-color: #f9f9f9;
  touch-action: none;
}
.button-group {
  margin-top: 10px;
  display: flex;
  gap: 10px;
}
button {
  padding: 5px 10px;
  cursor: pointer;
}
</style>

使用组件示例

在父组件中使用签名组件:

<template>
  <div>
    <h3>请在此签名</h3>
    <SignaturePad @save="handleSaveSignature" />
    <div v-if="signatureImage">
      <h4>签名预览:</h4>
      <img :src="signatureImage" alt="签名" />
    </div>
  </div>
</template>

<script>
import SignaturePad from './SignaturePad.vue'

export default {
  components: {
    SignaturePad
  },
  data() {
    return {
      signatureImage: null
    }
  },
  methods: {
    handleSaveSignature(image) {
      this.signatureImage = image
      // 可以在这里将图片发送到服务器
    }
  }
}
</script>

进阶功能扩展

  1. 签名验证:添加签名完成后的验证逻辑,确保签名符合要求
  2. 笔迹样式:提供不同颜色和粗细的笔迹选择
  3. 背景网格:在签名区域添加辅助网格线
  4. 时间戳:在保存签名时自动添加时间戳
  5. 多页签名:支持多页文档的连续签名

移动端适配注意事项

  1. 确保添加了touch-action: none样式防止页面滚动干扰
  2. 处理触摸事件时注意调用preventDefault()
  3. 考虑不同设备像素比的影响,可能需要调整canvas分辨率

服务器端处理

签名图片通常以Base64格式发送到服务器,后端需要将其转换为文件存储:

vue实现电子签名

// Node.js示例
const fs = require('fs')
const base64Data = req.body.image.replace(/^data:image\/png;base64,/, '')
fs.writeFile('signature.png', base64Data, 'base64', (err) => {
  if (err) throw err
  console.log('签名已保存')
})

以上实现提供了电子签名的基本功能,可根据实际需求进一步扩展和完善。

标签: 电子签名vue
分享给朋友:

相关文章

用vue实现搜索查询

用vue实现搜索查询

实现搜索查询的基本思路 在Vue中实现搜索查询功能通常涉及以下几个核心部分:数据绑定、事件监听、过滤逻辑和UI反馈。以下是一个完整的实现方案。 数据绑定与输入框 创建一个Vue组件,包含一个输入框用…

vue按钮实现截屏

vue按钮实现截屏

Vue 按钮实现截屏的方法 在 Vue 项目中实现截屏功能可以通过多种方式完成,以下是几种常见的方法: 使用 html2canvas 库 安装 html2canvas 库: npm install…

vue实现拖放

vue实现拖放

Vue 实现拖放功能 Vue 中实现拖放功能通常可以通过 HTML5 的原生拖放 API 或第三方库(如 vuedraggable)来实现。以下是两种方法的详细说明。 使用 HTML5 原生拖放 A…

vue 实现单选

vue 实现单选

使用 v-model 绑定单选值 在 Vue 中,可以通过 v-model 绑定单选按钮的值。定义一个响应式数据(如 selectedOption),并将其与单选按钮的 value 属性关联。…

vue 实现筛选

vue 实现筛选

实现筛选功能的基本方法 在Vue中实现筛选功能通常涉及以下几个核心步骤: 数据绑定与筛选逻辑 使用v-model绑定筛选条件到Vue实例的数据属性,结合计算属性实现动态筛选: data() {…

vue如何实现

vue如何实现

Vue 实现方法 在 Vue 中实现功能通常涉及组件化开发、状态管理和响应式数据绑定。以下是几种常见的实现方式: 组件化开发 使用 Vue 的单文件组件(.vue 文件)结构,将 UI 拆分为可复用…