Skip to content

Commit

Permalink
docs: added new example
Browse files Browse the repository at this point in the history
  • Loading branch information
dohooo committed Jun 10, 2023
1 parent 0237ae8 commit cfa8ca9
Show file tree
Hide file tree
Showing 11 changed files with 325 additions and 76 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ English | [简体中文](./README.zh-CN.md)
| <a href="./exampleExpo/src/cube-3d/index.tsx">cube-3d</a> | <a href="./exampleExpo/src/blur-parallax/index.tsx">blur-parallax</a> | <a href="./exampleExpo/src/curve/index.tsx">curve</a> |
| <img src="assets/parallax-layers.gif"/> | <img src="assets/stack-cards.gif"/> | <img src="assets/flow.gif"/> |
| <a href="./exampleExpo/src/parallax-layers/index.tsx">parallax-layers</a> | <a href="./exampleExpo/src/stack-cards/index.tsx">stack-cards</a> | <a href="./exampleExpo/src/flow/index.tsx">flow</a> |
| <img src="assets/blur-rotate.gif"/> | ||
| <a href="./exampleExpo/src/blur-rotate/index.tsx">blur-rotate</a> | | |

## Table of contents

Expand Down
2 changes: 2 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
| <a href="./exampleExpo/src/cube-3d/index.tsx">cube-3d</a> | <a href="./exampleExpo/src/blur-parallax/index.tsx">blur-parallax</a> | <a href="./exampleExpo/src/curve/index.tsx">curve</a> |
| <img src="assets/parallax-layers.gif"/> | <img src="assets/stack-cards.gif"/> | <img src="assets/flow.gif"/> |
| <a href="./exampleExpo/src/parallax-layers/index.tsx">parallax-layers</a> | <a href="./exampleExpo/src/stack-cards/index.tsx">stack-cards</a> | <a href="./exampleExpo/src/flow/index.tsx">flow</a> |
| <img src="assets/blur-rotate.gif"/> | | |
| <a href="./exampleExpo/src/blur-rotate/index.tsx">blur-rotate</a> | | |

## 目录

Expand Down
Binary file added assets/blur-rotate.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions exampleExpo/src/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { useNavigation } from "@react-navigation/native";
import AdvancedParallaxComponent from "./pages/advanced-parallax";
import AnimTabBarComponent from "./pages/anim-tab-bar";
import BlurParallax from "./pages/blur-parallax";
import BlurRotate from "./pages/blur-rotate";
import Circular from "./pages/circular";
import ComplexComponent from "./pages/complex";
import Cube3D from "./pages/cube-3d";
Expand Down Expand Up @@ -64,6 +65,10 @@ export const LayoutsPage = [
];

