Framer Motion 集成
Framer Motion 动画通常是基于时间的。为了将它们与 Helios(基于帧且可擦洗的)同步,您必须将 Helios 的帧状态映射到 MotionValue。
快速开始
1. useVideoFrame Hook
确保您拥有 useVideoFrame Hook(参见 examples/react/SKILL.md)。
2. 将帧映射到 MotionValues
使用 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/