$ git show coverflow
Coverflow carousel
Horizontal carousel where the centered item is large and flat, and the neighbors shrink and turn away.
Inspired by Philz CoffeeFrom William Candillon’s “Can it be done in React Native?” series. The code here is a simplified rewrite for Reanimated 4 and 3.
This snippet uses APIs that didn’t change between versions, so the same code runs on Reanimated 4 and 3.
const ITEM_W = width * 0.6;
const x = useSharedValue(0);
const onScroll = useAnimatedScrollHandler(e => { x.value = e.contentOffset.x; });
function Slide({ index, children }) {
const style = useAnimatedStyle(() => {
const p = (x.value - index * ITEM_W) / ITEM_W; // 0 when centered
return {
opacity: interpolate(Math.abs(p), [0, 1], [1, 0.5], Extrapolation.CLAMP),
transform: [
{ perspective: 800 },
{ rotateY: interpolate(p, [-1, 0, 1], [35, 0, -35], Extrapolation.CLAMP) + 'deg' },
{ scale: interpolate(Math.abs(p), [0, 1], [1, 0.8], Extrapolation.CLAMP) },
],
};
});
return <Animated.View style={[{ width: ITEM_W }, style]}>{children}</Animated.View>;
}
<Animated.ScrollView horizontal snapToInterval={ITEM_W} decelerationRate="fast"
contentContainerStyle={{ paddingHorizontal: (width - ITEM_W) / 2 }}
onScroll={onScroll} scrollEventThrottle={16} showsHorizontalScrollIndicator={false} />