查看原始帖子 devaradise.com/javascript-react-animation-l.. 以获得更好的导航,含目录表。
动画可以通过使您的网页应用程序更具吸引力和交互性,将您的网页应用程序从良好提升到优秀。它们提供视觉反馈,引导用户通过界面,并为您的项目增添一丝个性。
有许多动画库,从简单的基于CSS的库到能够创建复杂动画的强大JavaScript库。在本文中,我们将深入了解React和现代网页应用的前15个动画库。
从知名的React Spring和Framer Motion到专门的库如Vivus和Three.js,您会找到一些东西,让您的网页项目焕发光泽。让我们探索这些库,看看它们如何改变您的网站用户体验!
通用用例的动画库
1. Animate.css – CSS动画
Animate.css 是一个流行的 CSS 库,提供了许多预定义的动画,使得为任何网页项目应用动画变得容易。拥有超过 80.2k 个 GitHub 星标 和数百万个 npm 下载量,Animate.css 是实现快速简单的动画的首选。
安装和基本使用
npm install animate.css
import 'animate.css';
<div class="animate__animated animate__bounce">An animated element</div>
最佳用例
Animate.css 最适合需要快速实现而无需 JavaScript 的简单动画。
优点
易于使用,轻量级,动画种类繁多。
缺点
仅限于 CSS 动画,对动画行为控制较少。
2. React Spring
React Spring 是 React 的一个流行动画库,提供了一种基于弹簧物理的强大且灵活的方式来创建动画。它在 GitHub 上拥有超过 27.8k 个星标 并在社区中广泛使用。
安装和基本使用
npm install @react-spring/web
import React from 'react';
import { useSpring, animated } from '@react-spring/web';
const App = () => {
const springs = useSpring({
from: { x: 0 },
to: { x: 100 }
});
return (
<animated.div
style={{
width: 80,
height: 80,
background: '#ff6d6d',
borderRadius: 8
}}
/>
);
};
export default App;
最佳用例
创建具有细粒度控制的过渡和交互的复杂动画。
优点
高度灵活,支持高级动画,社区支持良好。
缺点
对于初学者来说,学习曲线可能很陡峭。
3. Framer Motion
Framer Motion 是一个以其易用性和全面功能而闻名的强大动画库。它在 GitHub 上拥有 超过 22.8k 的星标,因其直观的 API 而受到广泛好评。
安装和基本使用
npm install framer-motion
import React from 'react';
import { motion } from 'framer-motion';
const App = () => {
return (
<motion.div initial={{ opacity: 0 }} animate={{ opacity: 1 }} transition={{ duration: 1 }}>
Hello, Framer Motion!
</motion.div>
);
};
export default App;
最佳用例
使用最少的代码创建平滑且视觉上吸引人的动画。
优点
直观的 API,支持关键帧动画,文档齐全。
缺点
与其它库相比,捆绑包大小略有增大。
4. Anime.js
Anime.js 是一个具有强大 API 的轻量级 JavaScript 动画库。它在 GitHub 上拥有 超过 49.2k 的星标,常用于创建复杂且高度可定制的动画。
安装和基本使用
npm install animejs
import anime from 'animejs/lib/anime.es.js';
anime({
targets: 'div',
translateX: 250,
rotate: '1turn',
duration: 800
});
最佳用例
创建复杂且详细的动画,精确控制。
优点
轻量级,多功能,高度可定制。
缺点
需要 JavaScript 知识,对于简单动画来说可能较为复杂。
5. GSAP (GreenSock Animation Platform)
GSAP 是一个功能强大的 JavaScript 动画库,以其高性能和丰富的功能集而闻名。它在 GitHub 上有 超过 19.2k 的星标,并被广泛应用于 Web 和移动应用程序中。
安装和基本用法
npm install gsap
import { gsap } from 'gsap';
gsap.to('.box', { rotation: 27, x: 100, duration: 1 });
最佳用例
高性能动画和复杂序列。
优点
非常强大,健壮,高性能。
缺点
打包体积较大,需要学习时间。
6. Popmotion
Popmotion 是一个功能性强、灵活的 JavaScript 动画库。它为 Framer Motion 的动画提供动力。它在 GitHub 上拥有 超过 19.9k 的星标,并提供了用于创建动画和交互的一系列工具。
安装和基本用法
npm install popmotion
import { animate } from 'popmotion';
animate({
from: 0,
to: 100,
onUpdate: (latest) => {
console.log(latest);
}
});
最佳用例
创建低级别、复杂的交互和高定制化的动画。
优点
功能性的 API,灵活,可扩展且体积小。
缺点
对于简单用例可能较为复杂。
7. Mo.js
Mo.js是一个为网页设计的动画图形工具包。它在GitHub上有超过18.3k个星标(over 18.3k stars on GitHub),并提供了一系列工具来创建动画。
安装和基本使用
npm install @mojs/core
import { Mojs } from '@mojs/core';
const bouncyCircle = new mojs.Shape({
parent: '#bouncyCircle',
shape: 'circle',
fill: { '#F64040': '#FC46AD' },
radius: { 20: 80 },
duration: 2000,
isYoyo: true,
isShowStart: true,
easing: 'elastic.inout',
repeat: 1
});
bouncyCircle.play();
最佳应用场景
创建复杂的动画和动态图形。
优点
多功能、强大,非常适合动态图形。
缺点
学习曲线较陡,打包体积较大。
特定用例的动画库
8. Remotion – 使用React生成动画视频
Remotion允许您使用React程序化地创建视频。这是一个独特的库,在GitHub上有超过19.6k个星标(over 19.6k stars on GitHub),使开发者能够利用React的力量进行视频内容创建。
安装和基本使用
# new project
npx create-video@latest
# install to existing project
npm i --save-exact [email protected] @remotion/[email protected]
export const MyComposition = () => {
return null;
};
import React from 'react';
import {Composition} from 'remotion';
import {MyComposition} from './Composition';
export const RemotionRoot: React.FC = () => {
return (
<>
<Composition
id="Empty"
component={MyComposition}
durationInFrames={60}
fps={30}
width={1280}
height={720}
/>
</>
);
};
import { registerRoot } from 'remotion';
import { RemotionRoot } from './Root';
registerRoot(RemotionRoot);
最佳应用场景
使用React程序化地生成视频。
优点
独特的功能,利用React技能,强大的视频创建能力。
缺点
适用场景有限,对初学者可能复杂。
9. Vivus – SVG绘制动画
Vivus 是一个轻量级的 JavaScript 库,用于为 SVG 创建动画。在 GitHub 上拥有 超过 15.1k 的星标,它是创建 SVG 动画的一个很好的选择。
安装和基本使用
npm install vivus
import Vivus from 'vivus';
new Vivus(
'my-svg',
{
type: 'delayed',
duration: 200,
animTimingFunction: Vivus.EASE
},
myCallback
);
// svg
<object id='my-svg' type='image/svg+xml' data='link/to/my.svg'></object>;
最佳用例
为 SVG 创建绘图动画。
优点
轻量级,专门针对 SVG,易于使用。
缺点
仅限于 SVG,不适合其他类型的动画。
10. Lottie for Web – 渲染 After Effects 动画
Lottie 是一个用于渲染在 Adobe After Effects 中创建的动画的库。在 GitHub 上拥有 超过 29.9k 的星标,它广泛用于集成复杂的动画。
安装和基本使用
npm install lottie-web
import lottie from 'lottie-web';
import animationData from './animation.json';
lottie.loadAnimation({
container: document.getElementById('animation'),
renderer: 'svg',
loop: true,
autoplay: true,
animationData: animationData
});
最佳用例
集成在 After Effects 中创建的动画。
优点
高质量的动画,与 After Effects 集成良好。
缺点
创建动画需要 After Effects,文件较大。
11. ScrollReveal
滚动揭示(ScrollReveal)是一个JavaScript库,可以轻松地为进入或离开视窗的元素制作动画。在GitHub上有超过22.2k个星标,非常适合基于滚动动画。
安装和基本用法
npm install scrollreveal
import ScrollReveal from 'scrollreveal';
ScrollReveal().reveal('.box', { delay: 500 });
<h1 class="headline">Widget Inc.</h1>
最佳用例
添加基于滚动的揭示动画。
优点
易于使用,体积小,非常适合滚动动画。
缺点
仅限于滚动动画。
12. 滚动魔法(ScrollMagic)
滚动魔法是一个用于创建滚动交互和动画的库。在GitHub上有超过14.8k个星标,为处理滚动动画提供了一个强大的方式。
安装和基本用法
npm install scrollmagic
import ScrollMagic from 'scrollmagic';
const controller = new ScrollMagic.Controller();
var controller = new ScrollMagic.Controller();
// create a scene
new ScrollMagic.Scene({
duration: 100, // the scene should last for a scroll distance of 100px
offset: 50 // start this scene after scrolling for 50px
})
.setPin('#my-sticky-element') // pins the element for the the scene's duration
.addTo(controller); // assign the scene to the controller
最佳用例
创建复杂的滚动交互和动画。
优点
功能强大,灵活,与GSAP集成良好。
缺点
对于简单动画来说可能较为复杂,打包体积较大。
13. Typed.js
Typed.js 是一个 JavaScript 库,可以逐字打出文本,使其看起来像是人类在打字。拥有 超过 15.1k 个 GitHub 星标,它非常适合添加打字动画。
安装和基本用法
npm install typed.js
import Typed from 'typed.js';
const typed = new Typed('#element', {
strings: ['<i>First</i> sentence.', '& a second sentence.'],
typeSpeed: 50
});
最佳用例
为文本创建打字动画。
优点
易于使用,轻量级,非常适合打字效果。
缺点
仅限于打字动画,灵活性较低。
高级动画库
14. Three.js – 高级 JavaScript 3D 库
Three.js 是一个功能强大的 JavaScript 3D 库,允许您创建复杂的 3D 动画和可视化。拥有 超过 101k 个 GitHub 星标,它广泛用于 3D 网页应用。
安装和基本用法
npm install three
import * as THREE from 'three';
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
最佳用例
创建高级 3D 动画和可视化。
优点
极其强大,庞大的社区,支持复杂的 3D 场景。
缺点
学习曲线陡峭,打包体积较大。
15. Theatre.js
Theatre.js 是一个高级动画库,用于以编程方式创建和控制动画。GitHub 上有超过 11k颗星,它提供了一种基于时间线的动画方法。
安装和基本用法
# r3f and its dependencies
npm install --save react three @react-three/fiber
# Theatre.js
npm install --save @theatre/[email protected] @theatre/[email protected] @theatre/[email protected]
# Three.js types (when using Typescript)
npm install --save-dev @types/three
import * as THREE from 'three'
import { createRoot } from 'react-dom/client'
import React, { useRef, useState } from 'react'
import { Canvas, useFrame } from '@react-three/fiber'
import { getProject } from '@theatre/core'
// our Theatre.js project sheet, we'll use this later
const demoSheet = getProject('Demo Project').sheet('Demo Sheet')
const App = () => {
return (
<Canvas
camera={{
position: [5, 5, -5],
fov: 75,
}}
>
<ambientLight />
<pointLight position={[10, 10, 10]} />
<mesh>
<boxGeometry args={[1, 1, 1]} />
<meshStandardMaterial color="orange" />
</mesh>
</Canvas>
)
}
createRoot(document.getElementById('root')!).render(<App />)
最佳使用案例
通过精细控制创建基于时间线的动画。
优点
强大的时间轴控制、精确的动画序列。
缺点
较新的库,较小的社区,对于初学者来说比较复杂。
结论
选择合适的动画库取决于您的项目需求和复杂性。如果您需要简单、快速的动画,Animate.css 等基于 CSS 的库是理想之选。
Each library has its strengths, so evaluate them based on factors such as ease of use, community support, and the specific features you require.对于更复杂的交互和高性能需求,可以考虑 GSAP 或 Three.js 等功能强大的库。在将这些顶级动画库应用到您的项目之前,您可以开始尝试使用它们。
您还有其他值得一提的动画库吗?如果您觉得这篇文章对您有帮助,请与您的其他开发人员分享,并浏览本网站上的更多教程
谢谢。祝您愉快