当前位置:首页 > VUE

vue手写签名如何实现

2026-01-12 04:57:46VUE

实现 Vue 手写签名的步骤

使用 canvas 实现基础签名功能

在 Vue 项目中创建一个组件,利用 HTML5 的 canvas 元素实现手写签名功能。通过监听鼠标或触摸事件来捕获用户的绘制路径。

<template>
  <div>
    <canvas
      ref="signatureCanvas"
      @mousedown="startDrawing"
      @mousemove="draw"
      @mouseup="stopDrawing"
      @mouseleave="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,
      lastX: 0,
      lastY: 0,
    };
  },
  mounted() {
    this.setupCanvas();
  },
  methods: {
    setupCanvas() {
      const canvas = this.$refs.signatureCanvas;
      canvas.width = canvas.offsetWidth;
      canvas.height = canvas.offsetHeight;
      this.ctx = canvas.getContext('2d');
      this.ctx.strokeStyle = '#000';
      this.ctx.lineWidth = 2;
      this.ctx.lineCap = 'round';
    },
    startDrawing(e) {
      this.isDrawing = true;
      const canvas = this.$refs.signatureCanvas;
      const rect = canvas.getBoundingClientRect();
      this.lastX = e.clientX - rect.left;
      this.lastY = e.clientY - rect.top;
    },
    draw(e) {
      if (!this.isDrawing) return;
      const canvas = this.$refs.signatureCanvas;
      const rect = canvas.getBoundingClientRect();
      const currentX = e.clientX - rect.left;
      const currentY = e.clientY - rect.top;

      this.ctx.beginPath();
      this.ctx.moveTo(this.lastX, this.lastY);
      this.ctx.lineTo(currentX, currentY);
      this.ctx.stroke();

      this.lastX = currentX;
      this.lastY = currentY;
    },
    stopDrawing() {
      this.isDrawing = false;
    },
    clearSignature() {
      const canvas = this.$refs.signatureCanvas;
      this.ctx.clearRect(0, 0, canvas.width, canvas.height);
    },
    saveSignature() {
      const canvas = this.$refs.signatureCanvas;
      const dataURL = canvas.toDataURL('image/png');
      console.log(dataURL); // 可以保存或发送到服务器
    },
  },
};
</script>

处理触摸事件支持移动端

为了在移动设备上也能正常使用,需要添加触摸事件的处理逻辑。上述代码已经包含了触摸事件的监听,确保在触摸屏设备上也能正确捕获绘制路径。

优化绘制体验

可以通过调整线条的粗细、颜色和样式来优化绘制体验。例如,根据绘制速度动态调整线条粗细,使签名看起来更自然。

this.ctx.lineWidth = 2; // 可以根据需要调整
this.ctx.strokeStyle = '#000000'; // 黑色线条
this.ctx.lineCap = 'round'; // 线条末端为圆形

保存签名图片

使用 canvas 的 toDataURL 方法将绘制的签名转换为 base64 格式的图片数据,可以保存到本地或上传到服务器。

saveSignature() {
  const canvas = this.$refs.signatureCanvas;
  const dataURL = canvas.toDataURL('image/png');
  // 可以进一步处理,如保存到本地或发送到服务器
  console.log(dataURL);
}

清除签名

通过调用 clearRect 方法清除 canvas 上的绘制内容,实现清除签名的功能。

clearSignature() {
  const canvas = this.$refs.signatureCanvas;
  this.ctx.clearRect(0, 0, canvas.width, canvas.height);
}

响应式调整 canvas 大小

在窗口大小变化时,重新设置 canvas 的宽高,确保签名区域始终适应容器大小。

mounted() {
  this.setupCanvas();
  window.addEventListener('resize', this.setupCanvas);
},
beforeDestroy() {
  window.removeEventListener('resize', this.setupCanvas);
},

使用第三方库简化实现

如果需要更复杂的功能或更简单的实现方式,可以考虑使用第三方库如 vue-signaturesignature_pad

vue手写签名如何实现

npm install signature_pad
<template>
  <div>
    <canvas ref="signatureCanvas"></canvas>
    <button @click="clear">清除</button>
    <button @click="save">保存</button>
  </div>
</template>

<script>
import SignaturePad from 'signature_pad';

export default {
  mounted() {
    const canvas = this.$refs.signatureCanvas;
    this.signaturePad = new SignaturePad(canvas);
  },
  methods: {
    clear() {
      this.signaturePad.clear();
    },
    save() {
      const dataURL = this.signaturePad.toDataURL();
      console.log(dataURL);
    },
  },
};
</script>

标签: 如何实现vue
分享给朋友:

相关文章

vue实现menu

vue实现menu

Vue 实现 Menu 的方法 使用 Element UI 的 Menu 组件 Element UI 提供了现成的 Menu 组件,适合快速实现导航菜单。安装 Element UI 后,可以直接使用…

vue实现注册

vue实现注册

Vue 实现注册功能 注册功能通常涉及前端表单、数据验证、与后端 API 的交互等步骤。以下是一个完整的 Vue 实现注册功能的方案。 创建注册表单 使用 Vue 的模板语法创建注册表单,包含用户名…

vue实现popper

vue实现popper

Vue 实现 Popper 的方法 使用 Tippy.js 库 Tippy.js 是一个轻量级的 Popper.js 封装库,提供丰富的工具提示功能。安装 Tippy.js 及其 Vue 封装: n…

vue实现标注

vue实现标注

Vue 实现标注功能的方法 使用 Vue 实现标注功能可以通过多种方式实现,以下介绍几种常见的方法: 1. 使用 HTML5 Canvas 实现标注 Canvas 提供了强大的绘图能力,适合实现复…

vue框架实现

vue框架实现

Vue 框架实现方法 Vue 是一个渐进式 JavaScript 框架,适用于构建用户界面。以下是一些核心实现方法,涵盖基本使用、组件化、状态管理和优化技巧。 基本项目搭建 使用 Vue CLI…

vue实现卡片

vue实现卡片

Vue 实现卡片组件的方法 使用 Vue 实现卡片组件可以通过多种方式完成,以下是几种常见的实现方法: 使用原生 HTML 和 CSS 在 Vue 单文件组件中,可以通过模板和样式直接实现卡片效果…