当前位置:首页 > 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;
}

实现绘图功能

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

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添加一些基本样式,确保其在页面上正确显示:

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 实现方法 Vue 是一个流行的前端框架,用于构建用户界面。以下是几种常见的实现方法: 数据绑定 使用 v-model 指令实现双向数据绑定,适用于表单输入元素。 <input v-m…

vue实现抖动

vue实现抖动

Vue 实现抖动效果 在 Vue 中实现抖动效果可以通过 CSS 动画或 JavaScript 动画库来实现。以下是几种常见的方法: 使用 CSS 动画 通过定义 @keyframes 动画并在…

vue实现注销

vue实现注销

实现用户注销功能 在Vue应用中实现用户注销功能通常涉及清除用户会话、重置应用状态并跳转到登录页面。以下是具体实现方法: 清除用户认证信息 使用Vuex或Pinia管理用户状态时,需在注销时重置存…

vue实现tree

vue实现tree

Vue 实现 Tree 组件 使用 Vue 实现 Tree 组件可以通过递归组件的方式来实现层级结构展示。以下是一个完整的实现方法: 基础递归组件实现 创建 Tree 组件文件 Tree.vue,使…

vue实现心电图

vue实现心电图

Vue 实现心电图效果 在 Vue 中实现心电图效果通常需要结合 Canvas 或 SVG 进行动态绘图,模拟心电图数据的实时变化。以下是两种实现方式的核心思路和代码示例: 使用 Canvas…

vue 实现长按

vue 实现长按

实现长按功能的基本思路 在Vue中实现长按功能,通常需要监听元素的mousedown或touchstart事件,启动一个计时器。当用户按住元素超过设定的时间阈值时,触发长按回调函数。如果在时间阈值内触…