export const CustomAnimations = [
{
name: "BlurRotate",
page: BlurRotate,
},
{
name: "Curve",
page: Curve,
Expand Down
4 changes: 2 additions & 2 deletions exampleExpo/src/components/SBItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { SBTextItem } from "./SBTextItem";

interface Props extends AnimateProps<ViewProps> {
style?: StyleProp<ViewStyle>
index: number
index?: number
pretty?: boolean
}

Expand All @@ -28,7 +28,7 @@ export const SBItem: React.FC<Props> = (props) => {
<Animated.View testID={testID} style={{ flex: 1 }} {...animatedViewProps}>
{isPretty
? (
<SBImageItem style={style} index={index} />
<SBImageItem style={style} index={index} showIndex={typeof index === "number"} />
)
: (
<SBTextItem style={style} index={index} />
Expand Down
4 changes: 2 additions & 2 deletions exampleExpo/src/components/SBTextItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { StyleSheet, Text, View } from "react-native";

interface Props {
style?: StyleProp<ViewStyle>
index: number
index?: number
}

export const SBTextItem: React.FC<Props> = ({ style, index }) => {
return (
<View style={[styles.container, style]}>
<Text style={{ fontSize: 30, color: "black" }}>{index}</Text>
{typeof index === "number" && <Text style={{ fontSize: 30, color: "black" }}>{index}</Text>}
</View>
);
};
Expand Down
2 changes: 2 additions & 0 deletions exampleExpo/src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Dimensions } from "react-native";

import { isWeb } from "../utils";

export const HEADER_HEIGHT = 100;

export const ElementsText = {
AUTOPLAY: "AutoPlay",
};
Expand Down
114 changes: 114 additions & 0 deletions exampleExpo/src/pages/blur-rotate/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import * as React from "react";
import type { ImageSourcePropType } from "react-native";
import { Image, StyleSheet, View } from "react-native";
import Animated, {
interpolate,
useAnimatedStyle,
} from "react-native-reanimated";
import Carousel from "react-native-reanimated-carousel";

import { BlurView as _BlurView } from "expo-blur";

import { parallaxLayout } from "./parallax";

import { SBItem } from "../../components/SBItem";
import SButton from "../../components/SButton";
import { ElementsText, HEADER_HEIGHT, window } from "../../constants";
import { fruitItems } from "../../utils/items";

const BlurView = Animated.createAnimatedComponent(_BlurView);

function Index() {
const [isAutoPlay, setIsAutoPlay] = React.useState(false);
const PAGE_WIDTH = window.width;
const PAGE_HEIGHT = window.height - HEADER_HEIGHT;
const ITEM_WIDTH = PAGE_WIDTH * 0.8;

return (
<View style={{ flex: 1 }}>
<Carousel
vertical
loop={false}
autoPlay={isAutoPlay}
style={{
width: PAGE_WIDTH,
height: PAGE_HEIGHT,
alignItems: "center",
}}
width={ITEM_WIDTH}
height={ITEM_WIDTH}
pagingEnabled={false}
snapEnabled={false}
data={[...fruitItems, ...fruitItems]}
renderItem={({ item, index, animationValue }) => {
return (
<CustomItem
key={index}
source={item}
animationValue={animationValue}
/>
);
}}
customAnimation={parallaxLayout(
{
size: ITEM_WIDTH,
},
)}
scrollAnimationDuration={1200}
/>
<SButton
onPress={() => {
setIsAutoPlay(!isAutoPlay);
}}
>
{ElementsText.AUTOPLAY}:{`${isAutoPlay}`}
</SButton>
</View>
);
}

interface ItemProps {
source: ImageSourcePropType
animationValue: Animated.SharedValue<number>
}
const CustomItem: React.FC<ItemProps> = ({ source, animationValue }) => {
const maskStyle = useAnimatedStyle(() => {
const opacity = interpolate(
animationValue.value,
[-0.5, 0, 1, 1.5],
[1, 0, 0, 1],
);

return {
opacity,
};
}, [animationValue]);

return (
<View
style={{
flex: 1,
borderRadius: 30,
overflow: "hidden",
justifyContent: "center",
alignItems: "center",
}}
>
<View style={{ width: "100%", height: "100%", position: "absolute" }}>
<SBItem pretty style={{ flex: 1 }} />
</View>
<Image
source={source}
resizeMode={"contain"}
style={{ width: "80%", height: "80%" }}
/>
<BlurView
intensity={50}
pointerEvents="none"
style={[StyleSheet.absoluteFill, maskStyle]}
/>
</View>
);
};

export default Index;
117 changes: 117 additions & 0 deletions exampleExpo/src/pages/blur-rotate/parallax.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { Extrapolate, interpolate } from "react-native-reanimated";
import type { IComputedDirectionTypes } from "react-native-reanimated-carousel";

import { withAnchorPoint } from "../../utils/anchor-point";

interface TBaseConfig {
size: number
}

export interface ILayoutConfig {
/**
* control prev/next item offset.
* @default 100
*/
parallaxScrollingOffset?: number
/**
* control prev/current/next item offset.
* @default 0.8
*/
parallaxScrollingScale?: number
/**
* control prev/next item offset.
* @default Math.pow(parallaxScrollingScale, 2)
*/
parallaxAdjacentItemScale?: number
}

export type TParallaxModeProps = IComputedDirectionTypes<{
/**
* Carousel Animated transitions.
*/
mode?: "parallax"
modeConfig?: ILayoutConfig
}>;

export function parallaxLayout(
baseConfig: TBaseConfig,
) {
const { size } = baseConfig;
// const {
// parallaxScrollingOffset = 100,
// parallaxScrollingScale = 0.8,
// parallaxAdjacentItemScale = parallaxScrollingScale ** 2,
// } = modeConfig;

const parallaxScrollingScale = 1;
const parallaxAdjacentItemScale = 0.8;
const parallaxScrollingOffset = -40;

return (value: number) => {
"worklet";
const translateY = interpolate(
value,
[-1, 0, 1],
[-size + parallaxScrollingOffset, 0, size - parallaxScrollingOffset],
);

const translateX = interpolate(
value,
[-1, 0, 1, 2],
[-size * 0.2, 0, 0, -size * 0.2],
);

const zIndex = interpolate(
value,
[-1, 0, 1, 2],
[0, size, size, 0],
Extrapolate.CLAMP,
);

const scale = interpolate(
value,
[-1, 0, 1, 2],
[
parallaxAdjacentItemScale,
parallaxScrollingScale,
parallaxScrollingScale,
parallaxAdjacentItemScale,
],
Extrapolate.CLAMP,
);

const transform = {
transform: [
{ translateY },
{ translateX },
{ perspective: 200 },
{
rotateY: `${interpolate(
value,
[-1, 0, 1, 2],
[20, 0, 0, 20],
Extrapolate.CLAMP,
)}deg`,
},
{
rotateZ: `${interpolate(
value,
[-1, 0, 1, 2],
[-20, 0, 0, -20],
Extrapolate.CLAMP,
)}deg`,
},
{ scale },
],
};

return {
...withAnchorPoint(
transform,
{ x: 0.5, y: 0.5 },
{ width: size, height: size },
),
zIndex,
};
};
}
17 changes: 9 additions & 8 deletions scripts/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
const path = require('path');
const child_process = require('child_process');
const child_process = require("child_process");
const path = require("path");

const root = path.resolve(__dirname, '..');
const root = path.resolve(__dirname, "..");
const args = process.argv.slice(2);
const options = {
cwd: process.cwd(),
env: process.env,
stdio: 'inherit',
encoding: 'utf-8',
stdio: "inherit",
encoding: "utf-8",
};

let result;

if (process.cwd() !== root || args.length) {
// We're not in the root of the project, or additional arguments were passed
// In this case, forward the command to `yarn`
result = child_process.spawnSync('yarn', args, options);
} else {
result = child_process.spawnSync("yarn", args, options);
}
else {
// If `yarn` is run without arguments, perform bootstrap
result = child_process.spawnSync('yarn', ['bootstrap'], options);
result = child_process.spawnSync("yarn", ["bootstrap"], options);
}

process.exitCode = result.status;
Loading

0 comments on commit cfa8ca9

Please sign in to comment.