Skip to content

Commit

Permalink
Refactor scaling constants
Browse files Browse the repository at this point in the history
  • Loading branch information
wyne committed Nov 11, 2023
1 parent bba005b commit 123ee72
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
4 changes: 3 additions & 1 deletion src/components/PlayerTiles/AdditionTile/AdditionTile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ const AdditionTile: React.FunctionComponent<Props> = ({
const sharedScale = useSharedValue(1);
const sharedOpacity = useSharedValue(0);

// Animation styles for resizing due to text changes
/**
* Animation styles for resizing due to text changes
*/
const animatedStyles = useAnimatedStyle(() => {
return {
opacity: sharedOpacity.value,
Expand Down
15 changes: 9 additions & 6 deletions src/components/PlayerTiles/AdditionTile/Helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,23 @@ export const exitingAnimation = ZoomOut.duration(animationDuration);
*/
export const layoutAnimation = Layout.easing(Easing.ease).duration(animationDuration);

export const singleLineScoreSizeMultiplier = 1.2;

export const multiLineScoreSizeMultiplier = 0.7;

export const baseScoreFontSize = 40;

export const scoreMathOpacity = 0.75;

/**
* Calculates the font size based on the maximum width.
* @param containerWidth The maximum width of the text.
* @returns The calculated font size.
*/
export const calculateFontSize = (containerWidth: number) => {
// const baseScale: number = Math.min(1 / stringLength * 200, 100);
const baseScale = 40;

let widthFactor: number = containerWidth / 200;

if (Number.isNaN(widthFactor)) { widthFactor = 1; }

return baseScale * widthFactor;
return baseScoreFontSize * widthFactor;
};

/**
Expand Down
6 changes: 3 additions & 3 deletions src/components/PlayerTiles/AdditionTile/ScoreBefore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
withTiming
} from 'react-native-reanimated';

import { calculateFontSize, animationDuration, enteringAnimation } from './Helpers';
import { calculateFontSize, animationDuration, enteringAnimation, multiLineScoreSizeMultiplier, singleLineScoreSizeMultiplier, scoreMathOpacity } from './Helpers';

interface Props {
roundScore: number;
Expand Down Expand Up @@ -34,7 +34,7 @@ const ScoreBefore: React.FunctionComponent<Props> = ({
};
});

const scaleFactor = roundScore == 0 ? 1.2 : .7;
const scaleFactor = roundScore == 0 ? singleLineScoreSizeMultiplier : multiLineScoreSizeMultiplier;

useEffect(() => {
fontSize.value = withTiming(
Expand All @@ -43,7 +43,7 @@ const ScoreBefore: React.FunctionComponent<Props> = ({
);

fontOpacity.value = withTiming(
roundScore == 0 ? 100 : 75,
roundScore == 0 ? 100 : scoreMathOpacity * 100,
{ duration: animationDuration }
);
}, [roundScore, containerWidth]);
Expand Down
17 changes: 8 additions & 9 deletions src/components/PlayerTiles/AdditionTile/ScoreRound.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
withTiming
} from 'react-native-reanimated';

import { calculateFontSize, animationDuration } from './Helpers';
import { calculateFontSize, animationDuration, multiLineScoreSizeMultiplier, scoreMathOpacity } from './Helpers';

interface Props {
roundScore: number;
Expand All @@ -15,21 +15,19 @@ interface Props {
}

const ScoreRound: React.FunctionComponent<Props> = ({ containerWidth, roundScore, fontColor }) => {
const fontSizeRound = useSharedValue(calculateFontSize(containerWidth));
const fontSize = useSharedValue(calculateFontSize(containerWidth));

const animatedStyles = useAnimatedStyle(() => {
return {
fontSize: fontSizeRound.value,
fontSize: fontSize.value,
};
});

const d = roundScore;


useEffect(() => {
const scaleFactor = .7;

fontSizeRound.value = withTiming(
calculateFontSize(containerWidth) * scaleFactor,
fontSize.value = withTiming(
calculateFontSize(containerWidth) * multiLineScoreSizeMultiplier,
{ duration: animationDuration }
);

Expand All @@ -44,7 +42,8 @@ const ScoreRound: React.FunctionComponent<Props> = ({ containerWidth, roundScore
<Animated.Text numberOfLines={1}
style={[animatedStyles, {
fontVariant: ['tabular-nums'],
color: fontColor, opacity: .75
color: fontColor,
opacity: scoreMathOpacity
}]}>
{roundScore > 0 && " + "}
{roundScore < 0 && " - "}
Expand Down

0 comments on commit 123ee72

Please sign in to comment.