当前位置:首页 > JavaScript

js实现动画路径

2026-02-02 17:47:35JavaScript

使用 CSS 和 JavaScript 实现动画路径

CSS 的 animation@keyframes 可以结合 JavaScript 动态控制动画路径。通过定义关键帧,元素可以沿着预设路径移动。

@keyframes movePath {
  0% { transform: translate(0, 0); }
  50% { transform: translate(100px, 50px); }
  100% { transform: translate(200px, 0); }
}

.element {
  animation: movePath 2s linear infinite;
}

JavaScript 可以动态修改路径或控制动画的启停:

const element = document.querySelector('.element');
element.style.animation = 'movePath 3s ease-in-out infinite';

使用 SVG 和 JavaScript 实现路径动画

SVG 的 <path> 元素结合 getTotalLength()getPointAtLength() 方法可以实现精确路径动画。

js实现动画路径

<svg width="300" height="200">
  <path id="path" d="M10,100 Q150,200 290,100" fill="none" stroke="gray"/>
  <circle id="circle" r="10" fill="blue"/>
</svg>

JavaScript 控制元素沿路径移动:

const path = document.getElementById('path');
const circle = document.getElementById('circle');
const pathLength = path.getTotalLength();

let progress = 0;
function animate() {
  progress = (progress + 0.005) % 1;
  const point = path.getPointAtLength(progress * pathLength);
  circle.setAttribute('cx', point.x);
  circle.setAttribute('cy', point.y);
  requestAnimationFrame(animate);
}
animate();

使用 Canvas 绘制路径动画

Canvas 提供更灵活的路径绘制能力,适合复杂动态路径。

js实现动画路径

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

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

  // 绘制路径
  ctx.beginPath();
  ctx.moveTo(0, 100);
  ctx.quadraticCurveTo(150, 200, 300, 100);
  ctx.strokeStyle = 'gray';
  ctx.stroke();

  // 绘制移动元素
  const x = position * 300;
  const y = 100 + Math.sin(position * Math.PI) * 100;
  ctx.beginPath();
  ctx.arc(x, y, 10, 0, Math.PI * 2);
  ctx.fillStyle = 'blue';
  ctx.fill();

  position = (position + 0.005) % 1;
  requestAnimationFrame(drawPath);
}
drawPath();

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

GSAP 的 MotionPathPlugin 可以简化复杂路径动画的实现。

<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.11.4/gsap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.11.4/MotionPathPlugin.min.js"></script>

<div id="box"></div>

JavaScript 实现:

gsap.registerPlugin(MotionPathPlugin);

gsap.to("#box", {
  duration: 3,
  repeat: -1,
  motionPath: {
    path: [
      {x: 0, y: 0},
      {x: 100, y: 100},
      {x: 200, y: 0}
    ],
    curviness: 1.5
  },
  ease: "power1.inOut"
});

使用 Web Animation API

现代浏览器支持的 Web Animation API 可以直接控制元素运动。

const element = document.getElementById('animated');
const keyframes = [
  { transform: 'translate(0, 0)' },
  { transform: 'translate(100px, 50px)' },
  { transform: 'translate(200px, 0)' }
];

const options = {
  duration: 2000,
  iterations: Infinity,
  direction: 'alternate',
  easing: 'ease-in-out'
};

const animation = element.animate(keyframes, options);
animation.play();

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

相关文章

css3制作简单动画

css3制作简单动画

CSS3 制作简单动画的方法 CSS3 提供了多种方式制作动画,主要通过 transition、transform 和 animation 属性实现。以下是几种常见的实现方法。 使用 transit…

jquery 动画

jquery 动画

jQuery 动画基础 jQuery 提供了一系列动画方法,用于实现元素的动态效果,如淡入淡出、滑动、自定义动画等。 常用动画方法 fadeIn() / fadeOut(): 元素的淡入和…

vue实现滑块动画

vue实现滑块动画

Vue 实现滑块动画的方法 使用 Vue Transition 组件 Vue 的 <transition> 组件可以轻松实现滑块动画效果。通过定义进入和离开的 CSS 过渡类,结合 v-i…

vue实现ppt动画

vue实现ppt动画

Vue实现PPT动画的基本思路 使用Vue实现PPT动画效果,可以通过动态绑定CSS类、结合过渡动画和第三方库来实现。Vue的响应式特性非常适合处理动态内容切换和动画效果。 使用Vue过渡动画实现单…

vue使用animate实现动画

vue使用animate实现动画

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

vue实现循环image路径

vue实现循环image路径

在 Vue 中循环渲染图片路径的方法 使用 v-for 遍历图片路径数组 在 Vue 模板中,可以通过 v-for 指令循环遍历包含图片路径的数组,动态绑定 src 属性。 <template…