当前位置:首页 > 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实现桌面功能

Vue 实现桌面功能的方法 Vue.js 可以通过结合 Electron 或 NW.js 等框架实现桌面应用开发。以下是几种常见的方法: 使用 Vue 与 Electron 结合 Electron…

vue实现产品使用功能

vue实现产品使用功能

Vue 实现产品功能的关键方法 组件化开发 使用Vue的单文件组件(.vue)将产品功能模块化,每个功能拆分为独立组件。例如产品列表、详情页、购物车等可分别封装为ProductList.vue、Pro…

php怎么实现登录功能

php怎么实现登录功能

数据库准备 创建用户表存储登录信息,通常包括用户名、密码(需加密)等字段。示例SQL: CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY…

vue怎么实现功能

vue怎么实现功能

Vue 是一个渐进式 JavaScript 框架,常用于构建用户界面。以下是一些常见的功能实现方法: 数据绑定 Vue 通过 v-model 实现双向数据绑定,适用于表单输入等场景: <te…

vue实现复选功能

vue实现复选功能

Vue 实现复选功能 基础实现 使用 v-model 绑定到数组实现多选功能。当复选框被选中时,其 value 会被添加到数组中;取消选中时,会从数组中移除。 <template>…

vue实现功能列表

vue实现功能列表

Vue 实现功能列表的方法 在 Vue 中实现功能列表通常涉及数据绑定、列表渲染、事件处理和状态管理。以下是几种常见的方法: 数据驱动列表渲染 使用 v-for 指令可以轻松实现列表渲染。假设有一个…