Last active
April 24, 2023 10:27
-
-
Save aranda-adapptor/29be35730027fc496f65888e646d6e61 to your computer and use it in GitHub Desktop.
Starfield in Reanimated 2 - adding animation 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useEffect } from "react"; | |
import { View } from "react-native"; | |
// Import the required types from Reanimated | |
import Animated, { | |
Easing, | |
useAnimatedStyle, | |
useSharedValue, | |
withRepeat, | |
withTiming, | |
} from "react-native-reanimated"; | |
interface StarProps { | |
x: number; | |
y: number; | |
// Stars now take a shared time value | |
time: Animated.SharedValue<number>; | |
} | |
const Star: React.FC<StarProps> = (props) => { | |
// useAnimatedStyle takes a function that runs our | |
// procedural animation code and returns style properties. | |
const animatedStyle = useAnimatedStyle(() => { | |
const x = Math.sin(props.time.value * 5) * 50; | |
const y = Math.cos(props.time.value * 5) * 50; | |
return { | |
left: props.x + x, | |
top: props.y + y, | |
}; | |
}); | |
// Return an Animated.View and compose the static style values | |
// with the animated style values | |
return ( | |
<Animated.View | |
style={[ | |
{ | |
width: 10, | |
height: 10, | |
backgroundColor: "white", | |
}, | |
animatedStyle, | |
]} | |
/> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment