当前位置:首页 > VUE

vue 实现电子签名

2026-02-19 13:43:07VUE

实现电子签名的基础方法

使用Vue实现电子签名功能,可以通过HTML5的Canvas元素结合触摸或鼠标事件完成。核心步骤包括初始化画布、监听绘制事件、保存签名数据。

<template>
  <div>
    <canvas 
      ref="signaturePad" 
      @mousedown="startDrawing" 
      @mousemove="draw" 
      @mouseup="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,
      canvas: null,
      ctx: null
    };
  },
  mounted() {
    this.canvas = this.$refs.signaturePad;
    this.ctx = this.canvas.getContext('2d');
    this.resizeCanvas();
    window.addEventListener('resize', this.resizeCanvas);
  },
  methods: {
    resizeCanvas() {
      this.canvas.width = this.canvas.offsetWidth;
      this.canvas.height = this.canvas.offsetHeight;
    },
    startDrawing(e) {
      this.isDrawing = true;
      const pos = this.getPosition(e);
      this.ctx.beginPath();
      this.ctx.moveTo(pos.x, pos.y);
    },
    draw(e) {
      if (!this.isDrawing) return;
      const pos = this.getPosition(e);
      this.ctx.lineTo(pos.x, pos.y);
      this.ctx.stroke();
    },
    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 dataURL = this.canvas.toDataURL('image/png');
      console.log(dataURL); // 可上传至服务器或本地保存
    }
  }
};
</script>

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

使用第三方库优化体验

对于更专业的签名处理,可使用现成库如signature_pad

  1. 安装依赖:

    vue 实现电子签名

    npm install signature_pad
  2. 组件实现:

    
    <template>
    <div>
     <canvas ref="signatureCanvas"></canvas>
     <button @click="clear">清除</button>
     <button @click="save">保存</button>
    </div>
    </template>
import SignaturePad from 'signature_pad';

export default { data() { return { signaturePad: null }; }, mounted() { const canvas = this.$refs.signatureCanvas; canvas.width = canvas.offsetWidth; canvas.height = canvas.offsetHeight; this.signaturePad = new SignaturePad(canvas); }, methods: { clear() { this.signaturePad.clear(); }, save() { if (this.signaturePad.isEmpty()) { alert('请先签名'); return; } const data = this.signaturePad.toDataURL(); console.log(data); } } };

vue 实现电子签名

```

移动端适配要点

  1. 防止触摸滚动:在画布上添加@touchmove.prevent修饰符
  2. 笔迹平滑:调整signature_padthrottleminWidth参数
  3. 响应式设计:监听窗口变化自动调整画布尺寸
mounted() {
  this.initSignaturePad();
  window.addEventListener('resize', this.handleResize);
},
methods: {
  initSignaturePad() {
    const canvas = this.$refs.signatureCanvas;
    this.signaturePad = new SignaturePad(canvas, {
      minWidth: 1,
      maxWidth: 3,
      throttle: 16
    });
    this.handleResize();
  },
  handleResize() {
    const canvas = this.$refs.signatureCanvas;
    canvas.width = canvas.offsetWidth;
    canvas.height = canvas.offsetHeight;
    this.signaturePad.clear(); // 防止缩放导致笔迹变形
  }
}

签名数据后端处理

保存的Base64数据可通过以下方式处理:

  1. 前端转换:将Base64转为Blob对象上传

    function dataURLToBlob(dataURL) {
    const arr = dataURL.split(',');
    const mime = arr[0].match(/:(.*?);/)[1];
    const bstr = atob(arr[1]);
    let n = bstr.length;
    const u8arr = new Uint8Array(n);
    while (n--) {
     u8arr[n] = bstr.charCodeAt(n);
    }
    return new Blob([u8arr], { type: mime });
    }
  2. 后端验证:检查签名图片的有效性(非空白)

    isEmpty() {
    return this.signaturePad.isEmpty();
    }

高级功能扩展

  1. 笔迹颜色设置:通过penColor参数调整
  2. 背景网格:在Canvas底层绘制辅助线
  3. 压力感应:支持数位板压感(需浏览器支持)
  4. 时间戳水印:在保存时添加签名时间信息
// 设置笔迹颜色示例
setPenColor(color) {
  this.signaturePad.penColor = color;
}

以上方案可根据实际需求组合使用,纯前端实现适合简单场景,结合后端存储则能实现完整电子签名流程。

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

相关文章

vue如何实现到期提醒

vue如何实现到期提醒

实现 Vue 到期提醒功能 使用计算属性计算剩余时间 在 Vue 组件中定义一个计算属性,计算目标日期与当前日期的差值。通过 Date 对象获取时间戳并计算剩余天数或小时数。 computed: {…

vue实现录音文件播放

vue实现录音文件播放

使用 Vue 实现录音文件播放 准备工作 确保项目中已安装必要的依赖,如 vue-audio-recorder 或原生 Web Audio API。录音文件播放通常涉及录音、存储和播放三个核心环节。…

vue 实现挂载

vue 实现挂载

Vue 实现挂载的基本方法 在 Vue 中,挂载是将 Vue 实例与 DOM 元素关联的过程。通过挂载,Vue 可以管理 DOM 并实现数据绑定和响应式更新。 创建 Vue 实例并挂载 const…

vue 实现表单

vue 实现表单

Vue 表单实现方法 Vue.js 提供了多种方式实现表单,包括双向数据绑定、表单验证和动态表单生成。以下是常见的实现方法: 基础表单绑定 使用 v-model 指令实现表单元素与数据的双向绑定:…

vue模版实现

vue模版实现

Vue 模板实现方法 Vue 模板是 Vue.js 的核心特性之一,用于声明式地将 DOM 绑定至底层 Vue 实例的数据。以下是几种常见的 Vue 模板实现方式: 单文件组件(SFC) 使用 .v…

vue怎么实现数据

vue怎么实现数据

Vue 数据实现方法 Vue 提供了多种方式来实现数据的绑定和管理,核心是通过响应式系统确保数据与视图的同步。以下是常见的实现方法: 数据绑定 在 Vue 实例的 data 选项中定义数据,Vue…