当前位置:首页 > 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格式发送到服务器,后端需要将其转换为文件存储:

// 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实现年历

Vue 实现年历的方法 使用 Vue 实现年历可以通过组件化开发方式,结合日期计算和动态渲染完成。以下是两种常见的实现方式: 基于原生 Vue 的日历组件 创建 Calendar.vue 组件文件…

vue实现好友列表

vue实现好友列表

实现好友列表的Vue组件 数据准备 在Vue组件的data中定义好友列表数据,通常是一个数组对象,每个对象包含好友的基本信息如ID、头像、昵称等。 data() { return {…

vue实现安全免登录

vue实现安全免登录

Vue 实现安全免登录的方案 免登录功能通常通过持久化认证状态(如 Token)实现,但需兼顾安全性。以下是基于 Vue 的常见实现方案: 基于 Token 的免登录 登录成功后存储 Token…

vue公共列表的实现

vue公共列表的实现

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

vue前端分页怎么实现

vue前端分页怎么实现

前端分页实现方法 在Vue中实现前端分页通常涉及数据分割和页面控制逻辑。以下是常见的实现方式: 使用计算属性分页 通过计算属性对数据进行切片处理: computed: { paginatedD…

vue设计与实现下载

vue设计与实现下载

vue设计与实现电子书下载 《Vue.js设计与实现》是一本深入解析Vue.js框架原理的书籍,由霍春阳(HcySunYang)撰写。以下是获取该资源的常见途径: 正版购买渠道 京东、当当、天…