React & Modern Javascript 應用的前15大動畫庫

請查看原始貼文 devaradise.com/javascript-react-animation-l.. 以獲得更好的導航和內容目錄。

動畫能將您的網絡應用程序從好提升到優秀,通過讓它們更加吸引人和互動性強。它們提供視覺反饋,引导学生通過界面,並為您的項目增添一絲個性。

從簡單的CSS基礎庫到能夠創建複雜動畫的強大JavaScript庫,有很多動畫庫可供選擇。在本文中,我們將深入研究15個最适合React和現代網絡應用程序的動畫庫。

從知名的React Spring和Framer Motion到专门的庫如Vivus和Three.js,您一定能找到些东西來讓您的網絡項目閃閃發光。讓我們探索這些庫,並看看它們如何改變您的網站用戶體驗!

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 的簡單動畫。

易於使用,輕量级, animation 种类多。

僅限 CSS animation,對 animation 行為的控制較少。

React Spring 是一個受歡迎的 React 动画库,提供基於弹簧物理的強大和靈活的动画创建方式。它擁有 27.8k 颗星於 GitHub 上,並在社區中被廣泛使用。

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;

創建带有细緻控制的过渡和互動的複雜动画。

高度靈活,支持進階动画,有良好的社區支持。

對初学者來說學習曲線可能較陡。

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,支持關鍵幀動畫,文档完善。

與其他庫相比,不打包大小略大。

Anime.js 是一個輕量级的 JavaScript 動畫庫,具有強大的 API。在 GitHub 上有 超過 49.2k 星号,常用于創建複雜且高度可定制的動畫。

npm install animejs
import anime from 'animejs/lib/anime.es.js';

anime({
    targets: 'div',
    translateX: 250,
    rotate: '1turn',
    duration: 800
});

使用精確的控制創建複雜且詳細的動畫。

輕量級,多才多藝,高度可定制。

需要 JavaScript 知識,對於簡單的動畫來說可能會較為複雜。

GSAP 是一款強大的 JavaScript 動畫庫,以其高性能和豐富的功能集而聞名。它在 GitHub 上有 超過 19.2k 的星號,並且在網絡和移動應用中廣泛使用。

npm install gsap
import { gsap } from 'gsap';

gsap.to('.box', { rotation: 27, x: 100, duration: 1 });

高性能動畫和複雜序列。

极为强大、健壯、高性能。

包體大小較大,需要學習時間。

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,靈活,可擴展且包體小巧。

對於簡單的應用場景可能會較為複雜。

Mo.js是一個為網絡設計的动态圖形工具帶。它在GitHub上有超過18.3k個星號,並提供各種工具來創建動畫。

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

創建複雜的動態圖形和動畫。

多才多藝,強大,非常適合動態圖形。

學習曲線較陡,捆包大小較大。

Remotion 讓您可以使用 React 程序化地創建視頻。它是一個擁有超過19.6k個星號的独特庫,讓開發者能夠利用 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 技能,強大的視頻創建能力。

特定的應用場景,對於初學者可能會較複雜。

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,不適合其他類型的動畫。

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 來創建動畫,文件較大。

ScrollingReveal 是一個 JavaScript 庫,用來輕鬆地为進入或離開可视区域的元素進行動畫效果。擁有 GitHub 上超過 22.2k 的星标,它非常适合基於滚动的歌劇 animation。

npm install scrollreveal
import ScrollReveal from 'scrollreveal';

ScrollReveal().reveal('.box', { delay: 500 });
<h1 class="headline">Widget Inc.</h1>

添加基於滚动的揭示 animation。

使用簡單,體積輕巧,非常適合 scroll animation。

僅限基於 scroll 的 animation。

ScrollMagic 是一個用於創建 scroll 互動和 animation 的庫。擁有 GitHub 上超過 14.8k 的星标,它提供了一种強大且靈活的處理基於 scroll 的 animation 方式。

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

創建複雜的 scroll 互動和 animation。

強大,靈活,與 GSAP 結合得很好。

對於簡單的 animation 而言可能會过于複雜,而且 bundle size 較大。

Typed.js 是一個 JavaScript 庫,可以打出文字,讓它看起來像是由人類打的字。擁有 GitHub 上的 15.1k 星号,它很適合添加打字動畫。

npm install typed.js
import Typed from 'typed.js';

const typed = new Typed('#element', {
    strings: ['<i>First</i> sentence.', '&amp; a second sentence.'],
    typeSpeed: 50
});

創建文字的打字動畫。

易於使用,輕量级,對於打字效果來說非常好。

僅限打字動畫,靈活性較低。

Three.js 是一個強大的 JavaScript 3D 庫,讓你可以創建複雜的 3D 動畫和可视化。擁有 GitHub 上的 101k 星号,它被廣泛用於 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 場景。

學習曲線陡峭,打包大小較大。

劇場.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 />)

使用細致控制的時間線基礎動畫。

強大的時間線控制,精確的動畫序列。

較新的庫,社區較小,對初學者來說較複雜。

選擇正確的動畫庫取決於您项目的需求和複雜度。如果您正在尋找簡單快速的動畫,CSS 基礎的庫如 Animate.css 是理想的選擇。對於更複雜的互動和高性能需求,請考慮像 GSAP 或 Three.js 這樣的功能強大的庫。

每個庫都有它的優點,所以根據易用性、社區支持以及您特定需求的特點來評估它們。您可以在實施到项目中之前,開始嘗試這些顶级的動畫庫。

您有其他值得提及的庫嗎? 如果您發現這篇文章有幫助,請與您的開發同伴分享,並在以下評論中分享更多的教學文章。

感謝您。祝您有美好的一天!

Source:
https://devaradise.hashnode.dev/top-15-animation-libraries-for-react-modern-javascript-apps-clyppjb22000b09l26nxe85v7