GSAP は何をしますか?
概要
GSAP(GreenSock Animation Platform)は、最も強力なウェブアニメーションライブラリです。複雑なタイムライン、スクロール駆動アニメーション、SVGモーフィング、テキストエフェクト、物理ベースのモーションを処理できます。Apple、Google、Nikeがランディングページや製品紹介に使用しています。ほとんどの用途で無料です。
手順
ステップ1: 基本アニメーション
import gsap from 'gsap'
// アニメーション(現在の状態から目標へ)
gsap.to('.hero-title', { opacity: 1, y: 0, duration: 1, ease: 'power3.out' })
// アニメーション(目標から現在の状態へ)
gsap.from('.card', { opacity: 0, y: 50, duration: 0.8, stagger: 0.15 })
// 即時に設定
gsap.set('.hidden-element', { opacity: 0, y: 20 })
ステップ2: タイムライン
// 順次アニメーション — 順番に再生
const tl = gsap.timeline({ defaults: { duration: 0.6, ease: 'power2.out' } })
tl.from('.logo', { scale: 0, rotation: -180 })
.from('.nav-link', { opacity: 0, y: -20, stagger: 0.1 }, '-=0.3') // 0.3秒重ねる
.from('.hero-text', { opacity: 0, x: -50 })
.from('.cta-button', { opacity: 0, scale: 0.8 }, '<') // 直前のアニメーションと同時に開始
// 制御
tl.play()
tl.reverse()
tl.pause()
tl.seek(1.5) // 1.5秒の位置にジャンプ
ステップ3: ScrollTrigger
import { ScrollTrigger } from 'gsap/ScrollTrigger'
gsap.registerPlugin(ScrollTrigger)
// スクロールして表示されたときにアニメーション
gsap.from('.feature-section', {
scrollTrigger: {
trigger: '.feature-section',
start: 'top 80%', // 要素のトップがビューポートの80%に達したときにトリガー
end: 'bottom 20%',
toggleActions: 'play none none reverse', // onEnter, onLeave, onEnterBack, onLeaveBack
},
opacity: 0,
y: 100,
duration: 1,
})
// スクロール中に要素を固定
const horizontalSection = document.querySelector('.horizontal-section')
const horizontalWrapper = document.querySelector('.horizontal-wrapper')
gsap.to(horizontalSection, {
x: () => -(horizontalSection.scrollWidth - window.innerWidth),
scrollTrigger: {
trigger: horizontalWrapper,
pin: true,
scrub: 1, // スムーズなスクラブ(1秒の遅延)
end: () => '+=' + horizontalSection.scrollWidth,
},
})
ステップ4: Reactとの統合
import { useRef } from 'react'
import { useGSAP } from '@gsap/react'
import gsap from 'gsap'
function AnimatedSection() {
const container = useRef(null)
useGSAP(() => {
gsap.from('.card', {
opacity: 0, y: 40, stagger: 0.1, duration: 0.6,
scrollTrigger: { trigger: container.current, start: 'top 80%' },
})
}, { scope: container })
return (
<div ref={container}>
<div className="card">Card 1</div>
<div className="card">Card 2</div>
<div className="card">Card 3</div>
</div>
)
}
ガイドライン
- GSAPは公開ウェブサイトでは無料です。商用プラグイン(MorphSVG、SplitText)にはライセンスが必要です。
- Reactでは
useGSAPフックを使用してください — アンマウント時に自動的にクリーンアップが行われます。 scrubを使ったScrollTriggerは、スクロールに連動したアニメーション(パララックス、進行状況表示)を作成します。staggerは複数の要素を順次アニメーションさせます — 手動で遅延を設定するよりもすっきりします。- GSAPは複雑なシーケンスにおいてCSSアニメーションよりも優れたパフォーマンスを発揮します — requestAnimationFrameベースです。