当前位置:首页 > JavaScript

js实现轨迹

2026-03-14 05:31:23JavaScript

js实现轨迹动画

使用requestAnimationFrame实现平滑动画效果,通过不断更新元素的位置属性形成轨迹运动。以下是基础实现代码:

let startTime = null;
const duration = 2000; // 动画持续时间(ms)
const element = document.getElementById('movingElement');

function animate(timestamp) {
  if (!startTime) startTime = timestamp;
  const progress = (timestamp - startTime) / duration;

  if (progress < 1) {
    const x = progress * 500; // 水平移动距离
    const y = Math.sin(progress * Math.PI * 2) * 50; // 垂直正弦波动
    element.style.transform = `translate(${x}px, ${y}px)`;
    requestAnimationFrame(animate);
  }
}

requestAnimationFrame(animate);

贝塞尔曲线轨迹

使用三次贝塞尔曲线创建复杂轨迹路径,需要计算曲线上的点坐标:

js实现轨迹

function cubicBezier(t, p0, p1, p2, p3) {
  const mt = 1 - t;
  return {
    x: mt*mt*mt*p0.x + 3*mt*mt*t*p1.x + 3*mt*t*t*p2.x + t*t*t*p3.x,
    y: mt*mt*mt*p0.y + 3*mt*mt*t*p1.y + 3*mt*t*t*p2.y + t*t*t*p3.y
  };
}

const controlPoints = [
  {x:0, y:0},   // 起点
  {x:100, y:50}, // 控制点1
  {x:200, y:-50},// 控制点2
  {x:300, y:0}   // 终点
];

SVG路径轨迹

对于更复杂的轨迹,可以结合SVG路径和getPointAtLength方法:

js实现轨迹

const path = document.getElementById('motionPath');
const pathLength = path.getTotalLength();
let position = 0;

function followPath() {
  position = (position + 1) % pathLength;
  const point = path.getPointAtLength(position);
  element.style.left = point.x + 'px';
  element.style.top = point.y + 'px';
  requestAnimationFrame(followPath);
}

物理轨迹模拟

实现抛物线等物理运动轨迹,需要考虑初速度和重力加速度:

let velocityX = 2;
let velocityY = -8;
const gravity = 0.2;
let posX = 0;
let posY = 0;

function physicsAnimation() {
  velocityY += gravity;
  posX += velocityX;
  posY += velocityY;

  if (posY > window.innerHeight) {
    posY = window.innerHeight;
    velocityY *= -0.8; // 弹性系数
  }

  element.style.left = posX + 'px';
  element.style.top = posY + 'px';
  requestAnimationFrame(physicsAnimation);
}

轨迹记录与回放

记录移动轨迹数据并实现回放功能:

const recordedPositions = [];
let isRecording = true;

function recordPosition(x, y) {
  if (isRecording) {
    recordedPositions.push({x, y});
  }
}

function replayAnimation(index = 0) {
  if (index < recordedPositions.length) {
    const pos = recordedPositions[index];
    element.style.left = pos.x + 'px';
    element.style.top = pos.y + 'px';
    setTimeout(() => replayAnimation(index + 1), 16);
  }
}

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

相关文章

js实现拖拽

js实现拖拽

实现拖拽的基本步骤 使用JavaScript实现拖拽功能需要监听鼠标事件,包括mousedown、mousemove和mouseup。以下是实现的基本逻辑: const draggableEleme…

js 实现分页

js 实现分页

实现分页的基本逻辑 分页功能通常需要后端返回数据总量或总页数,前端根据当前页码和每页条数截取对应数据。以下是一个基于JavaScript的简单分页实现方案: 前端分页实现 假设已有从后端获取的完整数…

js实现图

js实现图

JavaScript 实现图的常用方法 在 JavaScript 中,图(Graph)可以通过多种方式实现,常见的包括邻接表(Adjacency List)和邻接矩阵(Adjacency Matrix…

节流js实现

节流js实现

节流(Throttle)的实现原理 节流是一种限制函数执行频率的技术,确保函数在一定时间间隔内最多执行一次。适用于高频触发事件(如滚动、输入、窗口调整等)的场景。 基础实现方式 使用时间戳判断是否执…

js 实现拖拽

js 实现拖拽

实现拖拽的基本步骤 在JavaScript中实现拖拽功能需要监听几个关键事件:mousedown、mousemove和mouseup。以下是实现的基本逻辑。 监听目标元素的mousedown事件,记…

js 实现全屏

js 实现全屏

使用 requestFullscreen 方法 通过调用元素的 requestFullscreen 方法可以实现全屏。该方法兼容现代浏览器,但不同浏览器可能需要前缀。 const element =…