当前位置:首页 > 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中实现打字机效果可以通过动态更新文本内容并添加延迟来实现。以下是几种常见的实现方法: 使用setInterval实现 <template> <d…

vue如何实现放大缩小

vue如何实现放大缩小

Vue实现放大缩小功能的方法 在Vue中实现元素的放大缩小功能,可以通过CSS变换、第三方库或直接操作DOM样式来实现。以下是几种常见的方法: 使用CSS transform属性 通过绑定Vue的d…

vue实现无线滚动列表

vue实现无线滚动列表

无限滚动列表的实现方法 在Vue中实现无限滚动列表通常结合监听滚动事件和动态加载数据。以下是几种常见方法: 使用自定义指令 创建自定义指令v-infinite-scroll监听滚动事件:…

vue自己实现下拉导航

vue自己实现下拉导航

实现下拉导航的基本思路 在Vue中实现下拉导航,可以通过结合Vue的响应式数据和事件绑定特性来完成。主要思路是利用v-show或v-if控制下拉菜单的显示与隐藏,并通过鼠标事件或点击事件触发状态变化。…

vue实现生成二维码

vue实现生成二维码

使用qrcode.vue库生成二维码 安装qrcode.vue库: npm install qrcode.vue --save 在Vue组件中使用: <template> <…

vue实现功能

vue实现功能

Vue 功能实现方法 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是实现常见功能的几种方法: 数据绑定与响应式更新 在 Vue 中,数据绑定通过 v-model…