当前位置:首页 > VUE

vue实现页面签字

2026-03-28 16:59:42VUE

实现页面签字的Vue方案

使用Vue实现页面签字功能可以通过HTML5的Canvas或第三方库完成。以下是两种常见方法:

vue实现页面签字

使用Canvas原生实现

在Vue组件中创建Canvas画布,通过鼠标/触摸事件捕获绘制路径:

vue实现页面签字

<template>
  <div>
    <canvas 
      ref="signaturePad"
      @mousedown="startDrawing"
      @mousemove="draw"
      @mouseup="stopDrawing"
      @mouseleave="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
    }
  },
  methods: {
    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 ctx = canvas.getContext('2d')
      const rect = canvas.getBoundingClientRect()
      const currentX = (e.clientX || e.touches[0].clientX) - rect.left
      const currentY = (e.clientY || e.touches[0].clientY) - rect.top

      ctx.beginPath()
      ctx.moveTo(this.lastX, this.lastY)
      ctx.lineTo(currentX, currentY)
      ctx.strokeStyle = '#000'
      ctx.lineWidth = 2
      ctx.stroke()

      this.lastX = currentX
      this.lastY = currentY
    },
    stopDrawing() {
      this.isDrawing = false
    },
    clearCanvas() {
      const canvas = this.$refs.signaturePad
      const ctx = canvas.getContext('2d')
      ctx.clearRect(0, 0, canvas.width, canvas.height)
    },
    saveSignature() {
      const canvas = this.$refs.signaturePad
      const image = canvas.toDataURL('image/png')
      this.$emit('save', image)
    }
  },
  mounted() {
    const canvas = this.$refs.signaturePad
    canvas.width = canvas.offsetWidth
    canvas.height = canvas.offsetHeight
  }
}
</script>

使用第三方库vue-signature-pad

安装依赖:

npm install vue-signature-pad

组件实现:

<template>
  <div>
    <VueSignaturePad 
      ref="signaturePad"
      :options="options"
      width="500px"
      height="300px"
    />
    <button @click="undo">撤销</button>
    <button @click="save">保存</button>
    <button @click="clear">清除</button>
  </div>
</template>

<script>
import VueSignaturePad from 'vue-signature-pad'

export default {
  components: { VueSignaturePad },
  data() {
    return {
      options: {
        penColor: "#000",
        backgroundColor: "#fff"
      }
    }
  },
  methods: {
    undo() {
      this.$refs.signaturePad.undoSignature()
    },
    save() {
      const { data } = this.$refs.signaturePad.saveSignature()
      this.$emit('save', data)
    },
    clear() {
      this.$refs.signaturePad.clearSignature()
    }
  }
}
</script>

关键注意事项

  • 移动端适配需要处理touch事件
  • 保存签名时可转换为Base64或Blob格式
  • 清除功能需重置画布状态
  • 考虑添加撤销(undo)功能提升用户体验
  • 对于正式项目,建议使用成熟库如signature_pad或vue-signature-pad

两种方案均可实现电子签名功能,原生Canvas方案更轻量但需要自行处理更多细节,第三方库方案功能更完善但会增加包体积。

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

相关文章

vue实现导航栏切图

vue实现导航栏切图

Vue 实现导航栏切图 使用 Vue Router 实现基础导航 在 Vue 项目中,可以通过 Vue Router 实现导航栏的路由切换。创建一个导航栏组件,并在其中使用 <router-li…

vue实现遮罩

vue实现遮罩

Vue 实现遮罩层的方法 在 Vue 中实现遮罩层可以通过多种方式完成,以下是几种常见的方法: 方法一:使用 v-show 或 v-if 控制显示 通过 Vue 的指令动态控制遮罩层的显示与隐藏。…

vue实现返回

vue实现返回

返回按钮功能实现 在Vue中实现返回功能通常有两种方式:使用浏览器历史记录的history.back()或结合Vue Router进行编程式导航。以下是具体实现方法: 使用浏览器API metho…

vue 实现拖拉

vue 实现拖拉

Vue 实现拖拽功能的方法 使用 HTML5 原生拖放 API HTML5 提供了原生的拖放 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 dr…

vue 绑定实现

vue 绑定实现

Vue 绑定实现 Vue 提供了多种数据绑定的方式,包括文本插值、属性绑定、事件绑定、双向绑定等。以下是常见的绑定实现方法: 文本插值 使用双大括号 {{ }} 进行文本插值,将数据动态渲染到 DO…

vue实现加减

vue实现加减

Vue 实现加减功能 在 Vue 中实现加减功能可以通过数据绑定和事件监听来完成。以下是一个简单的实现方法: 模板部分 <template> <div> <…