Replies: 2 comments 3 replies
-
This is a very cool example.
Here you want to switch the font but without triggering a new react render.
The font property should be a reanimated value. Did you try that?
Maybe there is another way to do this with variable font widths? I don't
really know about them.
…On Sun, Mar 23, 2025 at 1:37 AM Akarshan Mishra ***@***.***> wrote:
Hi I have been trying to replicate this text animation
https://www.fancycomponents.dev/docs/components/text/breathing-text
with react native skia
but I found it is kinda hard to set the font weight based on reanimated 3
useSharedValue hook and with what I was able to do, the results are very
choppy to say the least
https://github.com/user-attachments/assets/b5e59c5c-039d-4881-997a-faa0849ec6b8
here is my code
import React, { memo, useEffect, useState } from "react";
import {
Easing,
useSharedValue,
withTiming,
withRepeat,
runOnJS,
useDerivedValue,
} from "react-native-reanimated";
import { matchFont, Text, useFonts } from ***@***.***/react-native-skia";
type FontWeightTextProps = {
xCord: number;
yCord: number;
text: string;
fontSize?: number;
};
const FontWeightText = memo((props: FontWeightTextProps) => {
const { xCord, yCord, text, fontSize = 32 } = props;
const fontMgr = useFonts({
OverusedGrotesk: [
require("../../../assets/fonts/Overused-Grotesk/OverusedGrotesk-Black.ttf"),
require("../../../assets/fonts/Overused-Grotesk/OverusedGrotesk-BlackItalic.ttf"),
require("../../../assets/fonts/Overused-Grotesk/OverusedGrotesk-Bold.ttf"),
require("../../../assets/fonts/Overused-Grotesk/OverusedGrotesk-BoldItalic.ttf"),
require("../../../assets/fonts/Overused-Grotesk/OverusedGrotesk-Book.ttf"),
require("../../../assets/fonts/Overused-Grotesk/OverusedGrotesk-BookItalic.ttf"),
require("../../../assets/fonts/Overused-Grotesk/OverusedGrotesk-ExtraBold.ttf"),
require("../../../assets/fonts/Overused-Grotesk/OverusedGrotesk-ExtraBoldItalic.ttf"),
require("../../../assets/fonts/Overused-Grotesk/OverusedGrotesk-Italic.ttf"),
require("../../../assets/fonts/Overused-Grotesk/OverusedGrotesk-Light.ttf"),
require("../../../assets/fonts/Overused-Grotesk/OverusedGrotesk-LightItalic.ttf"),
require("../../../assets/fonts/Overused-Grotesk/OverusedGrotesk-Medium.ttf"),
require("../../../assets/fonts/Overused-Grotesk/OverusedGrotesk-MediumItalic.ttf"),
require("../../../assets/fonts/Overused-Grotesk/OverusedGrotesk-Roman.ttf"),
require("../../../assets/fonts/Overused-Grotesk/OverusedGrotesk-SemiBold.ttf"),
require("../../../assets/fonts/Overused-Grotesk/OverusedGrotesk-SemiBoldItalic.ttf"),
],
});
const boldIndex = useSharedValue(0);
// Local state to trigger re-renders.
const [currentBold, setCurrentBold] = useState(0);
useEffect(() => {
boldIndex.value = withRepeat(
withTiming(text.length - 1, {
duration: 2000,
easing: Easing.linear,
}),
-1,
false
);
}, [text]);
// Update local state from the shared value.
useDerivedValue(() => {
runOnJS(setCurrentBold)(Math.round(boldIndex.get()));
}, [boldIndex]);
let currentX = xCord;
return (
<>
{text.split("").map((char, index) => {
const isBold = index === currentBold;
const fontStyle = {
fontFamily: "OverusedGrotesk",
fontWeight: isBold ? "600" : "300",
fontStyle: isBold ? "italic" : "normal",
fontSize: fontSize,
} as const;
// Get the matching font.
const font = fontMgr ? matchFont(fontStyle, fontMgr) : null;
const charWidth = font ? font.getTextWidth(char) : fontSize * 0.5;
const charXPosition = currentX;
currentX += charWidth;
return (
<Text
key={index}
font={font}
text={char}
x={charXPosition}
y={yCord}
opacity={0.9}
/>
);
})}
</>
);
});
export default FontWeightText;
Any help is appreciated. Thanks a bunch
—
Reply to this email directly, view it on GitHub
<#3046>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AACKXVUSTUL7LKZMJVEXSLL2VX65HAVCNFSM6AAAAABZSNO2XGVHI2DSMVQWIX3LMV43ERDJONRXK43TNFXW4OZYGEYTQMRTG4>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
1 reply
-
You could render each letter as an SVG and animate the stroke width of each letter, as well as skew them a little bit. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi I have been trying to replicate this text animation
https://www.fancycomponents.dev/docs/components/text/breathing-text
with react native skia
but I found it is kinda hard to set the font weight based on reanimated 3 useSharedValue hook and with what I was able to do, the results are very choppy to say the least
screenrecord.mov
here is my code
Any help is appreciated. Thanks a bunch
Beta Was this translation helpful? Give feedback.
All reactions