当前位置:首页 > VUE

vue消除笔实现

2026-02-20 08:05:38VUE

Vue 消除笔实现

在 Vue 中实现消除笔功能通常涉及 Canvas 绘图和交互处理。以下是实现步骤:

初始化 Canvas 和 Vue 组件

在 Vue 组件中创建 Canvas 元素并设置基础属性:

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

<script>
export default {
  data() {
    return {
      isDrawing: false,
      canvas: null,
      ctx: null
    }
  },
  mounted() {
    this.canvas = this.$refs.canvas;
    this.ctx = this.canvas.getContext('2d');
    this.resizeCanvas();
    window.addEventListener('resize', this.resizeCanvas);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.resizeCanvas);
  }
}
</script>

实现绘图逻辑

添加绘图方法,使用 globalCompositeOperation 控制擦除效果:

methods: {
  resizeCanvas() {
    this.canvas.width = this.canvas.offsetWidth;
    this.canvas.height = this.canvas.offsetHeight;
  },
  startDrawing(e) {
    this.isDrawing = true;
    this.draw(e);
  },
  draw(e) {
    if (!this.isDrawing) return;

    const rect = this.canvas.getBoundingClientRect();
    const x = e.clientX - rect.left;
    const y = e.clientY - rect.top;

    this.ctx.globalCompositeOperation = 'destination-out';
    this.ctx.beginPath();
    this.ctx.arc(x, y, 10, 0, Math.PI * 2);
    this.ctx.fill();
  },
  stopDrawing() {
    this.isDrawing = false;
  }
}

添加背景图像

擦除效果需要可见的背景:

loadImage() {
  const img = new Image();
  img.onload = () => {
    this.ctx.drawImage(img, 0, 0, this.canvas.width, this.canvas.height);
    this.ctx.globalCompositeOperation = 'source-over';
  };
  img.src = '/path/to/image.jpg';
}

优化性能

使用 requestAnimationFrame 优化绘制性能:

draw(e) {
  if (!this.isDrawing) return;

  window.requestAnimationFrame(() => {
    const rect = this.canvas.getBoundingClientRect();
    const x = e.clientX - rect.left;
    const y = e.clientY - rect.top;

    this.ctx.globalCompositeOperation = 'destination-out';
    this.ctx.beginPath();
    this.ctx.arc(x, y, 10, 0, Math.PI * 2);
    this.ctx.fill();
  });
}

移动端支持

添加触摸事件支持:

vue消除笔实现

<canvas
  @touchstart="startDrawing"
  @touchmove="draw"
  @touchend="stopDrawing"
></canvas>
handleTouch(e) {
  e.preventDefault();
  const touch = e.touches[0];
  const mouseEvent = new MouseEvent(
    e.type === 'touchstart' ? 'mousedown' : 
    e.type === 'touchmove' ? 'mousemove' : 'mouseup',
    {
      clientX: touch.clientX,
      clientY: touch.clientY
    }
  );
  this.canvas.dispatchEvent(mouseEvent);
}

注意事项

  1. 擦除效果依赖 globalCompositeOperation 设置为 'destination-out'
  2. 需要预先加载背景图像才能看到擦除效果
  3. 擦除笔大小通过 arc 方法的半径参数调整
  4. 移动端需要阻止默认触摸行为防止页面滚动

完整实现需要考虑更多细节如撤销功能、不同形状的擦除笔等,但以上代码提供了核心功能实现。

标签: vue
分享给朋友:

相关文章

分页实现vue

分页实现vue

分页实现(Vue) 在Vue中实现分页功能通常需要结合前端分页逻辑和后端API支持。以下是两种常见的实现方式: 前端分页实现 适用于数据量较小的情况,直接在客户端完成分页逻辑。 <templ…

vue 动画实现

vue 动画实现

Vue 动画实现方式 Vue 提供了多种方式实现动画效果,主要分为内置组件和第三方库集成。 使用 Vue 内置过渡组件 Vue 的 <transition> 和 <transiti…

vue实现webapp

vue实现webapp

Vue 实现 WebApp 的基本步骤 环境准备 确保已安装 Node.js 和 npm/yarn。通过 Vue CLI 或 Vite 创建项目: npm install -g @vue/cli v…

vue实现拖动

vue实现拖动

Vue实现拖动的常用方法 在Vue中实现拖动功能通常需要结合HTML5的拖放API或第三方库。以下是几种常见实现方式: 使用HTML5原生拖放API HTML5提供了原生拖放API,可以通过drag…

vue实现pc

vue实现pc

Vue 实现 PC 端应用开发 Vue.js 是一个流行的前端框架,适用于构建 PC 端 Web 应用。以下是关键步骤和最佳实践: 项目初始化 使用 Vue CLI 或 Vite 创建项目:…

vue实现京东

vue实现京东

Vue 实现京东电商网站的关键步骤 项目搭建与基础配置 使用 Vue CLI 或 Vite 初始化项目,安装 Vue Router 管理路由,Vuex/Pinia 管理状态。配置基础 UI 框架如 E…