Framer Motion 統合
Framer Motion のアニメーションは通常時間ベースです。Helios(フレームベースでシーク可能)と同期させるには、Helios のフレーム状態を MotionValue にマッピングする必要があります。
クイックスタート
1. useVideoFrame フック
useVideoFrame フックが利用できることを確認してください(examples/react/SKILL.md を参照)。
2. フレームを MotionValue にマッピング
useTransform を使用して、現在のフレーム(useVideoFrame から取得)をアニメーション値にマッピングします。
import { useMotionValue, useTransform, motion } from 'framer-motion';
import { useVideoFrame } from './hooks/useVideoFrame';
export const MyComponent = ({ helios }) => {
// 1. 現在のフレームを取得
const frame = useVideoFrame(helios);
// 2. フレーム用の MotionValue を作成
const frameMv = useMotionValue(0);
// フレームが変更されるたびに MotionValue を同期
useEffect(() => {
frameMv.set(frame);
}, [frame, frameMv]);
// 3. フレーム → アニメーション値に変換
// 例: 60フレーム(0〜60)で360度回転
const rotate = useTransform(frameMv, [0, 60], [0, 360]);
// 例: 最初の30フレームでフェードイン
const opacity = useTransform(frameMv, [0, 30], [0, 1]);
return (
<motion.div
style={{
width: 100,
height: 100,
background: 'blue',
rotate, // 変換した値をバインド
opacity
}}
/>
);
};
上級: 手動による MotionValue の設定
深いツリー構造でパフォーマンスを向上させるには、サブスクリプション内で共有の MotionValue を直接更新し、React の状態管理をバイパスします。
const frameMv = useMotionValue(0);
useEffect(() => {
const unsubscribe = helios.subscribe((state) => {
// MotionValue を直接更新 - React の再レンダリングは発生しません!
frameMv.set(state.currentFrame);
});
return unsubscribe;
}, [helios, frameMv]);
const x = useTransform(frameMv, [0, 100], [0, 500]);
return <motion.div style={{ x }} />;
ソースファイル
- 例:
examples/framer-motion-animation/