当前位置:首页 > 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> 标签实现:

<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 逐帧绘制路径动画:

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 也可实现路径动画:

js实现动画路径

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);

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

相关文章

vue组件实现动画

vue组件实现动画

Vue 组件实现动画的方法 Vue 提供了多种方式实现组件动画,主要包括内置的 <transition> 和 <transition-group> 组件,以及结合 CSS 或…

vue实现列表动画

vue实现列表动画

Vue 列表动画的实现方法 Vue 提供了内置的过渡和动画系统,可以通过 <transition-group> 组件实现列表动画效果。以下是几种常见的实现方式: 使用 <trans…

vue使用animate实现动画

vue使用animate实现动画

使用 Animate.css 在 Vue 中实现动画 在 Vue 项目中集成 Animate.css 可以为元素添加丰富的 CSS 动画效果。Animate.css 是一个预定义的 CSS 动画库,无…

使用Vue实现洗牌动画

使用Vue实现洗牌动画

使用CSS和Vue实现洗牌动画 洗牌动画可以通过CSS过渡和Vue的动态数据绑定实现。以下是实现步骤: 准备数据 data() { return { cards: [ {…

vue实现投币动画效果

vue实现投币动画效果

实现投币动画效果的方法 在Vue中实现投币动画效果,可以通过CSS动画和Vue的过渡系统结合完成。以下是几种常见的实现方式: 使用CSS动画和Vue过渡 定义一个硬币组件,通过CSS动画控制…

vue实现切换路由动画

vue实现切换路由动画

路由动画的基本实现 在Vue中实现路由切换动画,通常结合<transition>或<transition-group>组件与Vue Router完成。以下是基础实现方式: &…