当前位置:首页 > 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组件结构 新建一个WeiboPost.vue组件,包含文本框、图片上传和发布按钮: <template> <div class="we…

vue实现删除

vue实现删除

Vue 实现删除功能的方法 在 Vue 中实现删除功能通常涉及以下几个步骤: 数据绑定与列表渲染 使用 v-for 指令渲染列表数据,并为每个项绑定唯一标识符(如 id)。例如: <ul&g…

vue 实现平面

vue 实现平面

Vue 实现平面的方法 在 Vue 中实现平面效果通常涉及 CSS 样式、UI 框架或自定义组件的使用。以下是几种常见方法: 使用 CSS 样式 通过 Vue 的样式绑定或 scoped CSS 为…

黑马vue实现

黑马vue实现

以下是关于使用 Vue.js 实现黑马相关功能或项目的关键方法和实践指南: 环境配置与项目初始化 安装最新版 Vue CLI 或使用 Vite 创建项目: npm create vue@lates…

vue数据劫持实现

vue数据劫持实现

Vue 数据劫持的实现原理 Vue 的数据劫持主要通过 Object.defineProperty 或 Proxy 实现,用于监听数据变化并触发视图更新。以下是核心实现方式: 使用 Object.d…