当前位置:首页 > VUE

vue实现手写签批

2026-02-19 04:21:08VUE

实现手写签批功能

在Vue中实现手写签批功能,可以通过HTML5的Canvas API结合触摸事件或鼠标事件来完成。以下是具体的实现方法:

引入Canvas元素

在Vue组件的模板中添加一个Canvas元素,用于绘制手写内容:

<template>
  <div>
    <canvas 
      ref="signatureCanvas" 
      @mousedown="startDrawing" 
      @mousemove="draw" 
      @mouseup="stopDrawing" 
      @touchstart="startDrawing" 
      @touchmove="draw" 
      @touchend="stopDrawing"
    ></canvas>
    <button @click="clearCanvas">清除</button>
    <button @click="saveSignature">保存</button>
  </div>
</template>

初始化Canvas

在组件的mounted生命周期钩子中初始化Canvas,设置其大小和样式:

mounted() {
  const canvas = this.$refs.signatureCanvas;
  canvas.width = canvas.offsetWidth;
  canvas.height = canvas.offsetHeight;
  this.ctx = canvas.getContext('2d');
  this.ctx.strokeStyle = '#000000';
  this.ctx.lineWidth = 2;
}

实现绘图功能

添加绘图相关的方法,处理鼠标和触摸事件:

vue实现手写签批

data() {
  return {
    isDrawing: false,
    lastX: 0,
    lastY: 0,
    ctx: null
  };
},
methods: {
  startDrawing(e) {
    this.isDrawing = true;
    const canvas = this.$refs.signatureCanvas;
    const rect = canvas.getBoundingClientRect();
    this.lastX = (e.clientX || e.touches[0].clientX) - rect.left;
    this.lastY = (e.clientY || e.touches[0].clientY) - rect.top;
  },
  draw(e) {
    if (!this.isDrawing) return;
    const canvas = this.$refs.signatureCanvas;
    const rect = canvas.getBoundingClientRect();
    const currentX = (e.clientX || e.touches[0].clientX) - rect.left;
    const currentY = (e.clientY || e.touches[0].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;
  }
}

清除和保存功能

添加清除Canvas和保存签名的方法:

methods: {
  clearCanvas() {
    const canvas = this.$refs.signatureCanvas;
    this.ctx.clearRect(0, 0, canvas.width, canvas.height);
  },
  saveSignature() {
    const canvas = this.$refs.signatureCanvas;
    const image = canvas.toDataURL('image/png');
    // 可以发送到服务器或本地保存
    console.log(image);
  }
}

样式调整

为Canvas添加一些基本样式,确保其在页面上正确显示:

vue实现手写签批

canvas {
  border: 1px solid #000;
  background-color: #fff;
  width: 100%;
  height: 200px;
}
button {
  margin-top: 10px;
  padding: 5px 10px;
}

响应式处理

如果需要支持移动端触摸事件,确保Canvas的触摸事件不会被浏览器默认行为阻止:

methods: {
  startDrawing(e) {
    e.preventDefault();
    this.isDrawing = true;
    // 其余代码...
  },
  draw(e) {
    e.preventDefault();
    // 其余代码...
  }
}

使用第三方库

如果不想从头开始实现,可以使用现成的Vue手写签名库,如vue-signature-pad

npm install vue-signature-pad

在组件中使用:

import VueSignaturePad from 'vue-signature-pad';

export default {
  components: {
    VueSignaturePad
  },
  methods: {
    save() {
      const { data } = this.$refs.signature.save();
      console.log(data);
    },
    undo() {
      this.$refs.signature.undo();
    }
  }
}
<template>
  <vue-signature-pad ref="signature" />
  <button @click="save">保存</button>
  <button @click="undo">撤销</button>
</template>

注意事项

确保Canvas的大小在初始化时正确设置,避免绘制时出现偏移。对于移动端,需要处理触摸事件并阻止默认行为,以确保绘图流畅。保存的签名可以转换为Base64格式的图片,便于上传到服务器或本地存储。

标签: vue
分享给朋友:

相关文章

vue实现一个标签选择

vue实现一个标签选择

Vue 标签选择组件实现 基础实现 使用 Vue 的 v-for 和 v-model 指令可以快速实现标签选择功能: <template> <div class="tag-se…

vue聊天实现

vue聊天实现

Vue 聊天应用实现步骤 创建 Vue 项目 使用 Vue CLI 或 Vite 初始化项目,安装必要依赖(如 Vue 3、Element UI 或 Vuetify 用于 UI 组件)。 npm c…

实现 vue 框架

实现 vue 框架

Vue 框架的基本实现 Vue 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是实现 Vue 框架的基本步骤和核心概念。 核心概念 响应式数据绑定 Vue 的核心特性之一是响应式数…

vue 实现后退

vue 实现后退

实现后退功能的方法 在Vue中实现后退功能通常需要结合浏览器历史记录API或Vue Router的导航方法。以下是几种常见的实现方式: 使用Vue Router的go方法 this.$router…

vue  怎么实现双击

vue 怎么实现双击

实现双击事件的方法 在 Vue 中实现双击事件可以通过 @dblclick 指令或自定义逻辑实现。以下是两种常见方法: 使用 @dblclick 指令 Vue 提供了原生 @dblclick…

vue实现导航栏

vue实现导航栏

使用 Vue 实现导航栏 基础导航栏结构 在 Vue 中实现导航栏通常使用 <router-link> 或自定义组件。以下是一个基础示例: <template> <…