当前位置:首页 > VUE

vue实现手写功能

2026-02-20 11:53:20VUE

实现手写功能的基本思路

在Vue中实现手写功能通常需要结合HTML5的Canvas API,通过监听鼠标或触摸事件来捕获用户的绘制路径,并将其渲染到画布上。核心步骤包括初始化画布、记录绘制路径、处理触摸事件兼容性等。

初始化Canvas画布

在Vue组件的模板中添加Canvas元素,并设置宽度和高度。建议通过ref获取DOM引用以便后续操作:

<template>
  <canvas
    ref="canvas"
    @mousedown="startDrawing"
    @mousemove="draw"
    @mouseup="stopDrawing"
    @touchstart="handleTouchStart"
    @touchmove="handleTouchMove"
    @touchend="stopDrawing"
  ></canvas>
</template>

mounted生命周期中初始化画布上下文:

mounted() {
  const canvas = this.$refs.canvas;
  canvas.width = canvas.offsetWidth;
  canvas.height = canvas.offsetHeight;
  this.ctx = canvas.getContext('2d');
  this.ctx.lineWidth = 5;
  this.ctx.lineCap = 'round';
  this.ctx.strokeStyle = '#000000';
}

处理绘制逻辑

定义绘制状态变量和事件处理函数。使用isDrawing标记是否处于绘制状态,并记录路径点:

vue实现手写功能

data() {
  return {
    isDrawing: false,
    lastX: 0,
    lastY: 0
  };
},
methods: {
  startDrawing(e) {
    this.isDrawing = true;
    const { offsetX, offsetY } = this.getPosition(e);
    [this.lastX, this.lastY] = [offsetX, offsetY];
  },
  draw(e) {
    if (!this.isDrawing) return;
    const { offsetX, offsetY } = this.getPosition(e);
    this.ctx.beginPath();
    this.ctx.moveTo(this.lastX, this.lastY);
    this.ctx.lineTo(offsetX, offsetY);
    this.ctx.stroke();
    [this.lastX, this.lastY] = [offsetX, offsetY];
  },
  stopDrawing() {
    this.isDrawing = false;
  }
}

处理触摸事件兼容性

添加触摸事件的支持需要转换触摸坐标为画布坐标:

methods: {
  getPosition(e) {
    const canvas = this.$refs.canvas;
    if (e.type.includes('touch')) {
      const rect = canvas.getBoundingClientRect();
      return {
        offsetX: e.touches[0].clientX - rect.left,
        offsetY: e.touches[0].clientY - rect.top
      };
    }
    return { offsetX: e.offsetX, offsetY: e.offsetY };
  },
  handleTouchStart(e) {
    e.preventDefault();
    this.startDrawing(e);
  },
  handleTouchMove(e) {
    e.preventDefault();
    this.draw(e);
  }
}

添加清除和保存功能

扩展功能按钮,实现清除画布和保存图像:

vue实现手写功能

<button @click="clearCanvas">清除</button>
<button @click="saveAsImage">保存</button>

对应方法实现:

methods: {
  clearCanvas() {
    const canvas = this.$refs.canvas;
    this.ctx.clearRect(0, 0, canvas.width, canvas.height);
  },
  saveAsImage() {
    const canvas = this.$refs.canvas;
    const link = document.createElement('a');
    link.download = 'signature.png';
    link.href = canvas.toDataURL('image/png');
    link.click();
  }
}

优化绘制性能

对于频繁触发的mousemove事件,可以使用requestAnimationFrame进行节流:

draw(e) {
  if (!this.isDrawing) return;
  window.requestAnimationFrame(() => {
    const { offsetX, offsetY } = this.getPosition(e);
    this.ctx.beginPath();
    this.ctx.moveTo(this.lastX, this.lastY);
    this.ctx.lineTo(offsetX, offsetY);
    this.ctx.stroke();
    [this.lastX, this.lastY] = [offsetX, offsetY];
  });
}

完整组件示例

整合后的完整Vue组件代码:

