nativebits-animated-text 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +17 -0
- package/README.md +564 -2
- package/docs/android-demo.gif +0 -0
- package/lib/commonjs/index.js +146 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/text/BlurText.js +146 -0
- package/lib/commonjs/text/BlurText.js.map +1 -0
- package/lib/commonjs/text/BounceText.js +83 -0
- package/lib/commonjs/text/BounceText.js.map +1 -0
- package/lib/commonjs/text/CountUpText.js +77 -0
- package/lib/commonjs/text/CountUpText.js.map +1 -0
- package/lib/commonjs/text/ElasticText.js +85 -0
- package/lib/commonjs/text/ElasticText.js.map +1 -0
- package/lib/commonjs/text/ExplosionText.js +191 -0
- package/lib/commonjs/text/ExplosionText.js.map +1 -0
- package/lib/commonjs/text/FlipText.js +101 -0
- package/lib/commonjs/text/FlipText.js.map +1 -0
- package/lib/commonjs/text/GlitchText.js +329 -0
- package/lib/commonjs/text/GlitchText.js.map +1 -0
- package/lib/commonjs/text/GradientText.js +73 -0
- package/lib/commonjs/text/GradientText.js.map +1 -0
- package/lib/commonjs/text/MorphText.js +192 -0
- package/lib/commonjs/text/MorphText.js.map +1 -0
- package/lib/commonjs/text/NeonText.js +134 -0
- package/lib/commonjs/text/NeonText.js.map +1 -0
- package/lib/commonjs/text/ParticleText.js +328 -0
- package/lib/commonjs/text/ParticleText.js.map +1 -0
- package/lib/commonjs/text/RevealText.js +142 -0
- package/lib/commonjs/text/RevealText.js.map +1 -0
- package/lib/commonjs/text/RotateText.js +99 -0
- package/lib/commonjs/text/RotateText.js.map +1 -0
- package/lib/commonjs/text/ScrambleText.js +82 -0
- package/lib/commonjs/text/ScrambleText.js.map +1 -0
- package/lib/commonjs/text/ShinyText.js +87 -0
- package/lib/commonjs/text/ShinyText.js.map +1 -0
- package/lib/commonjs/text/SplitText.js +81 -0
- package/lib/commonjs/text/SplitText.js.map +1 -0
- package/lib/commonjs/text/TextWave.js +130 -0
- package/lib/commonjs/text/TextWave.js.map +1 -0
- package/lib/commonjs/text/ThreeDText.js +145 -0
- package/lib/commonjs/text/ThreeDText.js.map +1 -0
- package/lib/commonjs/text/TypewriterText.js +119 -0
- package/lib/commonjs/text/TypewriterText.js.map +1 -0
- package/lib/commonjs/text/WaveText.js +79 -0
- package/lib/commonjs/text/WaveText.js.map +1 -0
- package/lib/module/index.js +23 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/text/BlurText.js +139 -0
- package/lib/module/text/BlurText.js.map +1 -0
- package/lib/module/text/BounceText.js +77 -0
- package/lib/module/text/BounceText.js.map +1 -0
- package/lib/module/text/CountUpText.js +70 -0
- package/lib/module/text/CountUpText.js.map +1 -0
- package/lib/module/text/ElasticText.js +78 -0
- package/lib/module/text/ElasticText.js.map +1 -0
- package/lib/module/text/ExplosionText.js +184 -0
- package/lib/module/text/ExplosionText.js.map +1 -0
- package/lib/module/text/FlipText.js +94 -0
- package/lib/module/text/FlipText.js.map +1 -0
- package/lib/module/text/GlitchText.js +322 -0
- package/lib/module/text/GlitchText.js.map +1 -0
- package/lib/module/text/GradientText.js +66 -0
- package/lib/module/text/GradientText.js.map +1 -0
- package/lib/module/text/MorphText.js +185 -0
- package/lib/module/text/MorphText.js.map +1 -0
- package/lib/module/text/NeonText.js +127 -0
- package/lib/module/text/NeonText.js.map +1 -0
- package/lib/module/text/ParticleText.js +321 -0
- package/lib/module/text/ParticleText.js.map +1 -0
- package/lib/module/text/RevealText.js +135 -0
- package/lib/module/text/RevealText.js.map +1 -0
- package/lib/module/text/RotateText.js +93 -0
- package/lib/module/text/RotateText.js.map +1 -0
- package/lib/module/text/ScrambleText.js +76 -0
- package/lib/module/text/ScrambleText.js.map +1 -0
- package/lib/module/text/ShinyText.js +80 -0
- package/lib/module/text/ShinyText.js.map +1 -0
- package/lib/module/text/SplitText.js +74 -0
- package/lib/module/text/SplitText.js.map +1 -0
- package/lib/module/text/TextWave.js +123 -0
- package/lib/module/text/TextWave.js.map +1 -0
- package/lib/module/text/ThreeDText.js +138 -0
- package/lib/module/text/ThreeDText.js.map +1 -0
- package/lib/module/text/TypewriterText.js +113 -0
- package/lib/module/text/TypewriterText.js.map +1 -0
- package/lib/module/text/WaveText.js +72 -0
- package/lib/module/text/WaveText.js.map +1 -0
- package/lib/typescript/index.d.ts +41 -0
- package/lib/typescript/index.d.ts.map +1 -0
- package/lib/typescript/text/BlurText.d.ts +16 -0
- package/lib/typescript/text/BlurText.d.ts.map +1 -0
- package/lib/typescript/text/BounceText.d.ts +15 -0
- package/lib/typescript/text/BounceText.d.ts.map +1 -0
- package/lib/typescript/text/CountUpText.d.ts +20 -0
- package/lib/typescript/text/CountUpText.d.ts.map +1 -0
- package/lib/typescript/text/ElasticText.d.ts +17 -0
- package/lib/typescript/text/ElasticText.d.ts.map +1 -0
- package/lib/typescript/text/ExplosionText.d.ts +23 -0
- package/lib/typescript/text/ExplosionText.d.ts.map +1 -0
- package/lib/typescript/text/FlipText.d.ts +17 -0
- package/lib/typescript/text/FlipText.d.ts.map +1 -0
- package/lib/typescript/text/GlitchText.d.ts +19 -0
- package/lib/typescript/text/GlitchText.d.ts.map +1 -0
- package/lib/typescript/text/GradientText.d.ts +12 -0
- package/lib/typescript/text/GradientText.d.ts.map +1 -0
- package/lib/typescript/text/MorphText.d.ts +18 -0
- package/lib/typescript/text/MorphText.d.ts.map +1 -0
- package/lib/typescript/text/NeonText.d.ts +16 -0
- package/lib/typescript/text/NeonText.d.ts.map +1 -0
- package/lib/typescript/text/ParticleText.d.ts +23 -0
- package/lib/typescript/text/ParticleText.d.ts.map +1 -0
- package/lib/typescript/text/RevealText.d.ts +19 -0
- package/lib/typescript/text/RevealText.d.ts.map +1 -0
- package/lib/typescript/text/RotateText.d.ts +17 -0
- package/lib/typescript/text/RotateText.d.ts.map +1 -0
- package/lib/typescript/text/ScrambleText.d.ts +14 -0
- package/lib/typescript/text/ScrambleText.d.ts.map +1 -0
- package/lib/typescript/text/ShinyText.d.ts +15 -0
- package/lib/typescript/text/ShinyText.d.ts.map +1 -0
- package/lib/typescript/text/SplitText.d.ts +28 -0
- package/lib/typescript/text/SplitText.d.ts.map +1 -0
- package/lib/typescript/text/TextWave.d.ts +15 -0
- package/lib/typescript/text/TextWave.d.ts.map +1 -0
- package/lib/typescript/text/ThreeDText.d.ts +24 -0
- package/lib/typescript/text/ThreeDText.d.ts.map +1 -0
- package/lib/typescript/text/TypewriterText.d.ts +21 -0
- package/lib/typescript/text/TypewriterText.d.ts.map +1 -0
- package/lib/typescript/text/WaveText.d.ts +14 -0
- package/lib/typescript/text/WaveText.d.ts.map +1 -0
- package/package.json +48 -4
- package/src/index.ts +45 -0
- package/src/text/BlurText.tsx +243 -0
- package/src/text/BounceText.tsx +158 -0
- package/src/text/CountUpText.tsx +153 -0
- package/src/text/ElasticText.tsx +143 -0
- package/src/text/ExplosionText.tsx +242 -0
- package/src/text/FlipText.tsx +161 -0
- package/src/text/GlitchText.tsx +525 -0
- package/src/text/GradientText.tsx +118 -0
- package/src/text/MorphText.tsx +305 -0
- package/src/text/NeonText.tsx +205 -0
- package/src/text/ParticleText.tsx +489 -0
- package/src/text/RevealText.tsx +255 -0
- package/src/text/RotateText.tsx +179 -0
- package/src/text/ScrambleText.tsx +137 -0
- package/src/text/ShinyText.tsx +163 -0
- package/src/text/SplitText.tsx +122 -0
- package/src/text/TextWave.tsx +223 -0
- package/src/text/ThreeDText.tsx +268 -0
- package/src/text/TypewriterText.tsx +189 -0
- package/src/text/WaveText.tsx +132 -0
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import React, { memo, useEffect } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
Platform,
|
|
4
|
+
View,
|
|
5
|
+
type StyleProp,
|
|
6
|
+
type TextStyle,
|
|
7
|
+
type ViewStyle,
|
|
8
|
+
} from 'react-native';
|
|
9
|
+
import Animated, {
|
|
10
|
+
Easing,
|
|
11
|
+
useAnimatedStyle,
|
|
12
|
+
useSharedValue,
|
|
13
|
+
withDelay,
|
|
14
|
+
withTiming,
|
|
15
|
+
} from 'react-native-reanimated';
|
|
16
|
+
|
|
17
|
+
export interface SplitTextProps {
|
|
18
|
+
/** Text to reveal one character at a time. */
|
|
19
|
+
text: string;
|
|
20
|
+
/** Duration of each character animation, in milliseconds. */
|
|
21
|
+
duration?: number;
|
|
22
|
+
/** Delay between the start of adjacent character animations, in milliseconds. */
|
|
23
|
+
stagger?: number;
|
|
24
|
+
/** Delay before the first character starts, in milliseconds. */
|
|
25
|
+
delay?: number;
|
|
26
|
+
/** Initial vertical offset, in logical pixels. */
|
|
27
|
+
fromY?: number;
|
|
28
|
+
/** Text style applied to each character. */
|
|
29
|
+
style?: StyleProp<TextStyle>;
|
|
30
|
+
/** Optional style applied to each animated character. */
|
|
31
|
+
characterStyle?: StyleProp<TextStyle>;
|
|
32
|
+
/** Optional style for the wrapping container. */
|
|
33
|
+
containerStyle?: StyleProp<ViewStyle>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface CharacterProps {
|
|
37
|
+
character: string;
|
|
38
|
+
index: number;
|
|
39
|
+
duration: number;
|
|
40
|
+
stagger: number;
|
|
41
|
+
delay: number;
|
|
42
|
+
fromY: number;
|
|
43
|
+
style?: StyleProp<TextStyle>;
|
|
44
|
+
characterStyle?: StyleProp<TextStyle>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const AnimatedCharacter = memo(({
|
|
48
|
+
character,
|
|
49
|
+
index,
|
|
50
|
+
duration,
|
|
51
|
+
stagger,
|
|
52
|
+
delay,
|
|
53
|
+
fromY,
|
|
54
|
+
style,
|
|
55
|
+
characterStyle,
|
|
56
|
+
}: CharacterProps) => {
|
|
57
|
+
const progress = useSharedValue(0);
|
|
58
|
+
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
progress.value = withDelay(
|
|
61
|
+
delay + index * stagger,
|
|
62
|
+
withTiming(1, {
|
|
63
|
+
duration,
|
|
64
|
+
easing: Easing.out(Easing.cubic),
|
|
65
|
+
}),
|
|
66
|
+
);
|
|
67
|
+
}, [delay, duration, index, progress, stagger]);
|
|
68
|
+
|
|
69
|
+
const animatedStyle = useAnimatedStyle(() => ({
|
|
70
|
+
opacity: progress.value,
|
|
71
|
+
transform: [{ translateY: (1 - progress.value) * fromY }],
|
|
72
|
+
}), Platform.OS === 'web' ? [fromY] : undefined);
|
|
73
|
+
|
|
74
|
+
return (
|
|
75
|
+
<Animated.Text style={[style, characterStyle, animatedStyle]}>
|
|
76
|
+
{character === ' ' ? '\u00A0' : character}
|
|
77
|
+
</Animated.Text>
|
|
78
|
+
);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
AnimatedCharacter.displayName = 'AnimatedCharacter';
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Reveals text character-by-character with a staggered fade and vertical rise.
|
|
85
|
+
*
|
|
86
|
+
* The initial MVP splits text by Unicode code point. Complex emoji made from
|
|
87
|
+
* multiple code points may animate as multiple pieces.
|
|
88
|
+
*/
|
|
89
|
+
export const SplitText = ({
|
|
90
|
+
text,
|
|
91
|
+
duration = 450,
|
|
92
|
+
stagger = 35,
|
|
93
|
+
delay = 0,
|
|
94
|
+
fromY = 16,
|
|
95
|
+
style,
|
|
96
|
+
characterStyle,
|
|
97
|
+
containerStyle,
|
|
98
|
+
}: SplitTextProps) => {
|
|
99
|
+
const characters = Array.from(text);
|
|
100
|
+
|
|
101
|
+
return (
|
|
102
|
+
<View
|
|
103
|
+
accessible
|
|
104
|
+
accessibilityLabel={text}
|
|
105
|
+
style={[{ flexDirection: 'row', flexWrap: 'wrap', alignItems: 'flex-start' }, containerStyle]}
|
|
106
|
+
>
|
|
107
|
+
{characters.map((character, index) => (
|
|
108
|
+
<AnimatedCharacter
|
|
109
|
+
key={`${index}-${character}`}
|
|
110
|
+
character={character}
|
|
111
|
+
index={index}
|
|
112
|
+
duration={duration}
|
|
113
|
+
stagger={stagger}
|
|
114
|
+
delay={delay}
|
|
115
|
+
fromY={fromY}
|
|
116
|
+
style={style}
|
|
117
|
+
characterStyle={characterStyle}
|
|
118
|
+
/>
|
|
119
|
+
))}
|
|
120
|
+
</View>
|
|
121
|
+
);
|
|
122
|
+
};
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
|
|
2
|
+
import React, { useEffect, useMemo } from 'react';
|
|
3
|
+
import {
|
|
4
|
+
View,
|
|
5
|
+
type TextStyle,
|
|
6
|
+
type ViewStyle,
|
|
7
|
+
} from 'react-native';
|
|
8
|
+
import {
|
|
9
|
+
Canvas,
|
|
10
|
+
Text as SkiaText,
|
|
11
|
+
matchFont,
|
|
12
|
+
} from '@shopify/react-native-skia';
|
|
13
|
+
import Animated, {
|
|
14
|
+
cancelAnimation,
|
|
15
|
+
Easing,
|
|
16
|
+
useDerivedValue,
|
|
17
|
+
useSharedValue,
|
|
18
|
+
withDelay,
|
|
19
|
+
withRepeat,
|
|
20
|
+
withSequence,
|
|
21
|
+
withTiming,
|
|
22
|
+
type SharedValue,
|
|
23
|
+
} from 'react-native-reanimated';
|
|
24
|
+
|
|
25
|
+
export interface TextWaveProps {
|
|
26
|
+
text: string;
|
|
27
|
+
amplitude?: number;
|
|
28
|
+
wavelength?: number;
|
|
29
|
+
duration?: number;
|
|
30
|
+
delay?: number;
|
|
31
|
+
direction?: 'left' | 'right';
|
|
32
|
+
loop?: boolean;
|
|
33
|
+
style?: TextStyle;
|
|
34
|
+
containerStyle?: ViewStyle;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface WaveCharacterProps {
|
|
38
|
+
character: string;
|
|
39
|
+
x: number;
|
|
40
|
+
index: number;
|
|
41
|
+
baseline: number;
|
|
42
|
+
amplitude: number;
|
|
43
|
+
wavelength: number;
|
|
44
|
+
direction: 'left' | 'right';
|
|
45
|
+
progress: SharedValue<number>;
|
|
46
|
+
font: ReturnType<typeof matchFont>;
|
|
47
|
+
color: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const WaveCharacter = ({
|
|
51
|
+
character,
|
|
52
|
+
x,
|
|
53
|
+
index,
|
|
54
|
+
baseline,
|
|
55
|
+
amplitude,
|
|
56
|
+
wavelength,
|
|
57
|
+
direction,
|
|
58
|
+
progress,
|
|
59
|
+
font,
|
|
60
|
+
color,
|
|
61
|
+
}: WaveCharacterProps) => {
|
|
62
|
+
const animatedY = useDerivedValue(() => {
|
|
63
|
+
const directionMultiplier = direction === 'right' ? 1 : -1;
|
|
64
|
+
const phase = progress.value * directionMultiplier * Math.PI * 2;
|
|
65
|
+
const characterPhase = (index / wavelength) * Math.PI * 2;
|
|
66
|
+
|
|
67
|
+
return (
|
|
68
|
+
baseline +
|
|
69
|
+
Math.sin(phase + characterPhase) * amplitude
|
|
70
|
+
);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
return (
|
|
74
|
+
<SkiaText
|
|
75
|
+
text={character}
|
|
76
|
+
x={x}
|
|
77
|
+
y={animatedY}
|
|
78
|
+
font={font}
|
|
79
|
+
color={color}
|
|
80
|
+
/>
|
|
81
|
+
);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export const TextWave = ({
|
|
85
|
+
text,
|
|
86
|
+
amplitude = 8,
|
|
87
|
+
wavelength = 6,
|
|
88
|
+
duration = 1800,
|
|
89
|
+
delay = 0,
|
|
90
|
+
direction = 'right',
|
|
91
|
+
loop = true,
|
|
92
|
+
style,
|
|
93
|
+
containerStyle,
|
|
94
|
+
}: TextWaveProps) => {
|
|
95
|
+
const progress = useSharedValue(0);
|
|
96
|
+
|
|
97
|
+
const fontSize = Number(style?.fontSize ?? 32);
|
|
98
|
+
const color = String(style?.color ?? '#FFFFFF');
|
|
99
|
+
|
|
100
|
+
const font = useMemo(
|
|
101
|
+
() =>
|
|
102
|
+
matchFont({
|
|
103
|
+
fontFamily: style?.fontFamily ?? 'Arial',
|
|
104
|
+
fontSize,
|
|
105
|
+
fontWeight: style?.fontWeight as
|
|
106
|
+
| 'normal'
|
|
107
|
+
| 'bold'
|
|
108
|
+
| '100'
|
|
109
|
+
| '200'
|
|
110
|
+
| '300'
|
|
111
|
+
| '400'
|
|
112
|
+
| '500'
|
|
113
|
+
| '600'
|
|
114
|
+
| '700'
|
|
115
|
+
| '800'
|
|
116
|
+
| '900'
|
|
117
|
+
| undefined ?? 'normal',
|
|
118
|
+
fontStyle: style?.fontStyle ?? 'normal',
|
|
119
|
+
}),
|
|
120
|
+
[
|
|
121
|
+
style?.fontFamily,
|
|
122
|
+
style?.fontWeight,
|
|
123
|
+
style?.fontStyle,
|
|
124
|
+
fontSize,
|
|
125
|
+
],
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
const characters = Array.from(text);
|
|
129
|
+
|
|
130
|
+
const { positions, textWidth } = useMemo(() => {
|
|
131
|
+
const glyphIds = font.getGlyphIDs(text, characters.length);
|
|
132
|
+
const glyphWidths = font.getGlyphWidths(glyphIds);
|
|
133
|
+
|
|
134
|
+
let currentX = 0;
|
|
135
|
+
|
|
136
|
+
const positions = characters.map((character, index) => {
|
|
137
|
+
const position = {
|
|
138
|
+
character,
|
|
139
|
+
x: currentX,
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
currentX += glyphWidths[index] ?? 0;
|
|
143
|
+
|
|
144
|
+
return position;
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
return {
|
|
148
|
+
positions,
|
|
149
|
+
textWidth: currentX,
|
|
150
|
+
};
|
|
151
|
+
}, [text, font]);
|
|
152
|
+
|
|
153
|
+
useEffect(() => {
|
|
154
|
+
progress.value = 0;
|
|
155
|
+
|
|
156
|
+
const waveAnimation = withTiming(1, {
|
|
157
|
+
duration,
|
|
158
|
+
easing: Easing.linear,
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
const returnAnimation = withTiming(0, {
|
|
162
|
+
duration,
|
|
163
|
+
easing: Easing.inOut(Easing.cubic),
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
progress.value = withDelay(
|
|
167
|
+
delay,
|
|
168
|
+
loop
|
|
169
|
+
? withRepeat(waveAnimation, -1, false)
|
|
170
|
+
: withSequence(waveAnimation, returnAnimation),
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
return () => {
|
|
174
|
+
cancelAnimation(progress);
|
|
175
|
+
};
|
|
176
|
+
}, [duration, delay, loop, progress]);
|
|
177
|
+
|
|
178
|
+
const metrics = font.getMetrics();
|
|
179
|
+
|
|
180
|
+
const padding = Math.ceil(fontSize * 0.2);
|
|
181
|
+
const verticalPadding = padding + amplitude;
|
|
182
|
+
|
|
183
|
+
const canvasWidth = textWidth + padding * 2;
|
|
184
|
+
const canvasHeight =
|
|
185
|
+
metrics.descent -
|
|
186
|
+
metrics.ascent +
|
|
187
|
+
verticalPadding * 2;
|
|
188
|
+
|
|
189
|
+
const baseline = -metrics.ascent + verticalPadding;
|
|
190
|
+
|
|
191
|
+
return (
|
|
192
|
+
<View style={containerStyle}>
|
|
193
|
+
<Canvas
|
|
194
|
+
style={{
|
|
195
|
+
width: canvasWidth,
|
|
196
|
+
height: canvasHeight,
|
|
197
|
+
}}
|
|
198
|
+
>
|
|
199
|
+
{positions.map((item, index) => {
|
|
200
|
+
if (item.character === ' ') {
|
|
201
|
+
return null;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return (
|
|
205
|
+
<WaveCharacter
|
|
206
|
+
key={`${index}-${item.character}`}
|
|
207
|
+
character={item.character}
|
|
208
|
+
x={item.x + padding}
|
|
209
|
+
index={index}
|
|
210
|
+
baseline={baseline}
|
|
211
|
+
amplitude={amplitude}
|
|
212
|
+
wavelength={Math.max(1, wavelength)}
|
|
213
|
+
direction={direction}
|
|
214
|
+
progress={progress}
|
|
215
|
+
font={font}
|
|
216
|
+
color={color}
|
|
217
|
+
/>
|
|
218
|
+
);
|
|
219
|
+
})}
|
|
220
|
+
</Canvas>
|
|
221
|
+
</View>
|
|
222
|
+
);
|
|
223
|
+
};
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
|
|
2
|
+
import React, { useEffect, useMemo } from 'react';
|
|
3
|
+
import {
|
|
4
|
+
View,
|
|
5
|
+
type TextStyle,
|
|
6
|
+
type ViewStyle,
|
|
7
|
+
} from 'react-native';
|
|
8
|
+
import {
|
|
9
|
+
Canvas,
|
|
10
|
+
LinearGradient,
|
|
11
|
+
matchFont,
|
|
12
|
+
Text as SkiaText,
|
|
13
|
+
vec,
|
|
14
|
+
} from '@shopify/react-native-skia';
|
|
15
|
+
import Animated, {
|
|
16
|
+
cancelAnimation,
|
|
17
|
+
Easing,
|
|
18
|
+
useAnimatedStyle,
|
|
19
|
+
useDerivedValue,
|
|
20
|
+
useSharedValue,
|
|
21
|
+
withRepeat,
|
|
22
|
+
withTiming,
|
|
23
|
+
} from 'react-native-reanimated';
|
|
24
|
+
|
|
25
|
+
export interface ThreeDTextProps {
|
|
26
|
+
text: string;
|
|
27
|
+
color?: string;
|
|
28
|
+
depthColor?: string;
|
|
29
|
+
depth?: number;
|
|
30
|
+
depthOffset?: number;
|
|
31
|
+
bevelSize?: number;
|
|
32
|
+
bevelColor?: string;
|
|
33
|
+
shadowColor?: string;
|
|
34
|
+
gradientColors?: string[];
|
|
35
|
+
rotationX?: number;
|
|
36
|
+
rotationY?: number;
|
|
37
|
+
perspective?: number;
|
|
38
|
+
duration?: number;
|
|
39
|
+
animated?: boolean;
|
|
40
|
+
loop?: boolean;
|
|
41
|
+
shine?: boolean;
|
|
42
|
+
style?: TextStyle;
|
|
43
|
+
containerStyle?: ViewStyle;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export const ThreeDText = ({
|
|
47
|
+
text,
|
|
48
|
+
color,
|
|
49
|
+
depthColor = '#542080',
|
|
50
|
+
depth = 16,
|
|
51
|
+
depthOffset = 0.7,
|
|
52
|
+
bevelSize = 1,
|
|
53
|
+
bevelColor = '#F5D0FE',
|
|
54
|
+
shadowColor = '#160824',
|
|
55
|
+
gradientColors = ['#F5D0FE', '#C084FC', '#7E22CE'],
|
|
56
|
+
rotationX = 8,
|
|
57
|
+
rotationY = 12,
|
|
58
|
+
perspective = 800,
|
|
59
|
+
duration = 3000,
|
|
60
|
+
animated = true,
|
|
61
|
+
loop = true,
|
|
62
|
+
shine = true,
|
|
63
|
+
style,
|
|
64
|
+
containerStyle,
|
|
65
|
+
}: ThreeDTextProps) => {
|
|
66
|
+
const progress = useSharedValue(0);
|
|
67
|
+
|
|
68
|
+
const fontSize = Number(style?.fontSize ?? 36);
|
|
69
|
+
const frontColor = color ?? String(style?.color ?? '#A855F7');
|
|
70
|
+
|
|
71
|
+
const fontWeight = (style?.fontWeight ?? 'normal') as
|
|
72
|
+
| 'normal'
|
|
73
|
+
| 'bold'
|
|
74
|
+
| '100'
|
|
75
|
+
| '200'
|
|
76
|
+
| '300'
|
|
77
|
+
| '400'
|
|
78
|
+
| '500'
|
|
79
|
+
| '600'
|
|
80
|
+
| '700'
|
|
81
|
+
| '800'
|
|
82
|
+
| '900';
|
|
83
|
+
|
|
84
|
+
const font = useMemo(
|
|
85
|
+
() =>
|
|
86
|
+
matchFont({
|
|
87
|
+
fontFamily: style?.fontFamily ?? 'Arial',
|
|
88
|
+
fontSize,
|
|
89
|
+
fontWeight,
|
|
90
|
+
fontStyle: style?.fontStyle ?? 'normal',
|
|
91
|
+
}),
|
|
92
|
+
[
|
|
93
|
+
style?.fontFamily,
|
|
94
|
+
style?.fontStyle,
|
|
95
|
+
fontSize,
|
|
96
|
+
fontWeight,
|
|
97
|
+
],
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
const metrics = font.getMetrics();
|
|
101
|
+
const textWidth = font.measureText(text).width;
|
|
102
|
+
|
|
103
|
+
const safeDepth = Math.max(0, Math.min(24, Math.floor(depth)));
|
|
104
|
+
const safeOffset = Math.max(0, depthOffset);
|
|
105
|
+
const safeBevel = Math.max(0, bevelSize);
|
|
106
|
+
|
|
107
|
+
const layers = useMemo(
|
|
108
|
+
() =>
|
|
109
|
+
Array.from(
|
|
110
|
+
{ length: safeDepth },
|
|
111
|
+
(_, index) => safeDepth - index,
|
|
112
|
+
),
|
|
113
|
+
[safeDepth],
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
const totalDepth = safeDepth * safeOffset;
|
|
117
|
+
const padding = Math.ceil(fontSize * 0.3 + safeBevel * 2);
|
|
118
|
+
|
|
119
|
+
const canvasWidth = Math.ceil(
|
|
120
|
+
textWidth + padding * 2 + totalDepth + safeBevel,
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
const canvasHeight = Math.ceil(
|
|
124
|
+
metrics.descent -
|
|
125
|
+
metrics.ascent +
|
|
126
|
+
padding * 2 +
|
|
127
|
+
totalDepth +
|
|
128
|
+
safeBevel,
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
const baseline = -metrics.ascent + padding;
|
|
132
|
+
|
|
133
|
+
useEffect(() => {
|
|
134
|
+
cancelAnimation(progress);
|
|
135
|
+
progress.value = 0;
|
|
136
|
+
|
|
137
|
+
if (animated || shine) {
|
|
138
|
+
progress.value = withRepeat(
|
|
139
|
+
withTiming(1, {
|
|
140
|
+
duration,
|
|
141
|
+
easing: Easing.linear,
|
|
142
|
+
}),
|
|
143
|
+
loop ? -1 : 1,
|
|
144
|
+
false,
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return () => {
|
|
149
|
+
cancelAnimation(progress);
|
|
150
|
+
};
|
|
151
|
+
}, [animated, shine, duration, loop, progress]);
|
|
152
|
+
|
|
153
|
+
const animatedStyle = useAnimatedStyle(() => {
|
|
154
|
+
const rotationXValue = animated
|
|
155
|
+
? Math.sin(progress.value * Math.PI * 2) * rotationX
|
|
156
|
+
: rotationX;
|
|
157
|
+
|
|
158
|
+
const rotationYValue = animated
|
|
159
|
+
? Math.cos(progress.value * Math.PI * 2) * rotationY
|
|
160
|
+
: rotationY;
|
|
161
|
+
|
|
162
|
+
return {
|
|
163
|
+
transform: [
|
|
164
|
+
{ perspective },
|
|
165
|
+
{ rotateX: `${rotationXValue}deg` },
|
|
166
|
+
{ rotateY: `${rotationYValue}deg` },
|
|
167
|
+
],
|
|
168
|
+
};
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
const shineTransform = useDerivedValue(() => [
|
|
172
|
+
{
|
|
173
|
+
translateX: -textWidth + progress.value * textWidth * 2,
|
|
174
|
+
},
|
|
175
|
+
]);
|
|
176
|
+
|
|
177
|
+
return (
|
|
178
|
+
<View style={containerStyle}>
|
|
179
|
+
<Animated.View
|
|
180
|
+
style={[
|
|
181
|
+
{
|
|
182
|
+
width: canvasWidth,
|
|
183
|
+
height: canvasHeight,
|
|
184
|
+
},
|
|
185
|
+
animatedStyle,
|
|
186
|
+
]}
|
|
187
|
+
>
|
|
188
|
+
<Canvas
|
|
189
|
+
style={{
|
|
190
|
+
width: canvasWidth,
|
|
191
|
+
height: canvasHeight,
|
|
192
|
+
}}
|
|
193
|
+
>
|
|
194
|
+
{/* Drop shadow */}
|
|
195
|
+
<SkiaText
|
|
196
|
+
text={text}
|
|
197
|
+
x={padding + totalDepth * 0.8}
|
|
198
|
+
y={baseline + totalDepth * 0.8}
|
|
199
|
+
font={font}
|
|
200
|
+
color={shadowColor}
|
|
201
|
+
/>
|
|
202
|
+
|
|
203
|
+
{/* Extruded side layers */}
|
|
204
|
+
{layers.map((layer) => (
|
|
205
|
+
<SkiaText
|
|
206
|
+
key={layer}
|
|
207
|
+
text={text}
|
|
208
|
+
x={padding + layer * safeOffset}
|
|
209
|
+
y={baseline + layer * safeOffset}
|
|
210
|
+
font={font}
|
|
211
|
+
color={depthColor}
|
|
212
|
+
/>
|
|
213
|
+
))}
|
|
214
|
+
|
|
215
|
+
{/* Upper-left bevel highlight */}
|
|
216
|
+
{safeBevel > 0 && (
|
|
217
|
+
<SkiaText
|
|
218
|
+
text={text}
|
|
219
|
+
x={padding - safeBevel * 0.5}
|
|
220
|
+
y={baseline - safeBevel * 0.5}
|
|
221
|
+
font={font}
|
|
222
|
+
color={bevelColor}
|
|
223
|
+
/>
|
|
224
|
+
)}
|
|
225
|
+
|
|
226
|
+
{/* Main front face */}
|
|
227
|
+
<SkiaText
|
|
228
|
+
text={text}
|
|
229
|
+
x={padding}
|
|
230
|
+
y={baseline}
|
|
231
|
+
font={font}
|
|
232
|
+
color={frontColor}
|
|
233
|
+
>
|
|
234
|
+
{gradientColors.length >= 2 && (
|
|
235
|
+
<LinearGradient
|
|
236
|
+
start={vec(padding, 0)}
|
|
237
|
+
end={vec(padding + textWidth, 0)}
|
|
238
|
+
colors={gradientColors}
|
|
239
|
+
/>
|
|
240
|
+
)}
|
|
241
|
+
</SkiaText>
|
|
242
|
+
|
|
243
|
+
{/* Moving specular shine */}
|
|
244
|
+
{shine && (
|
|
245
|
+
<SkiaText
|
|
246
|
+
text={text}
|
|
247
|
+
x={padding}
|
|
248
|
+
y={baseline}
|
|
249
|
+
font={font}
|
|
250
|
+
>
|
|
251
|
+
<LinearGradient
|
|
252
|
+
start={vec(0, 0)}
|
|
253
|
+
end={vec(textWidth, 0)}
|
|
254
|
+
colors={[
|
|
255
|
+
'rgba(255,255,255,0)',
|
|
256
|
+
'rgba(255,255,255,0.75)',
|
|
257
|
+
'rgba(255,255,255,0)',
|
|
258
|
+
]}
|
|
259
|
+
positions={[0, 0.5, 1]}
|
|
260
|
+
transform={shineTransform}
|
|
261
|
+
/>
|
|
262
|
+
</SkiaText>
|
|
263
|
+
)}
|
|
264
|
+
</Canvas>
|
|
265
|
+
</Animated.View>
|
|
266
|
+
</View>
|
|
267
|
+
);
|
|
268
|
+
};
|