動かす・止める(Motion)
このページでは、Framer Motion(drag・useSpring・offset-path)と GSAP(Draggable・quickTo・MotionPathPlugin)を使った、ドラッグ・マウス追従・経路アニメーション・回転の実装パターンを、インタラクティブなデモとコードで確認できます。
操作に合わせて動くアニメーションは、ドラッグやマウスの動きに要素を追わせたり、決めた経路に沿って要素を動かしたりする表現。UIのフィードバックや、触って楽しい演出に使われる。
GSAPはDraggable・MotionPathPluginなどのプラグインで、範囲・向き・経路を細かく制御できる。Framer MotionはdragやuseSpringを、Reactのpropsとフックで書ける。経路は、Framer MotionならCSSのoffset-path(2022年3月から主要ブラウザで使える)に沿ってoffsetDistanceを動かし、GSAPならMotionPathPluginでSVGのpathに合わせる。
Framer Motion
ドラッグは drag を付けて dragConstraints で範囲を決め、マウス追従は useMotionValue の値を useSpring で追わせる。経路は CSS の offset-path に path を書き、useAnimate の animate() で offsetDistance を 0% → 100% にする。
読み込み中...
'use client';
import { useRef } from 'react';
import { motion, useAnimate, useMotionValue, useSpring } from 'motion/react';
// ① ドラッグ:dragConstraints に枠の ref を渡すと、その中だけ動く。dragElastic は枠の外へのはみ出しやすさ
function Drag() {
const area = useRef<HTMLDivElement>(null);
return (
<div ref={area} style={{ height: 160, border: '1px dashed #c084fc', borderRadius: 12, display: 'grid', placeItems: 'center', overflow: 'hidden' }}>
<motion.div
drag dragConstraints={area} dragElastic={0.2} whileDrag={{ scale: 1.1 }} whileTap={{ scale: 0.95 }}
style={{ width: 56, height: 56, borderRadius: 12, background: '#c084fc', cursor: 'grab' }}
/>
</div>
);
}
// ② マウス追従:マウスの位置を motion value に入れ、useSpring でばねのように遅れて追わせる
function MouseFollow() {
const rawX = useMotionValue(0);
const rawY = useMotionValue(0);
const x = useSpring(rawX, { stiffness: 150, damping: 20 });
const y = useSpring(rawY, { stiffness: 150, damping: 20 });
// 玉は枠の中心に置いてあるので、枠の中心からの距離を入れる
const follow = (e: React.MouseEvent<HTMLDivElement>) => {
const r = e.currentTarget.getBoundingClientRect();
rawX.set(e.clientX - r.left - r.width / 2);
rawY.set(e.clientY - r.top - r.height / 2);
};
return (
<div onMouseMove={follow} style={{ position: 'relative', height: 160, border: '1px dashed #6ee7b7', borderRadius: 12, overflow: 'hidden' }}>
<motion.div
style={{
position: 'absolute', top: '50%', left: '50%', width: 40, height: 40, marginTop: -20, marginLeft: -20,
borderRadius: '50%', background: '#34d399', x, y, pointerEvents: 'none',
}}
/>
</div>
);
}
// ③ 経路:CSS の offset-path に SVG と同じ path を指定し、offsetDistance(道のりの何%か)を 0% → 100% にする。
// offset-path の座標は px なので、SVG は viewBox と同じ 260×160 で描き、玉と同じ箱に重ねる
const PATH = 'M 20,80 C 54,20 105,140 138,80 S 206,20 240,80';
function PathMotion() {
const [scope, animate] = useAnimate();
// 0% → 100% を毎回指定するので、途中で押しても始点から再生し直す
const play = () => animate('.path-ball', { offsetDistance: ['0%', '100%'] }, { duration: 2, ease: 'easeInOut' });
return (
<div ref={scope}>
<div style={{ position: 'relative', width: 260, height: 160 }}>
<svg width="260" height="160" viewBox="0 0 260 160" style={{ position: 'absolute', top: 0, left: 0 }}>
<path d={PATH} fill="none" stroke="#e9d5ff" strokeWidth="2" strokeDasharray="6 4" />
</svg>
{/* offsetRotate: auto で、進む向きに合わせて回る(GSAP の autoRotate にあたる) */}
<div
className="path-ball"
style={{
position: 'absolute', top: 0, left: 0, width: 32, height: 32, borderRadius: '50%', background: '#c084fc',
offsetPath: `path("${PATH}")`, offsetRotate: 'auto',
}}
/>
</div>
<button onClick={play}>再生</button>
</div>
);
}
// ④ 回転:0 → 360 度を毎回指定するので、回転中に押しても最後はまっすぐに止まる。連続回転は repeat: Infinity
function Spin() {
const [scope, animate] = useAnimate();
const spin = () => animate('.spin-box', { rotate: [0, 360] }, { duration: 0.8, ease: 'backOut' });
return (
<div ref={scope} style={{ display: 'flex', gap: 32, alignItems: 'center' }}>
<div className="spin-box" style={{ width: 56, height: 56, borderRadius: 12, background: '#fb923c' }} />
<button onClick={spin}>1回転</button>
<motion.div
animate={{ rotate: 360 }}
transition={{ duration: 2, ease: 'linear', repeat: Infinity }}
style={{ width: 56, height: 56, borderRadius: '50%', border: '4px solid transparent', borderTopColor: '#c084fc', borderRightColor: '#a78bfa' }}
/>
</div>
);
}
export default function MotionDemo() {
return (
<div style={{ display: 'grid', gap: 32 }}>
<Drag />
<MouseFollow />
<PathMotion />
<Spin />
</div>
);
}AIプロンプトテンプレート
Next.js + Motion(motion/react)で、操作に合わせて動くアニメーションを実装してください。 - ドラッグ:drag と dragConstraints(枠の ref)で、枠の中だけ動かす。whileDrag でドラッグ中に少し拡大する - マウス追従:マウスの位置を useMotionValue に入れ、useSpring でなめらかに追わせる - 経路:CSS の offset-path に SVG と同じ path を指定し、useAnimate の animate() で offsetDistance を 0% → 100% にする。offsetRotate: auto で進む向きに回す - 回転:useAnimate の animate() で rotate を [0, 360] にする。連続回転は repeat: Infinity - TypeScript / Tailwind CSS v4 を使用
このプロンプトはあくまでたたき台です。AIの回答はモデルやバージョン、会話の文脈によって毎回異なります。意図通りに動かない場合は、条件を追記・修正してお使いください。
実装方法の比較
| 項目 | Framer Motion | GSAP |
|---|---|---|
| ドラッグ | drag を付けるだけ。dragConstraints で範囲、whileDrag でドラッグ中の見た目を指定 | Draggable.create()。bounds で範囲、type で動かし方(x・y・rotation など)を指定 |
| マウス追従 | useMotionValue に位置を入れ、useSpring で遅れて追わせる | quickTo() が作った関数を mousemove で呼ぶ。毎回 tween を作らないので軽い |
| 経路アニメーション | CSS の offset-path に沿って offsetDistance を動かす。座標は px なので、SVG を拡大縮小すると path と合わない | MotionPathPlugin。SVG の path をそのまま渡せ、拡大縮小した SVG にも align で合わせられる |
| 回転 | rotate(rotateX・rotateY で3D も)。[0, 360] のように始点と終点を書く | rotation(rotationX・rotationY で3D も)。'+=360' のような相対値でも書ける |
同じカテゴリの他のページ
文字を演出する(Text Animation)の実装比較
Framer Motion・GSAPの文字アニメーション(Text Animation)実装を比較。SplitText・ScrambleTextと自前の分割で、1文字ずつのフェード・スライド・波・スクランブル・再集合のデモ・コード付き。
動きの質感を選ぶ(Easing)の実装比較
Framer Motion・GSAP・CSS Transitionのイージング(Easing)実装を比較。easeOut・back・bounce・elasticの違いと、CSSのlinear()で再現する方法のデモ・コード付き。
スクロールで物語る(Scroll Storytelling)の実装比較
GSAPのScrollTriggerで実装するスクロール演出のサンプル。scrub・pin・横スクロール・パララックスなど、スクロールに連動した高度なアニメーションのUIパターン実例集。デモ・コード付き。