当前位置:首页 > 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 弹幕不重叠的方法 CSS 定位与动画控制 通过绝对定位和动态计算弹幕轨道高度,确保弹幕在不同轨道上运行。每条弹幕占据固定高度的轨道,避免重叠。 // 弹幕轨道高度计算 const tr…

vue实现微博印象

vue实现微博印象

实现微博印象功能的核心思路 微博印象功能通常指用户发布微博后,系统自动分析文本内容并提取关键词或标签,以可视化形式展示。Vue实现该功能需要结合文本分析、关键词提取和前端渲染技术。 文本分析与关键词…

vue实现按钮组轮换

vue实现按钮组轮换

实现按钮组轮换的方法 在Vue中实现按钮组轮换效果可以通过动态绑定类和事件处理来完成。以下是一种常见的实现方式: 模板部分 <template> <div class="bu…

vue实现管道

vue实现管道

Vue 中实现管道(Pipe)功能 在 Vue 中可以通过过滤器(Filters)或计算属性(Computed Properties)实现类似管道的功能,将数据经过多个处理步骤后输出。 使用过滤器(…

vue实现建模

vue实现建模

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

tabbar 实现 vue

tabbar 实现 vue

在 Vue 中实现 TabBar 功能可以通过多种方式完成,以下是几种常见的方法: 使用 Vue Router 结合自定义组件 创建一个自定义的 TabBar 组件,结合 Vue Router 实现…