当前位置:首页 > 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 实现滑动效果 使用 CSS 过渡和动画 通过 Vue 的 transition 组件结合 CSS 过渡或动画实现滑动效果。适用于简单的元素入场/离场滑动。 <template>…

vue双向实现

vue双向实现

Vue 双向数据绑定的实现原理 Vue 的双向数据绑定主要通过 v-model 指令实现,其核心是结合了数据劫持(Object.defineProperty 或 Proxy)和发布-订阅模式。 数…

vue实现静态查询

vue实现静态查询

Vue 实现静态查询 在 Vue 中实现静态查询通常指不依赖后端接口,直接在前端处理数据筛选或搜索功能。以下是几种常见实现方式: 使用计算属性过滤数据 通过计算属性对静态数据进行筛选,适合简单查询场…

vue手动实现弹窗

vue手动实现弹窗

实现弹窗组件的基本结构 在Vue中手动实现弹窗组件,需要创建一个独立的弹窗组件,并通过props控制其显示与隐藏。弹窗组件通常包含遮罩层、内容区域和关闭按钮。 <template>…

课程表vue实现

课程表vue实现

实现课程表的Vue组件 创建课程表需要设计数据结构、布局和交互逻辑。以下是一个基于Vue 3的实现方案: 数据结构设计 const timetableData = ref([ { ti…

vue实现文字播放栏

vue实现文字播放栏

Vue 实现文字播放栏(跑马灯效果) 方法一:使用 CSS 动画 + Vue 数据绑定 通过 CSS 的 @keyframes 实现动画效果,结合 Vue 的动态数据绑定控制内容。 <t…