当前位置:首页 > 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渲染上下文,并设置默认样式:

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
    };
  }
}

添加清除功能

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

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组件示例:

用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实现tab功能

vue实现tab功能

Vue 实现 Tab 功能的方法 使用动态组件和 v-if 指令 通过 v-if 或 v-show 控制不同 Tab 内容的显示与隐藏。这种方法适合简单的 Tab 切换需求。 <templat…

vue实现桌面功能

vue实现桌面功能

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

vue实现复选功能

vue实现复选功能

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

vue实现插件功能

vue实现插件功能

Vue 插件实现方法 安装与注册插件 在 Vue 项目中,插件通常以独立模块形式存在。通过 Vue.use() 方法注册插件,该方法会自动调用插件的 install 函数。 // 引入插件 imp…

vue留言功能实现

vue留言功能实现

Vue 留言功能实现 数据模型设计 留言功能通常需要以下数据结构: id: 唯一标识符 content: 留言内容 createdAt: 创建时间 author: 作者信息(可选) 示例数据结构:…

vue 实现拍照功能

vue 实现拍照功能

使用HTML5的getUserMedia API实现拍照 在Vue中实现拍照功能可以通过HTML5的getUserMedia API访问摄像头,结合canvas元素捕获图像。 安装依赖(如需处理图像…