Skip to content

Commit

Permalink
fix: fix algorithm errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dohooo committed Sep 7, 2021
1 parent 2dcceb8 commit af61df8
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 34 deletions.
5 changes: 4 additions & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ export default function App() {
<View style={{ height: 300 }}>
<Carousel<{ color: string }>
width={width}
height={300}
data={[
{ color: 'red' },
{ color: 'purple' },
{ color: 'yellow' },
{ color: 'blue' },
{ color: 'pink' },
{ color: 'green' },
]}
renderItem={({ color }) => {
return (
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-reanimated-carousel",
"version": "0.1.0",
"version": "0.1.1",
"description": "Simple carousel component.fully implemented using Reanimated 2.Infinitely scrolling, very smooth.",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down
48 changes: 32 additions & 16 deletions src/Carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Animated, {
import { CarouselItem } from './CarouselItem';
import { fillNum } from './fillNum';
import type { TLayout } from './layouts';
import { DefaultLayout } from './layouts/DefaultLayout';
import { DefaultLayout } from './layouts/index';
import { useCarouselController } from './useCarouselController';
import { useComputedAnim } from './useComputedAnim';

Expand Down Expand Up @@ -103,7 +103,7 @@ function Carousel<T extends unknown = any>(
) {
const {
height = '100%',
data,
data: _data = [],
width,
layout = 'default',
renderItem,
Expand All @@ -117,6 +117,15 @@ function Carousel<T extends unknown = any>(
const { onPressItem } = props;
const handlerOffsetX = useSharedValue<number>(0);
const timer = React.useRef<NodeJS.Timer>();
const data = React.useMemo<T[]>(() => {
if (_data.length === 1) {
return [_data[0], _data[0], _data[0]];
}
if (_data.length === 2) {
return [_data[0], _data[1], _data[0], _data[1]];
}
return _data;
}, [_data]);
const computedAnimResult = useComputedAnim(width, data.length);
const { next, prev } = useCarouselController({ width, handlerOffsetX });
const offsetX = useDerivedValue(
Expand Down Expand Up @@ -188,8 +197,9 @@ function Carousel<T extends unknown = any>(
const Layouts = React.useMemo(() => {
switch (layout) {
case 'default':
default:
return DefaultLayout;
default:
return undefined;
}
}, [layout]);

Expand All @@ -202,6 +212,7 @@ function Carousel<T extends unknown = any>(
width,
height,
flexDirection: 'row',
position: 'relative',
},
style,
]}
Expand All @@ -217,19 +228,24 @@ function Carousel<T extends unknown = any>(
index={index}
key={index}
>
{/* <View style={{ backgroundColor: "red", flex: 1 }} /> */}
<Layouts
parallaxScrollingOffset={
parallaxScrollingOffset
}
parallaxScrollingScale={parallaxScrollingScale}
computedAnimResult={computedAnimResult}
width={width}
handlerOffsetX={offsetX}
index={index}
>
{renderItem(item, index)}
</Layouts>
{Layouts ? (
<Layouts
parallaxScrollingOffset={
parallaxScrollingOffset
}
parallaxScrollingScale={
parallaxScrollingScale
}
computedAnimResult={computedAnimResult}
width={width}
handlerOffsetX={offsetX}
index={index}
>
{renderItem(item, index)}
</Layouts>
) : (
renderItem(item, index)
)}
</CarouselItem>
);
})}
Expand Down
4 changes: 1 addition & 3 deletions src/layouts/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { DefaultLayout } from './DefaultLayout';
export { DefaultLayout } from './DefaultLayout';

export type TLayout = 'default';

export default { DefaultLayout };
10 changes: 6 additions & 4 deletions src/useComputedAnim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ export interface IComputedAnimResult {
MAX: number;
MIN: number;
WL: number;
LENGTH: number;
}

export function useComputedAnim(
width: number,
length: number
LENGTH: number
): IComputedAnimResult {
const MAX = ((length - 1) / 2) * width * 1;
const MIN = ((length - 1) / 2) * width * -1;
const WL = width * length;
const MAX = (LENGTH - 2) * width;
const MIN = -MAX;
const WL = width * LENGTH;

return {
MAX,
MIN,
WL,
LENGTH,
};
}
22 changes: 13 additions & 9 deletions src/useOffsetX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,32 @@ interface IOpts {

export const useOffsetX = (opts: IOpts) => {
const { handlerOffsetX, index, width, computedAnimResult } = opts;
const { MAX, WL, MIN } = computedAnimResult;
const { MAX, WL, MIN, LENGTH } = computedAnimResult;
const x = useDerivedValue(() => {
const Wi = width * index;
const startPos = Wi > MAX ? MAX - Wi : Wi < MIN ? MIN - Wi : Wi;
const inputRange = [
-WL,
-WL / 2 - startPos - 1,
-WL / 2 - startPos,
-((LENGTH - 2) * width + width / 2) - startPos - 1,
-((LENGTH - 2) * width + width / 2) - startPos,
0,
WL / 2 - startPos,
WL / 2 - startPos + 1,
(LENGTH - 2) * width + width / 2 - startPos,
(LENGTH - 2) * width + width / 2 - startPos + 1,
WL,
];
const outputRange = [
startPos,
WL / 2 - 1,
-WL / 2,
1.5 * width - 1,
-((LENGTH - 2) * width + width / 2),
startPos,
WL / 2,
-WL / 2 + 1,
(LENGTH - 2) * width + width / 2,
-(1.5 * width - 1),
startPos,
];
if (index === 1) {
console.log(JSON.stringify(inputRange));
console.log(JSON.stringify(outputRange));
}
return interpolate(
handlerOffsetX.value,
inputRange,
Expand Down

0 comments on commit af61df8

Please sign in to comment.