Skip to content

Commit

Permalink
added scale function
Browse files Browse the repository at this point in the history
  • Loading branch information
hannared authored and erksch committed Nov 11, 2022
1 parent 85c4fb2 commit ea52e7d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/WheelPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface Props {
itemHeight?: number;
containerStyle?: ViewStyle;
containerProps?: Omit<ViewProps, 'style'>;
scaleFunction?: (x: number) => number;
rotationFunction?: (x: number) => number;
opacityFunction?: (x: number) => number;
visibleRest?: number;
Expand All @@ -40,6 +41,7 @@ const WheelPicker: React.FC<Props> = ({
itemStyle = {},
itemTextStyle = {},
itemHeight = 40,
scaleFunction = (x: number) => 0.7 ** x,
rotationFunction = (x: number) => 1 - Math.pow(1 / 2, x),
opacityFunction = (x: number) => Math.pow(1 / 3, x),
visibleRest = 2,
Expand Down Expand Up @@ -138,6 +140,7 @@ const WheelPicker: React.FC<Props> = ({
textStyle={itemTextStyle}
height={itemHeight}
currentScrollIndex={currentScrollIndex}
scaleFunction={scaleFunction}
rotationFunction={rotationFunction}
opacityFunction={opacityFunction}
visibleRest={visibleRest}
Expand Down
24 changes: 23 additions & 1 deletion src/WheelPickerItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface ItemProps {
visibleRest: number;
rotationFunction: (x: number) => number;
opacityFunction: (x: number) => number;
scaleFunction: (x: number) => number;
}

const WheelPickerItem: React.FC<ItemProps> = ({
Expand All @@ -24,6 +25,7 @@ const WheelPickerItem: React.FC<ItemProps> = ({
currentScrollIndex,
opacityFunction,
rotationFunction,
scaleFunction
}) => {
const relativeScrollIndex = Animated.subtract(index, currentScrollIndex);

Expand Down Expand Up @@ -71,6 +73,26 @@ const WheelPickerItem: React.FC<ItemProps> = ({
})(),
});

const scale = relativeScrollIndex.interpolate({
inputRange: (() => {
const range = [0];
for (let i = 1; i <= visibleRest + 1; i++) {
range.unshift(-i);
range.push(i);
}
return range;
})(),
outputRange: (() => {
const range = [1.0];
for (let x = 1; x <= visibleRest + 1; x++) {
const y = scaleFunction(x);
range.unshift(y);
range.push(y);
}
return range;
})(),
});

const rotateX = relativeScrollIndex.interpolate({
inputRange: (() => {
const range = [0];
Expand All @@ -96,7 +118,7 @@ const WheelPickerItem: React.FC<ItemProps> = ({
style={[
styles.option,
style,
{ height, opacity, transform: [{ translateY }, { rotateX }] },
{ height, opacity, transform: [{ translateY }, { rotateX }, { scale }]},
]}
>
<Text style={textStyle}>{option}</Text>
Expand Down

0 comments on commit ea52e7d

Please sign in to comment.