当前位置:首页 > VUE

vue实现手势签到

2026-03-09 23:20:33VUE

Vue 实现手势签到的步骤

使用 vue-signature 插件

安装 vue-signature 插件,这是一个专门用于 Vue 的手势签名库。
通过 npm 或 yarn 安装:

npm install vue-signature --save
# 或
yarn add vue-signature

在 Vue 组件中引入并使用:

<template>
  <div>
    <vue-signature ref="signature" :sigOption="options" />
    <button @click="save">保存签名</button>
    <button @click="clear">清除签名</button>
  </div>
</template>

<script>
import VueSignature from 'vue-signature';

export default {
  components: { VueSignature },
  data() {
    return {
      options: {
        penColor: '#000000',
        backgroundColor: '#ffffff'
      }
    };
  },
  methods: {
    save() {
      const signature = this.$refs.signature.save();
      console.log(signature); // 输出 base64 格式的签名图像
    },
    clear() {
      this.$refs.signature.clear();
    }
  }
};
</script>

使用 HTML5 Canvas 自定义实现

如果不想依赖第三方库,可以通过 HTML5 Canvas 实现手势签名功能。

在 Vue 组件中添加 Canvas 元素:

<template>
  <div>
    <canvas 
      ref="canvas" 
      @mousedown="startDrawing" 
      @mousemove="draw" 
      @mouseup="stopDrawing" 
      @mouseleave="stopDrawing"
      @touchstart="startDrawing" 
      @touchmove="draw" 
      @touchend="stopDrawing"
    ></canvas>
    <button @click="saveSignature">保存签名</button>
    <button @click="clearCanvas">清除</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isDrawing: false,
      canvas: null,
      ctx: null
    };
  },
  mounted() {
    this.canvas = this.$refs.canvas;
    this.ctx = this.canvas.getContext('2d');
    this.canvas.width = 400;
    this.canvas.height = 200;
  },
  methods: {
    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();
      const clientX = e.clientX || e.touches[0].clientX;
      const clientY = e.clientY || e.touches[0].clientY;
      return {
        x: clientX - rect.left,
        y: clientY - rect.top
      };
    },
    saveSignature() {
      const dataUrl = this.canvas.toDataURL('image/png');
      console.log(dataUrl); // 输出 base64 格式的签名图像
    },
    clearCanvas() {
      this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
    }
  }
};
</script>

移动端适配

为了在移动端支持触摸事件,需要在 Canvas 上添加 @touchstart@touchmove@touchend 事件。
getPosition 方法中处理触摸事件坐标,确保兼容性。

保存签名数据

签名完成后,可以通过 canvas.toDataURL() 方法将画布内容转换为 base64 格式的图像数据。
可以将数据发送到服务器或存储在本地(如 localStorage)。

样式优化

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

vue实现手势签到

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

注意事项

  • 确保 Canvas 的宽高在 mounted 钩子中设置,避免渲染问题。
  • 清除画布时使用 clearRect 方法,而非重新初始化 Canvas。
  • 移动端需测试触摸事件的兼容性,必要时添加 preventDefault 防止页面滚动。

标签: 手势vue
分享给朋友:

相关文章

vue实现视窗

vue实现视窗

Vue 实现视窗功能 在 Vue 中实现视窗功能通常涉及监听浏览器窗口大小变化、响应式布局或创建自定义弹窗组件。以下是几种常见实现方式: 监听浏览器窗口大小变化 使用 Vue 的 mounted 和…

vue实现iframe

vue实现iframe

Vue 中实现 iframe 的方法 在 Vue 项目中,可以通过多种方式实现 iframe 的嵌入。以下是几种常见的方法: 使用原生 HTML iframe 标签 直接在 Vue 组件的模板中使用…

vue实现slider

vue实现slider

Vue 实现 Slider 组件 使用 Vue 实现 Slider 组件可以通过原生开发或借助第三方库。以下是两种常见方法: 原生实现 Slider 创建一个基础的 Slider 组件,通过 v-m…

vue实现granfana

vue实现granfana

Vue 实现类似 Grafana 的仪表盘 要在 Vue 中实现类似 Grafana 的仪表盘功能,需要结合数据可视化库、状态管理和 UI 组件。以下是关键步骤和推荐工具: 数据可视化库 使用 E…

vue实现翻译

vue实现翻译

Vue 实现翻译功能的方法 使用 i18n 插件实现多语言切换 安装 vue-i18n 插件: npm install vue-i18n 创建语言资源文件(如 en.json 和 zh.json):…

vue实现通讯

vue实现通讯

Vue 组件通讯方法 父子组件通讯 父组件向子组件传递数据通过 props,子组件向父组件传递数据通过 $emit 事件。 父组件模板: <child-component :message=…