当前位置:首页 > VUE

vue实现移动轨迹

2026-01-16 17:48:34VUE

实现移动轨迹的基本思路

在Vue中实现移动轨迹通常涉及监听用户触摸或鼠标事件,记录坐标点,并通过动画或路径绘制展示轨迹。以下是具体实现方法:

监听触摸/鼠标事件

通过@touchstart@touchmove@mousedown@mousemove事件获取坐标:

vue实现移动轨迹

<template>
  <div 
    @touchstart="handleStart"
    @touchmove="handleMove"
    @mousedown="handleStart"
    @mousemove="handleMove"
  ></div>
</template>

<script>
export default {
  data() {
    return {
      points: [],
      isDrawing: false
    }
  },
  methods: {
    handleStart(e) {
      this.isDrawing = true;
      this.addPoint(e);
    },
    handleMove(e) {
      if (this.isDrawing) this.addPoint(e);
    },
    addPoint(e) {
      const point = {
        x: e.touches ? e.touches[0].clientX : e.clientX,
        y: e.touches ? e.touches[0].clientY : e.clientY
      };
      this.points.push(point);
    }
  }
}
</script>

绘制轨迹路径

使用Canvas或SVG动态绘制路径。以Canvas为例:

<template>
  <canvas 
    ref="canvas"
    @touchstart="handleStart"
    @touchmove="handleMove"
    @touchend="handleEnd"
  ></canvas>
</template>

<script>
export default {
  mounted() {
    this.ctx = this.$refs.canvas.getContext('2d');
    this.resizeCanvas();
    window.addEventListener('resize', this.resizeCanvas);
  },
  methods: {
    resizeCanvas() {
      this.$refs.canvas.width = window.innerWidth;
      this.$refs.canvas.height = window.innerHeight;
    },
    handleStart(e) {
      const { x, y } = this.getPosition(e);
      this.ctx.beginPath();
      this.ctx.moveTo(x, y);
    },
    handleMove(e) {
      const { x, y } = this.getPosition(e);
      this.ctx.lineTo(x, y);
      this.ctx.stroke();
    },
    getPosition(e) {
      return {
        x: e.touches ? e.touches[0].clientX : e.clientX,
        y: e.touches ? e.touches[0].clientY : e.clientY
      };
    }
  }
}
</script>

平滑轨迹处理

通过贝塞尔曲线或插值算法平滑轨迹:

vue实现移动轨迹

// 二次贝塞尔曲线示例
drawSmoothPath() {
  this.ctx.beginPath();
  this.ctx.moveTo(this.points[0].x, this.points[0].y);

  for (let i = 1; i < this.points.length - 2; i++) {
    const xc = (this.points[i].x + this.points[i + 1].x) / 2;
    const yc = (this.points[i].y + this.points[i + 1].y) / 2;
    this.ctx.quadraticCurveTo(this.points[i].x, this.points[i].y, xc, yc);
  }

  this.ctx.stroke();
}

轨迹动画回放

使用requestAnimationFrame实现轨迹回放:

animatePath() {
  let index = 0;
  const animate = () => {
    if (index >= this.points.length) return;

    this.ctx.lineTo(this.points[index].x, this.points[index].y);
    this.ctx.stroke();
    index++;
    requestAnimationFrame(animate);
  };
  animate();
}

性能优化建议

对于高频坐标采集,使用防抖或节流控制绘制频率:

import { throttle } from 'lodash';

methods: {
  handleMove: throttle(function(e) {
    this.addPoint(e);
  }, 16) // 约60fps
}

移动端适配注意事项

  • 添加touch-action: noneCSS防止浏览器默认行为
  • 使用passive: true提高滚动性能
  • 处理touchendtouchcancel事件确保轨迹完整

以上方法可根据具体需求组合使用,实现从基础轨迹绘制到高级动画效果的完整解决方案。

标签: 轨迹vue
分享给朋友:

相关文章

vue实现通知

vue实现通知

Vue 实现通知功能的方法 使用 Vue 实现通知功能可以通过多种方式实现,以下是一些常见的方法: 使用 Vue 的全局事件总线 在 Vue 中可以通过事件总线实现通知功能。创建一个全局事件总线,然…

vue拖拽实现

vue拖拽实现

Vue 拖拽实现方法 使用 HTML5 原生拖拽 API HTML5 提供了原生拖拽 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 drop 事…

vue实现swipe

vue实现swipe

Vue实现Swipe功能的方法 使用第三方库(推荐) Vue生态中有多个成熟的轮播/滑动组件库,例如vue-awesome-swiper或swiper/vue。以下是基于swiper/vue的实现示例…

vue实现京东

vue实现京东

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

实现vue message

实现vue message

实现 Vue 消息提示组件 创建 Message 组件 在 src/components 目录下创建 Message.vue 文件,内容如下: <template> <tran…

vue实现项目

vue实现项目

Vue 项目实现指南 环境准备 确保已安装 Node.js(建议版本 14+)和 npm/yarn。通过以下命令检查版本: node -v npm -v 创建 Vue 项目 使用 Vue CLI 快…