Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Algorithmic Font Scaling #330

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 7 additions & 30 deletions src/components/PlayerTiles/AdditionTile/AdditionTile.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect, useState } from 'react';

Check failure on line 1 in src/components/PlayerTiles/AdditionTile/AdditionTile.tsx

View workflow job for this annotation

GitHub Actions / build (16.x)

'useState' is defined but never used
import { StyleSheet, Dimensions, LayoutChangeEvent } from 'react-native';

Check failure on line 2 in src/components/PlayerTiles/AdditionTile/AdditionTile.tsx

View workflow job for this annotation

GitHub Actions / build (16.x)

'Dimensions' is defined but never used

Check failure on line 2 in src/components/PlayerTiles/AdditionTile/AdditionTile.tsx

View workflow job for this annotation

GitHub Actions / build (16.x)

'LayoutChangeEvent' is defined but never used
import Animated, {
useSharedValue,
useAnimatedStyle,
Expand All @@ -7,7 +7,7 @@
withDelay,
} from 'react-native-reanimated';

import { animationDuration, calcPlayerFontSize, calcScoreLengthRatio } from './Helpers';
import { animationDuration, calcFontSize } from './Helpers';
import ScoreBefore from './ScoreBefore';
import ScoreAfter from './ScoreAfter';
import ScoreRound from './ScoreRound';
Expand All @@ -31,60 +31,37 @@
maxHeight,
index
}) => {
const [w, setW] = useState(1);
const [h, setH] = useState(1);

const sharedScale = useSharedValue(1);
const sharedOpacity = useSharedValue(0);

const animatedStyles = useAnimatedStyle(() => {
return {
transform: [{ scale: sharedScale.value }],
opacity: sharedOpacity.value,
};
});

const layoutHandler = (e: LayoutChangeEvent) => {
const { width, height } = e.nativeEvent.layout;
setH(height);
setW(width);
};

useEffect(() => {
const hs = maxWidth / w;
const vs = maxHeight / h;
const scoreLengthRatio = calcScoreLengthRatio(totalScore.toString().length);
const widthRatio = (900 + maxWidth) / (900 + Dimensions.get("window").width);

if (Math.min(hs, vs) > 0) {
const s = Math.min(widthRatio * scoreLengthRatio * hs, widthRatio * scoreLengthRatio * vs);
sharedScale.value = withDelay(animationDuration, withTiming(
Math.min(s, 3), { duration: animationDuration }
));
}

sharedOpacity.value = withDelay(100 + index * animationDuration / 2, withTiming(
1, { duration: animationDuration * 2 }
));
});

const playerNameFontSize = calcPlayerFontSize(playerName.length) * .8;
const playerNameFontSize = calcFontSize(maxWidth, playerName.length) * .8;

return (
<Animated.View style={[animatedStyles, { justifyContent: 'center' }]}
onLayout={layoutHandler}>
<Animated.Text style={[styles.name, { fontSize: playerNameFontSize, color: fontColor }]}
<Animated.View style={[animatedStyles, { justifyContent: 'center' }]}>
<Animated.Text style={[styles.name, { textTransform: 'uppercase', fontSize: playerNameFontSize, color: fontColor }]}
numberOfLines={1} >
{playerName}
</Animated.Text>
<Animated.View
style={styles.scoreLineOne} >
<ScoreBefore roundScore={roundScore} totalScore={totalScore}
<ScoreBefore maxWidth={Math.min(maxWidth, maxHeight)} roundScore={roundScore} totalScore={totalScore}
fontColor={fontColor} />
<ScoreRound roundScore={roundScore} totalScore={totalScore}
<ScoreRound maxWidth={Math.min(maxWidth, maxHeight)} roundScore={roundScore} totalScore={totalScore}
fontColor={fontColor} />
</Animated.View>
<ScoreAfter roundScore={roundScore} totalScore={totalScore}
<ScoreAfter maxWidth={Math.min(maxWidth, maxHeight)} roundScore={roundScore} totalScore={totalScore}
fontColor={fontColor} />
</Animated.View>
);
Expand Down
53 changes: 6 additions & 47 deletions src/components/PlayerTiles/AdditionTile/Helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,15 @@ export const layoutAnimation = Layout.easing(Easing.ease).duration(animationDura

import { withTiming } from 'react-native-reanimated';

export const calcPlayerFontSize = (length: number) => {
if (length <= 3) {
return 50;
} else if (length <= 4) {
return 40;
} else if (length <= 5) {
return 40;
} else if (length <= 6) {
return 46;
} else if (length <= 7) {
return 43;
} else if (length <= 8) {
return 40;
} else if (length <= 8) {
return 37;
} else {
return 34;
}
};
export const calcFontSize = (maxWidth: number, length: number) => {
const baseScale = Math.min(1 / length * 200, 100);
let widthFactor: number = maxWidth / 200;

export const calcFontSize = (length: number) => {
if (length <= 3) {
return 50 * calcScoreLengthRatio(length);
} else if (length <= 4) {
return 40 * calcScoreLengthRatio(length);
} else if (length <= 5) {
return 40 * calcScoreLengthRatio(length);
} else if (length <= 6) {
return 46 * calcScoreLengthRatio(length);
} else if (length <= 7) {
return 43 * calcScoreLengthRatio(length);
} else if (length <= 8) {
return 40 * calcScoreLengthRatio(length);
} else if (length <= 8) {
return 37 * calcScoreLengthRatio(length);
} else {
return 34 * calcScoreLengthRatio(length);
if (Number.isNaN(widthFactor)) {
widthFactor = 1;
}
};

export const calcScoreLengthRatio = (length: number) => {
if (length <= 3) {
return .8;
} else if (length <= 4) {
return .75;
} else if (length <= 5) {
return .7;
} else {
return .6;
}
return baseScale * widthFactor;
};

export const ZoomOutFadeOut = () => {
Expand Down
9 changes: 5 additions & 4 deletions src/components/PlayerTiles/AdditionTile/ScoreAfter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ interface Props {
roundScore: number;
totalScore: number;
fontColor: string;
maxWidth: number;
}

const ScoreAfter: React.FunctionComponent<Props> = ({ roundScore, totalScore, fontColor }) => {
const fontSize = useSharedValue(calcFontSize(totalScore.toString().length));
const ScoreAfter: React.FunctionComponent<Props> = ({ maxWidth, roundScore, totalScore, fontColor }) => {
const fontSize = useSharedValue(calcFontSize(maxWidth, totalScore.toString().length));
const opacity = useSharedValue(1);

const animatedStyles = useAnimatedStyle(() => {
Expand All @@ -28,12 +29,12 @@ const ScoreAfter: React.FunctionComponent<Props> = ({ roundScore, totalScore, fo

useEffect(() => {
fontSize.value = withTiming(
roundScore == 0 ? 1 : calcFontSize(totalScore.toString().length), { duration: animationDuration },
roundScore == 0 ? 1 : calcFontSize(maxWidth, totalScore.toString().length), { duration: animationDuration },
);
opacity.value = withTiming(
roundScore == 0 ? 0 : 1, { duration: animationDuration },
);
}, [roundScore]);
}, [roundScore, maxWidth]);

return (
<Animated.View entering={enteringAnimation} exiting={ZoomOutFadeOut}>
Expand Down
13 changes: 8 additions & 5 deletions src/components/PlayerTiles/AdditionTile/ScoreBefore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,33 @@ interface Props {
roundScore: number;
totalScore: number;
fontColor: string;
maxWidth: number;
}

const ScoreBefore: React.FunctionComponent<Props> = ({ roundScore, totalScore, fontColor }) => {
const ScoreBefore: React.FunctionComponent<Props> = ({ maxWidth, roundScore, totalScore, fontColor }) => {
const firstRowLength = (roundScore == 0 ? 0 : roundScore.toString().length + 3) + totalScore.toString().length;
const d = totalScore - roundScore;

const fontSize = useSharedValue(calcFontSize(firstRowLength));
const fontSize = useSharedValue(calcFontSize(maxWidth, firstRowLength));

const fontOpacity = useSharedValue(100);
const animatedStyles = useAnimatedStyle(() => {
return {
fontSize: roundScore == 0 ? fontSize.value : fontSize.value * .8,
fontSize: fontSize.value,
fontWeight: roundScore == 0 ? 'bold' : 'normal',
opacity: fontOpacity.value / 100,
};
});

useEffect(() => {
fontSize.value = withTiming(
calcFontSize(firstRowLength), { duration: animationDuration }
calcFontSize(maxWidth, firstRowLength), { duration: animationDuration }
);

fontOpacity.value = withTiming(
roundScore == 0 ? 100 : 75, { duration: animationDuration }
);
}, [roundScore]);
}, [roundScore, maxWidth]);

return (
<Animated.View entering={enteringAnimation}>
Expand Down
10 changes: 6 additions & 4 deletions src/components/PlayerTiles/AdditionTile/ScoreRound.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ interface Props {
roundScore: number;
totalScore: number;
fontColor: string;
maxWidth: number;
}

const ScoreRound: React.FunctionComponent<Props> = ({ roundScore, totalScore, fontColor }) => {
const ScoreRound: React.FunctionComponent<Props> = ({ maxWidth, roundScore, totalScore, fontColor }) => {
const firstRowLength = (roundScore == 0 ? 0 : roundScore.toString().length + 3) + totalScore.toString().length;
const fontSizeRound = useSharedValue(calcFontSize(firstRowLength));
const fontSizeRound = useSharedValue(calcFontSize(maxWidth, firstRowLength));
const animatedStyles = useAnimatedStyle(() => {
return {
fontSize: fontSizeRound.value,
Expand All @@ -27,9 +28,10 @@ const ScoreRound: React.FunctionComponent<Props> = ({ roundScore, totalScore, fo

useEffect(() => {
fontSizeRound.value = withTiming(
calcFontSize(firstRowLength) * .8, { duration: animationDuration }
calcFontSize(maxWidth, firstRowLength), { duration: animationDuration }
);
}, [roundScore]);

}, [roundScore, maxWidth]);

if (roundScore == 0) {
return <></>;
Expand Down
Loading