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 설정
깊은 트리에서 더 나은 성능을 위해 구독(subscription) 내에서 직접 공유 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/