Skip to content

Instantly share code, notes, and snippets.

View aranda-adapptor's full-sized avatar

Aranda Morrison aranda-adapptor

View GitHub Profile
@aranda-adapptor
aranda-adapptor / ConvertToTS.sh
Last active March 3, 2021 07:41
Convert to TypeScript
curl https://raw.githubusercontent.com/expo/expo/master/templates/expo-template-blank-typescript/tsconfig.json -o tsconfig.json
yarn add --dev typescript @types/react @types/react-native @types/react-dom
mv App.js App.tsx
@aranda-adapptor
aranda-adapptor / App.tsx
Created February 24, 2021 03:54
The final App.tsx code
import React, { useEffect } from "react";
import { View, Dimensions } from "react-native";
import Animated, {
useSharedValue,
withRepeat,
withTiming,
useAnimatedStyle,
Easing,
} from "react-native-reanimated";
@aranda-adapptor
aranda-adapptor / App.tsx
Last active February 24, 2021 06:18
Now make it 3D
// Stars positions should be spread evenly between -0.5 and 0.5.
// We apply the screen width/height adjustment in the animated style function.
for (var i = 0; i < starCount; i++) {
stars.push({
id: i,
x: Math.random() - 0.5,
y: Math.random() - 0.5,
});
}
@aranda-adapptor
aranda-adapptor / App.tsx
Created December 24, 2020 04:51
Iterate and render all stars, passing in StarData as props
return (
<View
style={{
flex: 1,
backgroundColor: "black",
}}
>
{stars.map((s) => (
// The spread operator passes all StarData fields as individual props
<Star key={s.id} time={timeVal} {...s} />
@aranda-adapptor
aranda-adapptor / App.tsx
Created December 24, 2020 04:49
Use star id to vary the star animation
const animatedStyle = useAnimatedStyle(() => {
const x = Math.sin(props.time.value * 2 + props.id) * 50;
const y = Math.cos(props.time.value * 2 + props.id) * 50;
return {
left: props.x + x,
top: props.y + y,
};
});
// Split the static star data out from the props type
interface StarData {
id: number;
x: number;
y: number;
}
interface StarProps extends StarData {
time: Animated.SharedValue<number>;
}
@aranda-adapptor
aranda-adapptor / App.tsx
Created December 24, 2020 02:08
Starfield in Reanimated 2 - adding animation 2
const Starfield: React.FC<{}> = () => {
// Create a shared time value
const timeVal = useSharedValue(0);
// Start a repeating animation that goes from 0 to 3600 in an hour
const duration = 3600;
useEffect(
() => {
timeVal.value = 0;
timeVal.value = withRepeat(
withTiming(duration, {
@aranda-adapptor
aranda-adapptor / App.tsx
Last active April 24, 2023 10:27
Starfield in Reanimated 2 - adding animation 1
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";
@aranda-adapptor
aranda-adapptor / App.tsx
Created December 24, 2020 02:02
Starfield In Reanimated 2 blog post - initial App.tsx
import React from "react";
import { View } from "react-native";
// Props type for the star
interface StarProps {
x: number;
y: number;
}
// Star component