Skip to content

Instantly share code, notes, and snippets.

@IslamRustamov
Last active December 30, 2024 15:43
Show Gist options
  • Select an option

  • Save IslamRustamov/cf7eac69392f51a4f7d024a8a6f06593 to your computer and use it in GitHub Desktop.

Select an option

Save IslamRustamov/cf7eac69392f51a4f7d024a8a6f06593 to your computer and use it in GitHub Desktop.
import { Canvas, Rect } from '@shopify/react-native-skia';
import React, { useEffect, useRef } from 'react';
import { Dimensions, StyleSheet } from 'react-native';
import { DerivedValue, Easing, useDerivedValue, useSharedValue, withRepeat, withTiming } from 'react-native-reanimated';
const squares = Array.from(Array(100).keys());
const screenWidth = Dimensions.get('screen').width;
const screenHeight = Dimensions.get('screen').height;
function Square({transform}: {transform: DerivedValue<{
rotate: number;
}[]>}) {
const position = useRef({x: Math.random() * screenWidth, y: Math.random() * screenHeight});
return (
<Rect
transform={transform}
x={position.current.x}
y={position.current.y}
origin={{x: position.current.x + 25, y: position.current.y + 25}}
height={50}
width={50}
color={'red'}
/>
);
}
function App(): React.JSX.Element {
const rotationSharedValue = useSharedValue(0);
useEffect(() => {
rotationSharedValue.value = withRepeat(
withTiming(-360, {duration: 4000, easing: Easing.linear}),
-1
);
}, [rotationSharedValue]);
const transform = useDerivedValue(
() => [{rotate: rotationSharedValue.value}],
[rotationSharedValue],
);
return (
<Canvas style={styles.safeAreaView}>
{
squares.map((_, index) => {
return <Square key={index} transform={transform} />;
})
}
</Canvas>
);
}
const styles = StyleSheet.create({
safeAreaView: {
flex: 1,
},
});
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment