当前位置:首页 > VUE

vue实现页面签字

2026-03-08 06:19:27VUE

Vue 实现页面签字功能

使用 canvas 实现手写签字

在 Vue 项目中,可以通过 HTML5 的 canvas 元素实现手写签字功能。创建一个 Vue 组件,包含 canvas 元素和必要的交互逻辑。

<template>
  <div>
    <canvas 
      ref="signaturePad" 
      @mousedown="startDrawing" 
      @mousemove="draw" 
      @mouseup="stopDrawing"
      @touchstart="startDrawing" 
      @touchmove="draw" 
      @touchend="stopDrawing"
    ></canvas>
    <button @click="clearCanvas">清除</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.signaturePad;
      canvas.width = 500;
      canvas.height = 300;
      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.signaturePad;
      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.signaturePad;
      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;
    },
    clearCanvas() {
      const canvas = this.$refs.signaturePad;
      this.ctx.clearRect(0, 0, canvas.width, canvas.height);
    },
    saveSignature() {
      const canvas = this.$refs.signaturePad;
      const image = canvas.toDataURL('image/png');
      this.$emit('save', image);
    }
  }
};
</script>

使用第三方库 signature_pad

对于更专业的签字功能,可以使用 signature_pad 库。该库提供了更丰富的功能和更好的兼容性。

安装 signature_pad:

vue实现页面签字

npm install signature_pad

在 Vue 组件中使用:

<template>
  <div>
    <canvas ref="signaturePad"></canvas>
    <button @click="clear">清除</button>
    <button @click="save">保存</button>
  </div>
</template>

<script>
import SignaturePad from 'signature_pad';

export default {
  data() {
    return {
      signaturePad: null,
    };
  },
  mounted() {
    const canvas = this.$refs.signaturePad;
    canvas.width = 500;
    canvas.height = 300;
    this.signaturePad = new SignaturePad(canvas);
  },
  methods: {
    clear() {
      this.signaturePad.clear();
    },
    save() {
      if (this.signaturePad.isEmpty()) {
        alert('请先签字');
        return;
      }
      const image = this.signaturePad.toDataURL('image/png');
      this.$emit('save', image);
    }
  }
};
</script>

移动端适配

对于移动端设备,需要添加 touch 事件的支持。上述代码已经包含了 touch 事件的处理,确保在移动设备上也能正常使用。

vue实现页面签字

保存签字图片

签字完成后,可以将 canvas 转换为 base64 编码的图片数据,用于保存或上传到服务器。通过调用 canvas 的 toDataURL 方法实现。

const image = canvas.toDataURL('image/png');

可以将该图片数据通过事件传递给父组件,或直接发送到后端服务器。

样式优化

为 canvas 添加边框和背景色,提升用户体验:

canvas {
  border: 1px solid #ddd;
  background-color: #f9f9f9;
}

注意事项

  • 确保 canvas 的宽度和高度在 mounted 钩子中设置,避免渲染问题。
  • 对于移动端,考虑添加 preventDefault 以防止页面滚动干扰签字。
  • 签字数据保存前,检查是否为空,避免保存空白签字。

标签: 页面vue
分享给朋友:

相关文章

vue中实现显示和隐藏

vue中实现显示和隐藏

控制显示与隐藏的方法 在Vue中实现元素的显示和隐藏可以通过多种方式完成,以下是常见的几种方法: 1. 使用v-if指令v-if根据条件动态添加或移除DOM元素,适合需要频繁切换的场景。当条件为fa…

vue实现付款

vue实现付款

Vue 实现付款功能 在 Vue 中实现付款功能通常需要集成第三方支付网关(如支付宝、微信支付、Stripe 等)。以下是常见的实现方法: 集成支付宝/微信支付 安装必要的依赖(如 axios 用于…

代码实现vue

代码实现vue

创建Vue项目 使用Vue CLI快速初始化项目,确保已安装Node.js和npm/yarn: npm install -g @vue/cli vue create my-vue-project c…

vue实现建模

vue实现建模

Vue 实现建模的方法 在 Vue 中实现建模通常涉及数据绑定、组件化和状态管理。以下是几种常见的方法: 数据驱动建模 Vue 的核心是数据驱动视图。通过定义数据模型,Vue 会自动更新 DOM。例…

vue实现编辑

vue实现编辑

Vue 实现编辑功能 在 Vue 中实现编辑功能通常涉及表单绑定、状态管理和事件处理。以下是一个常见的实现方法: 双向数据绑定 使用 v-model 指令实现表单元素与数据的双向绑定: <i…

vue实现注销

vue实现注销

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