当前位置:首页 > VUE

vue实现canvas拖拽

2026-02-18 20:10:02VUE

Vue 实现 Canvas 拖拽功能

基础 Canvas 设置

在 Vue 中创建一个 Canvas 元素,并初始化绘图上下文:

<template>
  <canvas ref="canvas" @mousedown="startDrag" @mousemove="onDrag" @mouseup="endDrag"></canvas>
</template>

<script>
export default {
  data() {
    return {
      isDragging: false,
      selectedObject: null,
      objects: [
        { x: 100, y: 100, width: 50, height: 50, color: 'red' }
      ]
    }
  }
}
</script>

绘制对象到 Canvas

在 mounted 钩子中初始化 Canvas 并绘制对象:

vue实现canvas拖拽

mounted() {
  this.ctx = this.$refs.canvas.getContext('2d');
  this.drawObjects();
},
methods: {
  drawObjects() {
    this.ctx.clearRect(0, 0, this.$refs.canvas.width, this.$refs.canvas.height);
    this.objects.forEach(obj => {
      this.ctx.fillStyle = obj.color;
      this.ctx.fillRect(obj.x, obj.y, obj.width, obj.height);
    });
  }
}

实现拖拽逻辑

添加拖拽相关的事件处理方法:

methods: {
  startDrag(e) {
    const mouseX = e.offsetX;
    const mouseY = e.offsetY;

    this.selectedObject = this.objects.find(obj => 
      mouseX >= obj.x && 
      mouseX <= obj.x + obj.width &&
      mouseY >= obj.y && 
      mouseY <= obj.y + obj.height
    );

    if (this.selectedObject) {
      this.isDragging = true;
      this.dragOffsetX = mouseX - this.selectedObject.x;
      this.dragOffsetY = mouseY - this.selectedObject.y;
    }
  },

  onDrag(e) {
    if (!this.isDragging || !this.selectedObject) return;

    this.selectedObject.x = e.offsetX - this.dragOffsetX;
    this.selectedObject.y = e.offsetY - this.dragOffsetY;
    this.drawObjects();
  },

  endDrag() {
    this.isDragging = false;
    this.selectedObject = null;
  }
}

响应式调整 Canvas 尺寸

添加窗口大小变化时的处理:

vue实现canvas拖拽

mounted() {
  this.resizeCanvas();
  window.addEventListener('resize', this.resizeCanvas);
},
beforeDestroy() {
  window.removeEventListener('resize', this.resizeCanvas);
},
methods: {
  resizeCanvas() {
    const canvas = this.$refs.canvas;
    canvas.width = canvas.clientWidth;
    canvas.height = canvas.clientHeight;
    this.drawObjects();
  }
}

性能优化

对于复杂场景,可以使用 requestAnimationFrame 优化渲染:

methods: {
  onDrag(e) {
    if (!this.isDragging || !this.selectedObject) return;

    this.selectedObject.x = e.offsetX - this.dragOffsetX;
    this.selectedObject.y = e.offsetY - this.dragOffsetY;

    if (!this.rafId) {
      this.rafId = requestAnimationFrame(() => {
        this.drawObjects();
        this.rafId = null;
      });
    }
  }
}

触摸屏支持

添加触摸事件处理:

<canvas 
  ref="canvas" 
  @mousedown="startDrag" 
  @mousemove="onDrag" 
  @mouseup="endDrag"
  @touchstart="handleTouchStart"
  @touchmove="handleTouchMove"
  @touchend="handleTouchEnd"
></canvas>
methods: {
  handleTouchStart(e) {
    e.preventDefault();
    const touch = e.touches[0];
    const rect = this.$refs.canvas.getBoundingClientRect();
    this.startDrag({
      offsetX: touch.clientX - rect.left,
      offsetY: touch.clientY - rect.top
    });
  },

  handleTouchMove(e) {
    e.preventDefault();
    const touch = e.touches[0];
    const rect = this.$refs.canvas.getBoundingClientRect();
    this.onDrag({
      offsetX: touch.clientX - rect.left,
      offsetY: touch.clientY - rect.top
    });
  },

  handleTouchEnd() {
    this.endDrag();
  }
}

这个实现提供了完整的 Canvas 拖拽功能,包括鼠标和触摸支持,以及性能优化。可以根据具体需求调整对象检测逻辑或添加更多交互功能。

标签: 拖拽vue
分享给朋友:

相关文章

vue实现截图

vue实现截图

Vue 实现截图的方法 使用 html2canvas 库 安装 html2canvas 库: npm install html2canvas 在 Vue 组件中使用: import html2ca…

vue实现批量

vue实现批量

Vue 实现批量操作的方法 在 Vue 中实现批量操作通常涉及选择多个项目并执行统一处理,例如批量删除、批量更新等。以下是几种常见实现方式: 表格多选批量操作 使用 el-table 配合复选框实…

vue 实现关闭

vue 实现关闭

Vue 实现关闭功能的方法 在Vue中实现关闭功能通常涉及组件销毁、模态框隐藏或路由跳转等场景。以下是几种常见实现方式: 销毁当前组件 使用v-if或v-show控制组件显示/隐藏,适用于局部组件…

vue实现cs

vue实现cs

Vue 实现 CS(Client-Side)渲染的常见方法 Vue.js 是一个用于构建用户界面的渐进式框架,默认采用客户端渲染(CSR)。以下是几种常见的实现方式: 使用 Vue CLI 创建项目…

vue实现前端

vue实现前端

Vue 实现前端的核心方法与步骤 安装 Vue 项目 使用 Vue CLI 或 Vite 创建项目。Vue CLI 是传统构建工具,Vite 是新一代轻量级工具。 npm init vue@la…

vue实现曲线

vue实现曲线

Vue 实现曲线的方法 在 Vue 中实现曲线可以通过多种方式,包括使用 SVG、Canvas 或第三方库如 D3.js、ECharts 等。以下是几种常见的方法: 使用 SVG 绘制曲线 SVG…