当前位置:首页 > VUE

用vue实现手绘功能

2026-01-21 02:07:53VUE

实现手绘功能的核心思路

在Vue中实现手绘功能主要依赖HTML5的Canvas API,通过监听鼠标或触摸事件记录轨迹并实时绘制。以下分步骤说明具体实现方法。

创建Canvas画布

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

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

初始化画布上下文

mounted生命周期中初始化Canvas的2D渲染上下文,并设置默认样式:

用vue实现手绘功能

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

处理绘制逻辑

定义绘图状态变量和事件处理函数,实现以下功能:

data() {
  return {
    isDrawing: false,
    lastX: 0,
    lastY: 0
  }
},
methods: {
  startDrawing(e) {
    this.isDrawing = true;
    const pos = this.getPosition(e);
    [this.lastX, this.lastY] = [pos.x, pos.y];
  },

  draw(e) {
    if (!this.isDrawing) return;
    const pos = this.getPosition(e);
    this.ctx.beginPath();
    this.ctx.moveTo(this.lastX, this.lastY);
    this.ctx.lineTo(pos.x, pos.y);
    this.ctx.stroke();
    [this.lastX, this.lastY] = [pos.x, pos.y];
  },

  stopDrawing() {
    this.isDrawing = false;
  },

  getPosition(e) {
    const rect = this.canvas.getBoundingClientRect();
    return {
      x: (e.clientX || e.touches[0].clientX) - rect.left,
      y: (e.clientY || e.touches[0].clientY) - rect.top
    };
  }
}

添加清除功能

扩展功能以支持清除画布:

用vue实现手绘功能

methods: {
  clearCanvas() {
    this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
  }
}

响应式调整画布大小

监听窗口变化事件,动态调整Canvas尺寸:

mounted() {
  window.addEventListener('resize', this.handleResize);
},
methods: {
  handleResize() {
    this.canvas.width = window.innerWidth;
    this.canvas.height = window.innerHeight;
  }
},
beforeDestroy() {
  window.removeEventListener('resize', this.handleResize);
}

完整组件示例

整合上述代码的完整Vue组件示例:

<template>
  <div>
    <canvas 
      ref="canvas"
      @mousedown="startDrawing"
      @mousemove="draw"
      @mouseup="stopDrawing"
      @touchstart="startDrawing"
      @touchmove="draw"
      @touchend="stopDrawing">
    </canvas>
    <button @click="clearCanvas">Clear</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isDrawing: false,
      lastX: 0,
      lastY: 0
    }
  },
  mounted() {
    this.canvas = this.$refs.canvas;
    this.ctx = this.canvas.getContext('2d');
    this.handleResize();
    window.addEventListener('resize', this.handleResize);
  },
  methods: {
    startDrawing(e) {
      this.isDrawing = true;
      const pos = this.getPosition(e);
      [this.lastX, this.lastY] = [pos.x, pos.y];
    },
    draw(e) {
      if (!this.isDrawing) return;
      const pos = this.getPosition(e);
      this.ctx.beginPath();
      this.ctx.moveTo(this.lastX, this.lastY);
      this.ctx.lineTo(pos.x, pos.y);
      this.ctx.stroke();
      [this.lastX, this.lastY] = [pos.x, pos.y];
    },
    stopDrawing() {
      this.isDrawing = false;
    },
    clearCanvas() {
      this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
    },
    getPosition(e) {
      const rect = this.canvas.getBoundingClientRect();
      return {
        x: (e.clientX || e.touches[0].clientX) - rect.left,
        y: (e.clientY || e.touches[0].clientY) - rect.top
      };
    },
    handleResize() {
      this.canvas.width = window.innerWidth;
      this.canvas.height = window.innerHeight;
    }
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.handleResize);
  }
}
</script>

扩展功能建议

  • 颜色选择器:添加<input type="color">绑定到ctx.strokeStyle
  • 笔刷粗细:通过滑块控制ctx.lineWidth
  • 撤销功能:使用数组记录绘制路径实现多步撤销
  • 保存图片:通过canvas.toDataURL()导出绘制结果

标签: 手绘功能
分享给朋友:

相关文章

原生实现vue功能

原生实现vue功能

原生JavaScript实现Vue核心功能 使用原生JavaScript模拟Vue的核心功能需要理解响应式数据绑定、虚拟DOM和组件系统等概念。以下是关键实现方法: 响应式数据系统 通过Object…

vue实现收货功能

vue实现收货功能

Vue实现收货功能的基本思路 收货功能通常涉及前端界面交互、后端数据更新和状态管理。在Vue中,可以通过组件化开发实现这一功能,结合API调用与状态管理工具(如Vuex或Pinia)。 创建收货按…

vue 实现筛选功能

vue 实现筛选功能

实现筛选功能的基本思路 在Vue中实现筛选功能通常涉及以下几个核心步骤:数据绑定、筛选逻辑处理、结果展示。以下是具体实现方法。 数据准备 准备需要筛选的数据源,通常是一个数组,可以存储在Vue组件的…

前端实现拖拽功能vue

前端实现拖拽功能vue

使用 HTML5 拖放 API 在 Vue 中可以通过 HTML5 的原生拖放 API 实现拖拽功能。需要监听 dragstart、dragover 和 drop 事件。 <template…

php实现打印功能

php实现打印功能

PHP 实现打印功能的方法 PHP 可以通过多种方式实现打印功能,具体取决于打印的目标(如打印机、PDF、网页等)和需求(如直接打印、生成打印预览等)。以下是几种常见的实现方式: 使用 JavaSc…

js实现复制功能

js实现复制功能

使用 document.execCommand 方法 这种方法适用于较旧的浏览器,但在现代浏览器中可能被逐步淘汰。通过创建一个临时的 textarea 元素,将文本内容放入其中,然后执行复制命令。…