当前位置:首页 > 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 实现好评弹框可以通过组件化方式完成,结合动态数据绑定和事件处理。以下是具体实现步骤: 1. 创建弹框组件 新建一个 RatingDialog.vue…

vue实现压缩上传文件

vue实现压缩上传文件

压缩上传文件的实现方法 在Vue中实现文件压缩和上传功能,可以通过以下步骤完成。该方法结合了前端文件压缩库和HTTP请求,确保文件在上传前被有效压缩。 安装必要的依赖 需要使用compressorj…

vue实现3d宇宙

vue实现3d宇宙

Vue实现3D宇宙效果 使用Three.js库 Three.js是一个强大的JavaScript 3D库,可以轻松集成到Vue项目中。安装Three.js依赖: npm install three…

vue实现上移下移插件

vue实现上移下移插件

实现上移下移功能的 Vue 插件 在 Vue 中实现列表项的上移和下移功能,可以通过自定义指令或组件封装的方式完成。以下是两种常见实现方式: 基于数组操作的通用方法 通过操作数组索引实现元素位置交换…

vue实现ai

vue实现ai

Vue 实现 AI 功能 Vue.js 可以通过集成第三方 AI 服务或本地 AI 模型来实现 AI 功能。以下是几种常见的方法: 集成第三方 AI API 使用 Vue 调用如 OpenAI、Go…

实现vue组件

实现vue组件

Vue 组件的基本实现 Vue 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。以下是实现 Vue 组件的几种方式: 单文件组件 (SFC) 使用 .vue 文件格式,…