当前位置:首页 > VUE

vue消除笔实现

2026-01-19 15:50:22VUE

Vue 消除笔实现方案

核心思路

消除笔功能通常基于 Canvas 或 SVG 实现,通过记录用户绘制路径并与底层图像进行交互。Vue 作为框架主要负责状态管理和组件封装。

实现步骤

基础 Canvas 绘制

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

<script>
export default {
  data() {
    return {
      isDrawing: false,
      lastX: 0,
      lastY: 0
    }
  },
  methods: {
    startDrawing(e) {
      this.isDrawing = true;
      [this.lastX, this.lastY] = [e.offsetX, e.offsetY];
    },
    draw(e) {
      if (!this.isDrawing) return;
      const ctx = this.$refs.canvas.getContext('2d');
      ctx.globalCompositeOperation = 'destination-out'; // 关键消除模式
      ctx.beginPath();
      ctx.arc(e.offsetX, e.offsetY, 10, 0, Math.PI * 2);
      ctx.fill();
    },
    stopDrawing() {
      this.isDrawing = false;
    }
  }
}
</script>

进阶优化方案

vue消除笔实现

  1. 路径平滑处理 使用贝塞尔曲线替代直线连接:

    ctx.quadraticCurveTo(
    controlX, 
    controlY,
    e.offsetX,
    e.offsetY
    );
  2. 压力感应支持 通过监听指针事件获取压力值:

    vue消除笔实现

    window.PointerEvent && canvas.addEventListener('pointermove', (e) => {
    const pressure = e.pressure || 1;
    ctx.lineWidth = pressure * 20;
    });
  3. 撤销/重做功能 使用命令模式记录操作历史:

    
    const history = [];
    const redoStack = [];

function saveState() { history.push(canvas.toDataURL()); }


4. 移动端适配
添加触摸事件支持:
```javascript
canvas.addEventListener('touchmove', (e) => {
  e.preventDefault();
  const touch = e.touches[0];
  const mouseEvent = new MouseEvent('mousemove', {
    clientX: touch.clientX,
    clientY: touch.clientY
  });
  canvas.dispatchEvent(mouseEvent);
});

性能优化建议

  • 使用 requestAnimationFrame 节流绘制操作
  • 对大型画布采用分层渲染策略
  • 实现脏矩形算法减少重绘区域
  • 考虑使用 OffscreenCanvas 进行后台处理

完整组件示例

<template>
  <div class="eraser-container">
    <canvas 
      ref="canvas"
      :width="width"
      :height="height"
      @mousedown="startDrawing"
      @mousemove="throttledDraw"
      @mouseup="stopDrawing"
      @touchstart="handleTouch"
      @touchmove="handleTouch"
    />
    <div class="controls">
      <button @click="undo">撤销</button>
      <input type="range" v-model="brushSize" min="1" max="50">
    </div>
  </div>
</template>

<script>
import { throttle } from 'lodash';

export default {
  props: {
    width: { type: Number, default: 800 },
    height: { type: Number, default: 600 },
    imageSrc: String
  },
  data() {
    return {
      brushSize: 10,
      history: [],
      redoStack: []
    };
  },
  mounted() {
    this.initCanvas();
    this.throttledDraw = throttle(this.draw, 16);
  },
  methods: {
    initCanvas() {
      const ctx = this.$refs.canvas.getContext('2d');
      if (this.imageSrc) {
        const img = new Image();
        img.onload = () => {
          ctx.drawImage(img, 0, 0, this.width, this.height);
          this.saveState();
        };
        img.src = this.imageSrc;
      }
    }
  }
};
</script>

注意事项

  • 消除效果本质是通过 globalCompositeOperation 设置混合模式
  • 需要处理跨浏览器事件兼容性问题
  • 高DPI设备需考虑像素比例适配
  • 复杂场景建议使用 fabric.js 或 konva.js 等专业库

标签: vue
分享给朋友:

相关文章

vue实现条件判断

vue实现条件判断

Vue 条件判断实现方法 Vue 提供了多种方式实现条件判断,可以根据不同场景选择合适的方式。 v-if / v-else-if / v-else 这是 Vue 中最常用的条件渲染指令,根据表达式的…

vue实现visual

vue实现visual

Vue 实现可视化(Visual)的常见方法 在 Vue 中实现可视化通常涉及数据可视化库的集成,以下是几种主流方案: 使用 ECharts ECharts 是一个强大的 JavaScript 图表…

vue实现摘要

vue实现摘要

Vue 实现摘要的方法 在 Vue 中实现文本摘要功能通常涉及截取文本的前部分内容并添加省略号。可以通过计算属性、过滤器或自定义指令来实现。 计算属性实现 在 Vue 组件中定义一个计算属性,用于截…

antd vue 实现

antd vue 实现

使用 Ant Design Vue 实现功能 Ant Design Vue 是基于 Vue.js 的 UI 组件库,提供了丰富的组件和设计规范。以下是一些常见功能的实现方法。 安装 Ant Desi…

vue router 实现

vue router 实现

Vue Router 的实现 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是 Vue Router 的核心实现方式。 安装 Vue Router 通过…

vue grid实现

vue grid实现

Vue Grid 实现方法 在 Vue 中实现网格布局可以通过多种方式完成,包括使用原生 CSS Grid、第三方组件库或自定义组件。以下是几种常见方法: 使用原生 CSS Grid 通过 Vue…