当前位置:首页 > JavaScript

js实现动画路径

2026-03-15 18:04:58JavaScript

使用 CSS 和 JavaScript 实现动画路径

通过 CSS animation 结合 @keyframes 可以定义元素沿路径移动的动画。JavaScript 用于动态控制动画的启停或参数调整。

const element = document.getElementById('animated-element');
element.style.animation = 'movePath 3s linear infinite';

// 动态修改路径
function updatePath(newPath) {
  const styleSheet = document.styleSheets[0];
  styleSheet.insertRule(`
    @keyframes movePath {
      0% { transform: translate(0, 0); }
      100% { transform: translate(${newPath.x}px, ${newPath.y}px); }
    }
  `, styleSheet.cssRules.length);
}

使用 SVG 和 JavaScript 实现路径动画

SVG 原生支持路径动画,通过 <animateMotion> 标签实现:

js实现动画路径

<svg width="200" height="200">
  <path id="path" d="M10,50 Q50,10 90,50 T130,50" fill="none" stroke="black"/>
  <circle r="5" fill="red">
    <animateMotion dur="3s" repeatCount="indefinite">
      <mpath xlink:href="#path"/>
    </animateMotion>
  </circle>
</svg>

JavaScript 动态更新路径:

document.getElementById('path').setAttribute('d', 'M10,10 L100,100 Q150,50 200,10');

使用 Canvas 绘制路径动画

通过 Canvas 2D API 逐帧绘制路径动画:

js实现动画路径

const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
let progress = 0;

function animate() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);

  // 绘制路径
  ctx.beginPath();
  ctx.moveTo(10, 10);
  ctx.quadraticCurveTo(100, 100, 200, 50);
  ctx.stroke();

  // 计算动画点位置
  const point = getPointOnQuadraticCurve(10,10, 100,100, 200,50, progress);
  ctx.fillRect(point.x - 5, point.y - 5, 10, 10);

  progress = (progress + 0.01) % 1;
  requestAnimationFrame(animate);
}

function getPointOnQuadraticCurve(x1,y1, x2,y2, x3,y3, t) {
  const mt = 1 - t;
  return {
    x: mt*mt*x1 + 2*mt*t*x2 + t*t*x3,
    y: mt*mt*y1 + 2*mt*t*y2 + t*t*y3
  };
}

animate();

使用 GSAP 库实现高级路径动画

GSAP 提供强大的路径动画功能:

gsap.to("#logo", {
  duration: 5,
  motionPath: {
    path: "M10,50 Q50,10 90,50 T130,50",
    align: "#path",
    autoRotate: true
  },
  repeat: -1,
  ease: "power1.inOut"
});

使用 Web Animation API

现代浏览器支持的 Web Animation API 也可实现路径动画:

const element = document.getElementById('fly');
const keyframes = [
  { transform: 'translate(0, 0)' },
  { transform: 'translate(300px, 100px)' }
];
const options = {
  duration: 3000,
  iterations: Infinity,
  easing: 'cubic-bezier(0.42, 0, 0.58, 1)'
};
element.animate(keyframes, options);

标签: 路径动画
分享给朋友:

相关文章

uniapp 退场动画

uniapp 退场动画

实现退场动画的方法 在UniApp中实现退场动画可以通过CSS动画、Vue过渡效果或第三方动画库完成。以下是几种常见方法: 使用CSS动画 通过定义@keyframes和animation属性实现退…

vue实现过渡动画

vue实现过渡动画

Vue 过渡动画实现方法 Vue 提供了内置的 <transition> 和 <transition-group> 组件,用于处理进入/离开过渡和列表动画。以下是几种常见的实现…

vue实现首页动画

vue实现首页动画

Vue 实现首页动画的方法 在 Vue 中实现首页动画可以通过多种方式,包括使用 CSS 动画、Vue 过渡效果、第三方动画库等。以下是一些常见的实现方法。 使用 CSS 动画 通过 Vue 的 v…

vue动画怎么实现

vue动画怎么实现

Vue动画的实现方式 Vue提供了多种方式来实现动画效果,包括内置的过渡和动画系统,以及第三方动画库的集成。以下是几种常见的实现方法: 使用Vue的过渡系统 Vue的<transition&…

vue实现jquery动画

vue实现jquery动画

Vue 实现 jQuery 动画的方法 Vue 本身提供了过渡和动画的支持,可以通过内置的 <transition> 和 <transition-group> 组件实现类似 j…

vue实现消息动画

vue实现消息动画

Vue 实现消息动画的方法 使用 Vue Transition 组件 Vue 内置的 <transition> 组件可以方便地实现消息动画效果。通过添加 CSS 过渡或动画类名,可以实现平…