import React, { useEffect, useRef } from 'react'; import { View, Animated } from 'react-native'; import _ from 'lodash'; import styles from './loader.styles'; // oh God help me const DURATION = 1000; const POINTS_COUNT = 12; const ANGLE_STEP = (Math.PI * 2) / POINTS_COUNT; const INPUT_RANGE = _.map(Array.from(new Array(POINTS_COUNT + 1)), (__, id) => id); const OPACITY_STEPS = _.map( Array.from(new Array(POINTS_COUNT)), (__, id) => id / (POINTS_COUNT - 1), ); const getOutputRange = (id: number) => [...OPACITY_STEPS, ...OPACITY_STEPS].slice(id, id + POINTS_COUNT + 1); export const Loader: React.FC = () => { const animationRef = useRef(new Animated.Value(0)); const getPointStyle = (id: number) => { const position = { top: Math.cos(id * ANGLE_STEP) * 36 + 36, left: Math.sin(id * ANGLE_STEP) * 36 + 36, opacity: animationRef.current.interpolate({ inputRange: INPUT_RANGE, outputRange: getOutputRange(id), }), }; return [styles.point, position]; }; useEffect(() => { Animated.loop( Animated.timing(animationRef.current, { toValue: POINTS_COUNT - 1, duration: DURATION, useNativeDriver: true, }), ).start(); }, []); return ( {_.map(Array.from(new Array(POINTS_COUNT)), (__, id) => ( ))} ); };