当前位置:首页 > VUE

vue实现手写签名

2026-03-08 22:09:09VUE

Vue 实现手写签名的方法

在 Vue 中实现手写签名功能可以通过 HTML5 的 Canvas 元素和 JavaScript 事件监听来完成。以下是具体实现步骤:

安装依赖(可选)

如果需要更高级的功能或简化开发,可以安装第三方库如 signature_pad

npm install signature_pad

基础实现(使用原生 Canvas)

模板部分

vue实现手写签名

<template>
  <div>
    <canvas 
      ref="signatureCanvas" 
      @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.signatureCanvas;
    this.ctx = this.canvas.getContext('2d');
    this.resizeCanvas();
    window.addEventListener('resize', this.resizeCanvas);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.resizeCanvas);
  },
  methods: {
    resizeCanvas() {
      this.canvas.width = this.canvas.offsetWidth;
      this.canvas.height = this.canvas.offsetHeight;
      this.ctx.strokeStyle = '#000';
      this.ctx.lineWidth = 2;
    },
    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 - rect.left,
        y: e.clientY - rect.top
      };
    },
    clearSignature() {
      this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
    },
    saveSignature() {
      const image = this.canvas.toDataURL('image/png');
      console.log(image); // 可以保存或上传到服务器
    }
  }
};
</script>

样式部分

vue实现手写签名

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

使用 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 {
  data() {
    return {
      signaturePad: null
    };
  },
  mounted() {
    const canvas = this.$refs.signatureCanvas;
    this.signaturePad = new SignaturePad(canvas);
    this.resizeCanvas();
    window.addEventListener('resize', this.resizeCanvas);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.resizeCanvas);
  },
  methods: {
    resizeCanvas() {
      const canvas = this.$refs.signatureCanvas;
      canvas.width = canvas.offsetWidth;
      canvas.height = canvas.offsetHeight;
      this.signaturePad.clear(); // 防止缩放时签名变形
    },
    clear() {
      this.signaturePad.clear();
    },
    save() {
      if (this.signaturePad.isEmpty()) {
        alert('请先签名');
        return;
      }
      const image = this.signaturePad.toDataURL('image/png');
      console.log(image); // 可以保存或上传到服务器
    }
  }
};
</script>

移动端适配

  1. 添加 @touchstart@touchmove@touchend 事件(已在基础实现中包含)。
  2. 禁用页面滚动防止签名时页面移动:
    methods: {
    startDrawing(e) {
     e.preventDefault(); // 阻止默认行为
     this.isDrawing = true;
     // 其他逻辑
    }
    }

保存签名

签名可以转换为 Base64 或 Blob 格式上传到服务器:

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

// Blob 格式
canvas.toBlob((blob) => {
  const formData = new FormData();
  formData.append('signature', blob, 'signature.png');
  // 上传逻辑
}, 'image/png');

注意事项

  1. Canvas 宽高:必须在代码中显式设置 canvas.widthcanvas.height,否则绘制会模糊。
  2. 响应式设计:监听 resize 事件并调整 Canvas 大小。
  3. 库的优势signature_pad 提供了更平滑的绘制效果和内置方法(如 isEmpty())。

标签: vue
分享给朋友:

相关文章

vue实现图片搜索

vue实现图片搜索

Vue实现图片搜索功能 使用HTML5的File API获取图片 通过<input type="file">元素让用户选择图片文件,使用FileReader对象读取图片数据。 <t…

vue实现功能

vue实现功能

Vue 功能实现方法 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是实现常见功能的几种方法: 数据绑定与响应式更新 在 Vue 中,数据绑定通过 v-model…

vue 实现关闭

vue 实现关闭

Vue 实现关闭功能的方法 在Vue中实现关闭功能通常涉及组件销毁、模态框隐藏或路由跳转等场景。以下是几种常见实现方式: 销毁当前组件 使用v-if或v-show控制组件显示/隐藏,适用于局部组件:…

vue实现handsontable

vue实现handsontable

Vue 中实现 Handsontable 要在 Vue 项目中集成 Handsontable,可以使用官方提供的 @handsontable/vue 封装库。以下是一个完整的实现步骤。 安装依赖 确…

vue实现表白

vue实现表白

Vue 实现表白页面 使用 Vue 可以快速创建一个动态、交互式的表白页面。以下是一个简单的实现方案: 基础结构 创建一个 Vue 项目或单文件组件,包含以下核心部分: <template&…

vue实现picker

vue实现picker

Vue 实现 Picker 组件的方法 在 Vue 中实现 Picker 组件可以通过多种方式完成,以下是几种常见的实现方法: 使用原生 HTML 和 CSS 实现 通过 Vue 的模板和样式绑定,…