<template>
  <div>
    <canvas
      ref="canvas"
      @mousedown="startDrawing"
      @mousemove="draw"
      @mouseup="stopDrawing"
      @touchstart="handleTouchStart"
      @touchmove="handleTouchMove"
      @touchend="stopDrawing"
    ></canvas>
    <div>
      <button @click="clearCanvas">清除</button>
      <button @click="saveAsImage">保存</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isDrawing: false,
      lastX: 0,
      lastY: 0,
      ctx: null
    };
  },
  mounted() {
    const canvas = this.$refs.canvas;
    canvas.width = canvas.offsetWidth;
    canvas.height = canvas.offsetHeight;
    this.ctx = canvas.getContext('2d');
    this.ctx.lineWidth = 5;
    this.ctx.lineCap = 'round';
    this.ctx.strokeStyle = '#000000';
  },
  methods: {
    getPosition(e) {
      const canvas = this.$refs.canvas;
      if (e.type.includes('touch')) {
        const rect = canvas.getBoundingClientRect();
        return {
          offsetX: e.touches[0].clientX - rect.left,
          offsetY: e.touches[0].clientY - rect.top
        };
      }
      return { offsetX: e.offsetX, offsetY: e.offsetY };
    },
    startDrawing(e) {
      this.isDrawing = true;
      const { offsetX, offsetY } = this.getPosition(e);
      [this.lastX, this.lastY] = [offsetX, offsetY];
    },
    draw(e) {
      if (!this.isDrawing) return;
      window.requestAnimationFrame(() => {
        const { offsetX, offsetY } = this.getPosition(e);
        this.ctx.beginPath();
        this.ctx.moveTo(this.lastX, this.lastY);
        this.ctx.lineTo(offsetX, offsetY);
        this.ctx.stroke();
        [this.lastX, this.lastY] = [offsetX, offsetY];
      });
    },
    stopDrawing() {
      this.isDrawing = false;
    },
    handleTouchStart(e) {
      e.preventDefault();
      this.startDrawing(e);
    },
    handleTouchMove(e) {
      e.preventDefault();
      this.draw(e);
    },
    clearCanvas() {
      const canvas = this.$refs.canvas;
      this.ctx.clearRect(0, 0, canvas.width, canvas.height);
    },
    saveAsImage() {
      const canvas = this.$refs.canvas;
      const link = document.createElement('a');
      link.download = 'signature.png';
      link.href = canvas.toDataURL('image/png');
      link.click();
    }
  }
};
</script>

<style scoped>
canvas {
  border: 1px solid #000;
  background-color: white;
  touch-action: none;
}
</style>

标签: 功能vue
分享给朋友:

相关文章

vue实现裁剪头像

vue实现裁剪头像

Vue 实现头像裁剪功能 实现头像裁剪功能通常需要结合第三方库如 cropperjs 或 vue-cropper。以下是两种常见实现方式: 使用 vue-cropper 库 安装依赖: npm i…

vue实现横向导航

vue实现横向导航

vue实现横向导航的方法 使用Flex布局实现横向导航 在Vue中可以通过Flex布局快速实现横向导航。创建一个组件,使用display: flex和flex-direction: row属性使导航项…

vue实现效果

vue实现效果

以下是Vue实现常见效果的几种典型方法,分为不同场景说明: 数据绑定与响应式更新 使用v-model实现表单双向绑定: <template> <input v-model="…

vue实现键盘

vue实现键盘

Vue 实现键盘功能 在Vue中实现键盘功能通常涉及监听键盘事件、处理输入以及渲染虚拟键盘界面。以下是几种常见的实现方式: 监听键盘事件 通过@keydown或@keyup指令监听键盘事件,适用于需…

vue实现画图

vue实现画图

Vue 实现画图功能 在 Vue 中实现画图功能通常需要结合 HTML5 的 <canvas> 元素或第三方库。以下是几种常见的方法: 使用原生 Canvas API 通过 Vue 直接…

vue 实现单选

vue 实现单选

使用 v-model 绑定单选值 在 Vue 中,可以通过 v-model 绑定单选按钮的值。定义一个响应式数据(如 selectedOption),并将其与单选按钮的 value 属性关联。…