30 lines
1.6 KiB
TypeScript
30 lines
1.6 KiB
TypeScript
import { useEffect, type ReactNode } from 'react';
|
|
import type { ViewStyle } from 'react-native';
|
|
import Animated, { cancelAnimation, Easing, interpolate, useAnimatedStyle, useReducedMotion, useSharedValue, withDelay, withRepeat, withTiming } from 'react-native-reanimated';
|
|
|
|
/** 하늘에서 떨어진 뒤 둥둥 (웹 b-fall: b-drop 1.05초 뒤 b-float 무한). d·f 는 밀리초 */
|
|
export function Fall({ d, f, amp = 9, style, children }: { d: number; f: number; amp?: number; style?: ViewStyle; children: ReactNode }) {
|
|
const reduce = useReducedMotion();
|
|
const drop = useSharedValue(reduce ? 1 : 0);
|
|
const float = useSharedValue(0);
|
|
useEffect(() => {
|
|
if (reduce) return;
|
|
drop.value = withDelay(d, withTiming(1, { duration: 1050, easing: Easing.bezier(0.22, 0.9, 0.3, 1) }));
|
|
float.value = withDelay(d + 1050, withRepeat(withTiming(1, { duration: f / 2, easing: Easing.inOut(Easing.ease) }), -1, true));
|
|
return () => {
|
|
cancelAnimation(drop);
|
|
cancelAnimation(float);
|
|
};
|
|
}, [reduce, d, f, drop, float]);
|
|
const anim = useAnimatedStyle(() => {
|
|
const p = drop.value;
|
|
const y = interpolate(p, [0, 0.72, 0.86, 1], [-400, 0, -10, 0]) - amp * float.value;
|
|
const rot = interpolate(p, [0, 0.72, 1], [-10, 2, 0]) + 1.2 * float.value;
|
|
return {
|
|
opacity: interpolate(p, [0, 0.55, 1], [0, 1, 1]),
|
|
transform: [{ translateY: y }, { rotate: `${rot}deg` }, { scaleX: interpolate(p, [0, 0.72, 0.86, 1], [1, 1.04, 1, 1]) }, { scaleY: interpolate(p, [0, 0.72, 0.86, 1], [1, 0.96, 1, 1]) }],
|
|
};
|
|
});
|
|
return <Animated.View style={[style, anim]}>{children}</Animated.View>;
|
|
}
|