93 lines
4.7 KiB
TypeScript
93 lines
4.7 KiB
TypeScript
import type { TimeKind } from '@nalssigak/shared/lib';
|
|
import { LinearGradient } from 'expo-linear-gradient';
|
|
import { useEffect } from 'react';
|
|
import { StyleSheet, View, type DimensionValue } from 'react-native';
|
|
import Animated, { cancelAnimation, Easing, interpolate, useAnimatedStyle, useReducedMotion, useSharedValue, withDelay, withRepeat, withTiming } from 'react-native-reanimated';
|
|
|
|
import { useSky } from '@/lib/sky';
|
|
|
|
const KINDS: TimeKind[] = ['morning', 'day', 'sunset', 'night'];
|
|
|
|
const SKY: Record<TimeKind, { colors: readonly [string, string, string]; locations: readonly [number, number, number] }> = {
|
|
morning: { colors: ['#ffe0c4', '#fff0e2', '#e4f2ff'], locations: [0, 0.38, 1] },
|
|
day: { colors: ['#cde7ff', '#e9f5ff', '#f7fbff'], locations: [0, 0.45, 1] },
|
|
sunset: { colors: ['#ffd0b5', '#f9c3cf', '#ddd2f6'], locations: [0, 0.42, 1] },
|
|
night: { colors: ['#0e1433', '#1b2356', '#2a2e6e'], locations: [0, 0.5, 1] },
|
|
};
|
|
|
|
const STARS = Array.from({ length: 46 }, (_, i) => ({ left: `${(i * 41 + 7) % 100}%`, top: `${(i * 67 + 3) % 96}%`, size: 1 + (i % 3) * 0.8, period: (3 + (i % 5)) * 1000, delay: ((i * 0.7) % 6) * 1000 }) as const);
|
|
|
|
/** 시간대 하나의 하늘 판. 고른 시간대만 1.4초 동안 서서히 드러난다 */
|
|
function Layer({ kind, on, reduce }: { kind: TimeKind; on: boolean; reduce: boolean }) {
|
|
const shown = useSharedValue(on ? 1 : 0);
|
|
useEffect(() => {
|
|
shown.value = reduce ? (on ? 1 : 0) : withTiming(on ? 1 : 0, { duration: 1400, easing: Easing.ease });
|
|
}, [on, reduce, shown]);
|
|
const style = useAnimatedStyle(() => ({ opacity: shown.value }));
|
|
return (
|
|
<Animated.View style={[StyleSheet.absoluteFill, style]}>
|
|
<LinearGradient {...SKY[kind]} style={StyleSheet.absoluteFill} />
|
|
</Animated.View>
|
|
);
|
|
}
|
|
|
|
/** 밤 별: 제각각 주기로 반짝인다 */
|
|
function Star({ star, reduce }: { star: (typeof STARS)[number]; reduce: boolean }) {
|
|
const glow = useSharedValue(0);
|
|
useEffect(() => {
|
|
if (reduce) return;
|
|
glow.value = withDelay(star.delay, withRepeat(withTiming(1, { duration: star.period / 2, easing: Easing.inOut(Easing.ease) }), -1, true));
|
|
return () => cancelAnimation(glow);
|
|
}, [reduce, glow, star]);
|
|
const style = useAnimatedStyle(() => ({ opacity: reduce ? 0.7 : 0.25 + glow.value * 0.75 }));
|
|
return <Animated.View style={[{ position: 'absolute', left: star.left, top: star.top, width: star.size, height: star.size, borderRadius: star.size, backgroundColor: '#fff', boxShadow: '0 0 6px rgb(255 255 255 / 0.8)' }, style]} />;
|
|
}
|
|
|
|
export type ShootSpec = { top: DimensionValue; left: DimensionValue; len: number; dur: number; dl: number };
|
|
|
|
// 별똥별: 위치·길이·주기를 엇갈려 몇 초마다 하나씩 떨어지게 (웹 BLayout SHOOTS, 밀리초)
|
|
const SHOOTS: ShootSpec[] = [
|
|
{ top: '8%', left: '78%', len: 110, dur: 7000, dl: 1000 },
|
|
{ top: '22%', left: '46%', len: 80, dur: 9000, dl: 3500 },
|
|
{ top: '5%', left: '30%', len: 95, dur: 8000, dl: 5500 },
|
|
{ top: '36%', left: '88%', len: 70, dur: 10000, dl: 2200 },
|
|
{ top: '52%', left: '62%', len: 90, dur: 11000, dl: 7000 },
|
|
{ top: '14%', left: '96%', len: 120, dur: 12000, dl: 9000 },
|
|
];
|
|
|
|
/** 별똥별 하나: 주기 끝 14% 동안만 비스듬히 240px 미끄러지며 반짝 (웹 .b-shoot). 동작 줄이기면 그리지 않는다 */
|
|
export function Shoot({ top, left, len, dur, dl }: ShootSpec) {
|
|
const reduce = useReducedMotion();
|
|
const p = useSharedValue(0);
|
|
useEffect(() => {
|
|
if (reduce) return;
|
|
p.value = withDelay(dl, withRepeat(withTiming(1, { duration: dur, easing: Easing.in(Easing.ease) }), -1, false));
|
|
return () => cancelAnimation(p);
|
|
}, [reduce, dur, dl, p]);
|
|
const style = useAnimatedStyle(() => ({
|
|
opacity: interpolate(p.value, [0, 0.86, 0.89, 1], [0, 0, 1, 0]),
|
|
transform: [{ rotate: '-24deg' }, { translateX: interpolate(p.value, [0, 0.86, 1], [0, 0, -240]) }],
|
|
}));
|
|
if (reduce) return null;
|
|
return (
|
|
<Animated.View style={[{ position: 'absolute', top, left, width: len, height: 1.5 }, style]}>
|
|
<LinearGradient colors={['rgba(255,255,255,0)', '#ffffff']} start={{ x: 0, y: 0 }} end={{ x: 1, y: 0 }} style={{ flex: 1, borderRadius: 2 }} />
|
|
</Animated.View>
|
|
);
|
|
}
|
|
|
|
/** 화면 전체 하늘: 시간대별 색이 서서히 바뀌고, 밤에는 별이 반짝이고 별똥별이 떨어진다 */
|
|
export function SkyBg() {
|
|
const { kind } = useSky();
|
|
const reduce = useReducedMotion();
|
|
return (
|
|
<View style={StyleSheet.absoluteFill} pointerEvents="none">
|
|
{KINDS.map((k) => (
|
|
<Layer key={k} kind={k} on={k === kind} reduce={reduce} />
|
|
))}
|
|
{kind === 'night' && STARS.map((s, i) => <Star key={i} star={s} reduce={reduce} />)}
|
|
{kind === 'night' && SHOOTS.map((s, i) => <Shoot key={i} {...s} />)}
|
|
</View>
|
|
);
|
|
}
|