当前位置:首页 > VUE

vue实现手势签到

2026-01-17 17:41:53VUE

Vue 实现手势签到的方案

手势签到功能通常涉及绘制签名并保存为图片或数据。以下是基于 Vue 的实现方法:

使用 canvas 绘制签名

安装依赖库 vue-signature-pad

npm install vue-signature-pad --save

在组件中引入并使用:

vue实现手势签到

<template>
  <div>
    <vue-signature-pad 
      ref="signaturePad" 
      :options="{ penColor: '#000' }"
    />
    <button @click="clear">清除</button>
    <button @click="save">保存</button>
  </div>
</template>

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

export default {
  components: { VueSignaturePad },
  methods: {
    clear() {
      this.$refs.signaturePad.clearSignature();
    },
    async save() {
      const { data } = await this.$refs.signaturePad.saveSignature();
      console.log(data); // 获取 base64 格式的签名图片
    }
  }
};
</script>

自定义 canvas 实现

手动实现 canvas 绘制逻辑:

<template>
  <div>
    <canvas 
      ref="canvas"
      @mousedown="startDrawing"
      @mousemove="draw"
      @mouseup="stopDrawing"
    ></canvas>
    <button @click="clearCanvas">清除</button>
    <button @click="saveCanvas">保存</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.resizeCanvas();
  },
  methods: {
    resizeCanvas() {
      this.canvas.width = this.canvas.offsetWidth;
      this.canvas.height = this.canvas.offsetHeight;
    },
    startDrawing(e) {
      this.isDrawing = true;
      this.ctx.beginPath();
      this.ctx.moveTo(e.offsetX, e.offsetY);
    },
    draw(e) {
      if (!this.isDrawing) return;
      this.ctx.lineTo(e.offsetX, e.offsetY);
      this.ctx.stroke();
    },
    stopDrawing() {
      this.isDrawing = false;
    },
    clearCanvas() {
      this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
    },
    saveCanvas() {
      const image = this.canvas.toDataURL('image/png');
      console.log(image); // 获取 base64 图片数据
    }
  }
};
</script>

移动端触摸支持

为移动端添加触摸事件处理:

vue实现手势签到

<template>
  <canvas 
    ref="canvas"
    @touchstart="handleTouchStart"
    @touchmove="handleTouchMove"
    @touchend="handleTouchEnd"
  ></canvas>
</template>

<script>
export default {
  methods: {
    handleTouchStart(e) {
      e.preventDefault();
      const touch = e.touches[0];
      const mouseEvent = new MouseEvent('mousedown', {
        clientX: touch.clientX,
        clientY: touch.clientY
      });
      this.$refs.canvas.dispatchEvent(mouseEvent);
    },
    handleTouchMove(e) {
      e.preventDefault();
      const touch = e.touches[0];
      const mouseEvent = new MouseEvent('mousemove', {
        clientX: touch.clientX,
        clientY: touch.clientY
      });
      this.$refs.canvas.dispatchEvent(mouseEvent);
    },
    handleTouchEnd() {
      const mouseEvent = new MouseEvent('mouseup');
      this.$refs.canvas.dispatchEvent(mouseEvent);
    }
  }
};
</script>

保存签名数据

将签名数据上传至服务器:

async uploadSignature(imageData) {
  try {
    const response = await axios.post('/api/signature', {
      image: imageData
    });
    console.log('上传成功', response.data);
  } catch (error) {
    console.error('上传失败', error);
  }
}

优化用户体验

添加样式和交互优化:

canvas {
  border: 1px solid #ddd;
  background-color: #f9f9f9;
  touch-action: none; /* 禁用默认触摸行为 */
}
button {
  margin-top: 10px;
  padding: 8px 16px;
  background: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
}

通过以上方法,可以实现在 Vue 中完整的手势签到功能,支持 PC 和移动端,并能保存签名数据。

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

相关文章

vue实现防抖节流

vue实现防抖节流

Vue 中实现防抖(Debounce) 防抖的核心是延迟执行函数,在连续触发时取消前一次的调用,只执行最后一次。适用于输入框搜索、窗口大小调整等场景。 方法1:使用 Lodash 的 _.debou…

vue 实现筛选

vue 实现筛选

实现筛选功能的基本方法 在Vue中实现筛选功能通常涉及以下几个核心步骤: 数据绑定与筛选逻辑 使用v-model绑定筛选条件到Vue实例的数据属性,结合计算属性实现动态筛选: data() {…

vue实现webapp

vue实现webapp

Vue 实现 WebApp 的基本步骤 环境准备 确保已安装 Node.js 和 npm/yarn。通过 Vue CLI 或 Vite 创建项目: npm install -g @vue/cli v…

vue实现刷卡

vue实现刷卡

Vue 实现刷卡效果 在 Vue 中实现刷卡效果可以通过 CSS 动画和 Vue 的过渡系统结合完成。以下是一种常见的实现方式: 基础实现方法 安装 Vue 过渡依赖(如果尚未安装): npm i…

自实现vue

自实现vue

实现 Vue 的核心功能 要实现一个简化版的 Vue,需要理解其核心功能:数据响应式、模板编译、虚拟 DOM 和组件系统。以下是关键步骤的分解: 数据响应式 Vue 使用 Object.define…

vue实现音乐

vue实现音乐

Vue实现音乐播放功能 使用Vue实现音乐播放功能可以通过多种方式完成,以下是几种常见的实现方法: 使用HTML5 Audio API 在Vue组件中直接使用HTML5的Audio对象来控制音乐播放…