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.
Files changed (152) hide show
  1. package/LICENSE +17 -0
  2. package/README.md +564 -2
  3. package/docs/android-demo.gif +0 -0
  4. package/lib/commonjs/index.js +146 -0
  5. package/lib/commonjs/index.js.map +1 -0
  6. package/lib/commonjs/package.json +1 -0
  7. package/lib/commonjs/text/BlurText.js +146 -0
  8. package/lib/commonjs/text/BlurText.js.map +1 -0
  9. package/lib/commonjs/text/BounceText.js +83 -0
  10. package/lib/commonjs/text/BounceText.js.map +1 -0
  11. package/lib/commonjs/text/CountUpText.js +77 -0
  12. package/lib/commonjs/text/CountUpText.js.map +1 -0
  13. package/lib/commonjs/text/ElasticText.js +85 -0
  14. package/lib/commonjs/text/ElasticText.js.map +1 -0
  15. package/lib/commonjs/text/ExplosionText.js +191 -0
  16. package/lib/commonjs/text/ExplosionText.js.map +1 -0
  17. package/lib/commonjs/text/FlipText.js +101 -0
  18. package/lib/commonjs/text/FlipText.js.map +1 -0
  19. package/lib/commonjs/text/GlitchText.js +329 -0
  20. package/lib/commonjs/text/GlitchText.js.map +1 -0
  21. package/lib/commonjs/text/GradientText.js +73 -0
  22. package/lib/commonjs/text/GradientText.js.map +1 -0
  23. package/lib/commonjs/text/MorphText.js +192 -0
  24. package/lib/commonjs/text/MorphText.js.map +1 -0
  25. package/lib/commonjs/text/NeonText.js +134 -0
  26. package/lib/commonjs/text/NeonText.js.map +1 -0
  27. package/lib/commonjs/text/ParticleText.js +328 -0
  28. package/lib/commonjs/text/ParticleText.js.map +1 -0
  29. package/lib/commonjs/text/RevealText.js +142 -0
  30. package/lib/commonjs/text/RevealText.js.map +1 -0
  31. package/lib/commonjs/text/RotateText.js +99 -0
  32. package/lib/commonjs/text/RotateText.js.map +1 -0
  33. package/lib/commonjs/text/ScrambleText.js +82 -0
  34. package/lib/commonjs/text/ScrambleText.js.map +1 -0
  35. package/lib/commonjs/text/ShinyText.js +87 -0
  36. package/lib/commonjs/text/ShinyText.js.map +1 -0
  37. package/lib/commonjs/text/SplitText.js +81 -0
  38. package/lib/commonjs/text/SplitText.js.map +1 -0
  39. package/lib/commonjs/text/TextWave.js +130 -0
  40. package/lib/commonjs/text/TextWave.js.map +1 -0
  41. package/lib/commonjs/text/ThreeDText.js +145 -0
  42. package/lib/commonjs/text/ThreeDText.js.map +1 -0
  43. package/lib/commonjs/text/TypewriterText.js +119 -0
  44. package/lib/commonjs/text/TypewriterText.js.map +1 -0
  45. package/lib/commonjs/text/WaveText.js +79 -0
  46. package/lib/commonjs/text/WaveText.js.map +1 -0
  47. package/lib/module/index.js +23 -0
  48. package/lib/module/index.js.map +1 -0
  49. package/lib/module/text/BlurText.js +139 -0
  50. package/lib/module/text/BlurText.js.map +1 -0
  51. package/lib/module/text/BounceText.js +77 -0
  52. package/lib/module/text/BounceText.js.map +1 -0
  53. package/lib/module/text/CountUpText.js +70 -0
  54. package/lib/module/text/CountUpText.js.map +1 -0
  55. package/lib/module/text/ElasticText.js +78 -0
  56. package/lib/module/text/ElasticText.js.map +1 -0
  57. package/lib/module/text/ExplosionText.js +184 -0
  58. package/lib/module/text/ExplosionText.js.map +1 -0
  59. package/lib/module/text/FlipText.js +94 -0
  60. package/lib/module/text/FlipText.js.map +1 -0
  61. package/lib/module/text/GlitchText.js +322 -0
  62. package/lib/module/text/GlitchText.js.map +1 -0
  63. package/lib/module/text/GradientText.js +66 -0
  64. package/lib/module/text/GradientText.js.map +1 -0
  65. package/lib/module/text/MorphText.js +185 -0
  66. package/lib/module/text/MorphText.js.map +1 -0
  67. package/lib/module/text/NeonText.js +127 -0
  68. package/lib/module/text/NeonText.js.map +1 -0
  69. package/lib/module/text/ParticleText.js +321 -0
  70. package/lib/module/text/ParticleText.js.map +1 -0
  71. package/lib/module/text/RevealText.js +135 -0
  72. package/lib/module/text/RevealText.js.map +1 -0
  73. package/lib/module/text/RotateText.js +93 -0
  74. package/lib/module/text/RotateText.js.map +1 -0
  75. package/lib/module/text/ScrambleText.js +76 -0
  76. package/lib/module/text/ScrambleText.js.map +1 -0
  77. package/lib/module/text/ShinyText.js +80 -0
  78. package/lib/module/text/ShinyText.js.map +1 -0
  79. package/lib/module/text/SplitText.js +74 -0
  80. package/lib/module/text/SplitText.js.map +1 -0
  81. package/lib/module/text/TextWave.js +123 -0
  82. package/lib/module/text/TextWave.js.map +1 -0
  83. package/lib/module/text/ThreeDText.js +138 -0
  84. package/lib/module/text/ThreeDText.js.map +1 -0
  85. package/lib/module/text/TypewriterText.js +113 -0
  86. package/lib/module/text/TypewriterText.js.map +1 -0
  87. package/lib/module/text/WaveText.js +72 -0
  88. package/lib/module/text/WaveText.js.map +1 -0
  89. package/lib/typescript/index.d.ts +41 -0
  90. package/lib/typescript/index.d.ts.map +1 -0
  91. package/lib/typescript/text/BlurText.d.ts +16 -0
  92. package/lib/typescript/text/BlurText.d.ts.map +1 -0
  93. package/lib/typescript/text/BounceText.d.ts +15 -0
  94. package/lib/typescript/text/BounceText.d.ts.map +1 -0
  95. package/lib/typescript/text/CountUpText.d.ts +20 -0
  96. package/lib/typescript/text/CountUpText.d.ts.map +1 -0
  97. package/lib/typescript/text/ElasticText.d.ts +17 -0
  98. package/lib/typescript/text/ElasticText.d.ts.map +1 -0
  99. package/lib/typescript/text/ExplosionText.d.ts +23 -0
  100. package/lib/typescript/text/ExplosionText.d.ts.map +1 -0
  101. package/lib/typescript/text/FlipText.d.ts +17 -0
  102. package/lib/typescript/text/FlipText.d.ts.map +1 -0
  103. package/lib/typescript/text/GlitchText.d.ts +19 -0
  104. package/lib/typescript/text/GlitchText.d.ts.map +1 -0
  105. package/lib/typescript/text/GradientText.d.ts +12 -0
  106. package/lib/typescript/text/GradientText.d.ts.map +1 -0
  107. package/lib/typescript/text/MorphText.d.ts +18 -0
  108. package/lib/typescript/text/MorphText.d.ts.map +1 -0
  109. package/lib/typescript/text/NeonText.d.ts +16 -0
  110. package/lib/typescript/text/NeonText.d.ts.map +1 -0
  111. package/lib/typescript/text/ParticleText.d.ts +23 -0
  112. package/lib/typescript/text/ParticleText.d.ts.map +1 -0
  113. package/lib/typescript/text/RevealText.d.ts +19 -0
  114. package/lib/typescript/text/RevealText.d.ts.map +1 -0
  115. package/lib/typescript/text/RotateText.d.ts +17 -0
  116. package/lib/typescript/text/RotateText.d.ts.map +1 -0
  117. package/lib/typescript/text/ScrambleText.d.ts +14 -0
  118. package/lib/typescript/text/ScrambleText.d.ts.map +1 -0
  119. package/lib/typescript/text/ShinyText.d.ts +15 -0
  120. package/lib/typescript/text/ShinyText.d.ts.map +1 -0
  121. package/lib/typescript/text/SplitText.d.ts +28 -0
  122. package/lib/typescript/text/SplitText.d.ts.map +1 -0
  123. package/lib/typescript/text/TextWave.d.ts +15 -0
  124. package/lib/typescript/text/TextWave.d.ts.map +1 -0
  125. package/lib/typescript/text/ThreeDText.d.ts +24 -0
  126. package/lib/typescript/text/ThreeDText.d.ts.map +1 -0
  127. package/lib/typescript/text/TypewriterText.d.ts +21 -0
  128. package/lib/typescript/text/TypewriterText.d.ts.map +1 -0
  129. package/lib/typescript/text/WaveText.d.ts +14 -0
  130. package/lib/typescript/text/WaveText.d.ts.map +1 -0
  131. package/package.json +48 -4
  132. package/src/index.ts +45 -0
  133. package/src/text/BlurText.tsx +243 -0
  134. package/src/text/BounceText.tsx +158 -0
  135. package/src/text/CountUpText.tsx +153 -0
  136. package/src/text/ElasticText.tsx +143 -0
  137. package/src/text/ExplosionText.tsx +242 -0
  138. package/src/text/FlipText.tsx +161 -0
  139. package/src/text/GlitchText.tsx +525 -0
  140. package/src/text/GradientText.tsx +118 -0
  141. package/src/text/MorphText.tsx +305 -0
  142. package/src/text/NeonText.tsx +205 -0
  143. package/src/text/ParticleText.tsx +489 -0
  144. package/src/text/RevealText.tsx +255 -0
  145. package/src/text/RotateText.tsx +179 -0
  146. package/src/text/ScrambleText.tsx +137 -0
  147. package/src/text/ShinyText.tsx +163 -0
  148. package/src/text/SplitText.tsx +122 -0
  149. package/src/text/TextWave.tsx +223 -0
  150. package/src/text/ThreeDText.tsx +268 -0
  151. package/src/text/TypewriterText.tsx +189 -0
  152. package/src/text/WaveText.tsx +132 -0
@@ -0,0 +1,525 @@
1
+
2
+ import React, { useEffect, useState } from 'react';
3
+ import {
4
+ View,
5
+ type LayoutChangeEvent,
6
+ type TextStyle,
7
+ type ViewStyle,
8
+ } from 'react-native';
9
+
10
+ import Animated, {
11
+ cancelAnimation,
12
+ Easing,
13
+ useAnimatedStyle,
14
+ useSharedValue,
15
+ withDelay,
16
+ withRepeat,
17
+ withSequence,
18
+ withTiming,
19
+ } from 'react-native-reanimated';
20
+
21
+ export interface GlitchTextProps {
22
+ text: string;
23
+ intensity?: number;
24
+ duration?: number;
25
+ delay?: number;
26
+ loop?: boolean;
27
+ rgbSplit?: boolean;
28
+ noise?: boolean;
29
+ scanlines?: boolean;
30
+ flicker?: boolean;
31
+ slices?: number;
32
+ style?: TextStyle;
33
+ containerStyle?: ViewStyle;
34
+ }
35
+
36
+ interface GlitchSliceProps {
37
+ text: string;
38
+ top: number;
39
+ height: number;
40
+ width: number;
41
+ index: number;
42
+ intensity: number;
43
+ duration: number;
44
+ delay: number;
45
+ loop: boolean;
46
+ rgbSplit: boolean;
47
+ flicker: boolean;
48
+ style?: TextStyle;
49
+ }
50
+
51
+ function GlitchSlice({
52
+ text,
53
+ top,
54
+ height,
55
+ width,
56
+ index,
57
+ intensity,
58
+ duration,
59
+ delay,
60
+ loop,
61
+ rgbSplit,
62
+ flicker,
63
+ style,
64
+ }: GlitchSliceProps) {
65
+ const glitch = useSharedValue(0);
66
+
67
+ useEffect(() => {
68
+ const burst = withSequence(
69
+ withTiming(1, {
70
+ duration: duration * 0.15,
71
+ easing: Easing.linear,
72
+ }),
73
+ withTiming(-0.85, {
74
+ duration: duration * 0.12,
75
+ easing: Easing.linear,
76
+ }),
77
+ withTiming(0.65, {
78
+ duration: duration * 0.12,
79
+ easing: Easing.linear,
80
+ }),
81
+ withTiming(-0.3, {
82
+ duration: duration * 0.1,
83
+ easing: Easing.linear,
84
+ }),
85
+ withTiming(0, {
86
+ duration: duration * 0.25,
87
+ easing: Easing.out(Easing.quad),
88
+ }),
89
+ );
90
+
91
+ const animation = loop
92
+ ? withRepeat(
93
+ withSequence(
94
+ burst,
95
+ withTiming(0, { duration: 700 }),
96
+ ),
97
+ -1,
98
+ false,
99
+ )
100
+ : burst;
101
+
102
+ glitch.value = withDelay(
103
+ delay + index * 45,
104
+ animation,
105
+ );
106
+
107
+ return () => cancelAnimation(glitch);
108
+ }, [duration, delay, index, loop, glitch]);
109
+
110
+ // Main horizontal slice
111
+ const mainStyle = useAnimatedStyle(() => {
112
+ const value = glitch.value;
113
+
114
+ return {
115
+ opacity: Math.abs(value) * intensity,
116
+ transform: [
117
+ {
118
+ translateX: value * intensity * 22,
119
+ },
120
+ ],
121
+ };
122
+ });
123
+
124
+ // Red RGB channel
125
+ const redStyle = useAnimatedStyle(() => {
126
+ const value = glitch.value;
127
+
128
+ return {
129
+ opacity: Math.abs(value) * intensity,
130
+ transform: [
131
+ {
132
+ translateX: value * intensity * -32,
133
+ },
134
+ ],
135
+ };
136
+ });
137
+
138
+ // Cyan RGB channel
139
+ const cyanStyle = useAnimatedStyle(() => {
140
+ const value = glitch.value;
141
+
142
+ return {
143
+ opacity: Math.abs(value) * intensity,
144
+ transform: [
145
+ {
146
+ translateX: value * intensity * 32,
147
+ },
148
+ ],
149
+ };
150
+ });
151
+
152
+ const displayStyle = [
153
+ style,
154
+ {
155
+ position: 'absolute' as const,
156
+ left: 0,
157
+ top: -top,
158
+ width,
159
+ },
160
+ ];
161
+
162
+ return (
163
+ <View
164
+ pointerEvents="none"
165
+ style={{
166
+ position: 'absolute',
167
+ top,
168
+ left: 0,
169
+ width,
170
+ height,
171
+ overflow: 'hidden',
172
+ }}
173
+ >
174
+ {rgbSplit && (
175
+ <>
176
+ <Animated.Text
177
+ style={[
178
+ displayStyle,
179
+ { color: '#FF1744' },
180
+ redStyle,
181
+ ]}
182
+ >
183
+ {text}
184
+ </Animated.Text>
185
+
186
+ <Animated.Text
187
+ style={[
188
+ displayStyle,
189
+ { color: '#00E5FF' },
190
+ cyanStyle,
191
+ ]}
192
+ >
193
+ {text}
194
+ </Animated.Text>
195
+ </>
196
+ )}
197
+
198
+ <Animated.Text
199
+ style={[
200
+ displayStyle,
201
+ mainStyle,
202
+ ]}
203
+ >
204
+ {text}
205
+ </Animated.Text>
206
+ </View>
207
+ );
208
+ }
209
+
210
+ // --------------------------------------------------
211
+ // Digital noise blocks
212
+ // --------------------------------------------------
213
+
214
+ interface NoiseBarProps {
215
+ width: number;
216
+ top: number;
217
+ height: number;
218
+ index: number;
219
+ delay: number;
220
+ loop: boolean;
221
+ intensity: number;
222
+ }
223
+
224
+ function NoiseBar({
225
+ width,
226
+ top,
227
+ height,
228
+ index,
229
+ delay,
230
+ loop,
231
+ intensity,
232
+ }: NoiseBarProps) {
233
+ const progress = useSharedValue(0);
234
+
235
+ useEffect(() => {
236
+ const burst = withSequence(
237
+ withTiming(1, { duration: 45 }),
238
+ withTiming(0.2, { duration: 65 }),
239
+ withTiming(0, { duration: 80 }),
240
+ );
241
+
242
+ progress.value = withDelay(
243
+ delay + index * 90,
244
+ loop
245
+ ? withRepeat(
246
+ withSequence(
247
+ burst,
248
+ withTiming(0, { duration: 400 }),
249
+ ),
250
+ -1,
251
+ false,
252
+ )
253
+ : burst,
254
+ );
255
+
256
+ return () => cancelAnimation(progress);
257
+ }, [delay, index, loop, progress]);
258
+
259
+ const animatedStyle = useAnimatedStyle(() => ({
260
+ opacity: progress.value * intensity * 0.9,
261
+ transform: [
262
+ {
263
+ translateX:
264
+ progress.value * (index % 2 === 0 ? 25 : -25),
265
+ },
266
+ ],
267
+ }));
268
+
269
+ return (
270
+ <Animated.View
271
+ pointerEvents="none"
272
+ style={[
273
+ {
274
+ position: 'absolute',
275
+ left: index % 2 === 0 ? 0 : width * 0.35,
276
+ top,
277
+ width: width * (index % 2 === 0 ? 0.65 : 0.5),
278
+ height,
279
+ backgroundColor:
280
+ index % 2 === 0 ? '#FF1744' : '#00E5FF',
281
+ },
282
+ animatedStyle,
283
+ ]}
284
+ />
285
+ );
286
+ }
287
+
288
+ // --------------------------------------------------
289
+ // Main GlitchText
290
+ // --------------------------------------------------
291
+
292
+ export function GlitchText({
293
+ text,
294
+ intensity = 0.9,
295
+ duration = 600,
296
+ delay = 0,
297
+ loop = true,
298
+ rgbSplit = true,
299
+ noise = true,
300
+ scanlines = false,
301
+ flicker = true,
302
+ slices = 6,
303
+ style,
304
+ containerStyle,
305
+ }: GlitchTextProps) {
306
+ const [layout, setLayout] = useState({
307
+ width: 0,
308
+ height: 0,
309
+ });
310
+
311
+ const safeIntensity = Math.max(
312
+ 0,
313
+ Math.min(1, intensity),
314
+ );
315
+
316
+ const safeSlices = Math.max(
317
+ 1,
318
+ Math.min(12, Math.floor(slices)),
319
+ );
320
+
321
+ const safeDuration = Math.max(100, duration);
322
+
323
+ const flickerValue = useSharedValue(1);
324
+ const signalValue = useSharedValue(0);
325
+
326
+ // Measure actual text dimensions
327
+ const handleLayout = (event: LayoutChangeEvent) => {
328
+ const { width, height } = event.nativeEvent.layout;
329
+
330
+ setLayout(previous => {
331
+ if (
332
+ previous.width === width &&
333
+ previous.height === height
334
+ ) {
335
+ return previous;
336
+ }
337
+
338
+ return { width, height };
339
+ });
340
+ };
341
+
342
+ // Main flicker animation
343
+ useEffect(() => {
344
+ cancelAnimation(flickerValue);
345
+ flickerValue.value = 1;
346
+
347
+ if (!flicker) {
348
+ return;
349
+ }
350
+
351
+ const burst = withSequence(
352
+ withTiming(0.5, { duration: 45 }),
353
+ withTiming(1, { duration: 60 }),
354
+ withTiming(0.7, { duration: 35 }),
355
+ withTiming(1, { duration: 80 }),
356
+ );
357
+
358
+ flickerValue.value = withDelay(
359
+ delay,
360
+ loop
361
+ ? withRepeat(
362
+ withSequence(
363
+ burst,
364
+ withTiming(1, { duration: 500 }),
365
+ ),
366
+ -1,
367
+ false,
368
+ )
369
+ : burst,
370
+ );
371
+
372
+ return () => cancelAnimation(flickerValue);
373
+ }, [delay, flicker, loop, flickerValue]);
374
+
375
+ // Scanline visibility follows the glitch burst
376
+ useEffect(() => {
377
+ cancelAnimation(signalValue);
378
+ signalValue.value = 0;
379
+
380
+ const burst = withSequence(
381
+ withTiming(1, { duration: 100 }),
382
+ withTiming(0, {
383
+ duration: safeDuration + 500,
384
+ easing: Easing.out(Easing.quad),
385
+ }),
386
+ );
387
+
388
+ signalValue.value = withDelay(
389
+ delay,
390
+ loop
391
+ ? withRepeat(
392
+ withSequence(
393
+ burst,
394
+ withTiming(0, { duration: 500 }),
395
+ ),
396
+ -1,
397
+ false,
398
+ )
399
+ : burst,
400
+ );
401
+
402
+ return () => cancelAnimation(signalValue);
403
+ }, [delay, loop, safeDuration, signalValue]);
404
+
405
+ const baseStyle = useAnimatedStyle(() => ({
406
+ opacity: flickerValue.value,
407
+ }));
408
+
409
+ const scanlineStyle = useAnimatedStyle(() => ({
410
+ opacity: signalValue.value * 0.4,
411
+ }));
412
+
413
+ const sliceHeight = layout.height / safeSlices;
414
+
415
+ return (
416
+ <View
417
+ style={[
418
+ {
419
+ alignSelf: 'flex-start',
420
+ position: 'relative',
421
+ overflow: 'visible',
422
+ },
423
+ containerStyle,
424
+ ]}
425
+ >
426
+ {/* Original clean text */}
427
+ <Animated.Text
428
+ onLayout={handleLayout}
429
+ style={[
430
+ style,
431
+ {
432
+ includeFontPadding: false,
433
+ },
434
+ baseStyle,
435
+ ]}
436
+ >
437
+ {text}
438
+ </Animated.Text>
439
+
440
+ {/* Horizontal signal slices */}
441
+ {layout.width > 0 &&
442
+ layout.height > 0 &&
443
+ Array.from({ length: safeSlices }).map((_, index) => {
444
+ const top = index * sliceHeight;
445
+
446
+ return (
447
+ <GlitchSlice
448
+ key={`${text}-${index}`}
449
+ text={text}
450
+ top={top}
451
+ height={sliceHeight + 1}
452
+ width={layout.width}
453
+ index={index}
454
+ intensity={safeIntensity}
455
+ duration={safeDuration}
456
+ delay={delay}
457
+ loop={loop}
458
+ rgbSplit={rgbSplit}
459
+ flicker={flicker}
460
+ style={style}
461
+ />
462
+ );
463
+ })}
464
+
465
+ {/* Digital noise */}
466
+ {noise && layout.width > 0 && layout.height > 0 && (
467
+ <View
468
+ pointerEvents="none"
469
+ style={{
470
+ position: 'absolute',
471
+ top: 0,
472
+ left: 0,
473
+ width: layout.width,
474
+ height: layout.height,
475
+ overflow: 'hidden',
476
+ }}
477
+ >
478
+ {Array.from({ length: 5 }).map((_, index) => (
479
+ <NoiseBar
480
+ key={index}
481
+ width={layout.width}
482
+ height={Math.max(2, layout.height * 0.045)}
483
+ top={layout.height * (0.15 + index * 0.16)}
484
+ index={index}
485
+ delay={delay}
486
+ loop={loop}
487
+ intensity={safeIntensity}
488
+ />
489
+ ))}
490
+ </View>
491
+ )}
492
+
493
+ {/* Animated scanlines */}
494
+ {scanlines && layout.width > 0 && layout.height > 0 && (
495
+ <Animated.View
496
+ pointerEvents="none"
497
+ style={[
498
+ {
499
+ position: 'absolute',
500
+ top: 0,
501
+ left: 0,
502
+ width: layout.width,
503
+ height: layout.height,
504
+ justifyContent: 'space-evenly',
505
+ },
506
+ scanlineStyle,
507
+ ]}
508
+ >
509
+ {Array.from({ length: 8 }).map((_, index) => (
510
+ <View
511
+ key={index}
512
+ style={{
513
+ width: '100%',
514
+ height: 1,
515
+ backgroundColor: 'rgba(0,0,0,0.5)',
516
+ }}
517
+ />
518
+ ))}
519
+ </Animated.View>
520
+ )}
521
+ </View>
522
+ );
523
+ }
524
+
525
+ export default GlitchText;
@@ -0,0 +1,118 @@
1
+
2
+ import React, { useEffect, useMemo } from 'react';
3
+ import {
4
+ View,
5
+ type TextStyle,
6
+ type ViewStyle,
7
+ } from 'react-native';
8
+
9
+ import {
10
+ Canvas,
11
+ LinearGradient,
12
+ matchFont,
13
+ Text as SkiaText,
14
+ vec,
15
+ } from '@shopify/react-native-skia';
16
+
17
+ import {
18
+ cancelAnimation,
19
+ Easing,
20
+ useDerivedValue,
21
+ useSharedValue,
22
+ withRepeat,
23
+ withTiming,
24
+ } from 'react-native-reanimated';
25
+
26
+ export interface GradientTextProps {
27
+ text: string;
28
+ colors?: string[];
29
+ duration?: number;
30
+ loop?: boolean;
31
+ style?: TextStyle;
32
+ containerStyle?: ViewStyle;
33
+ }
34
+
35
+ export const GradientText = ({
36
+ text,
37
+ colors = ['#8B5CF6', '#EC4899', '#F97316'],
38
+ duration = 2500,
39
+ loop = true,
40
+ style,
41
+ containerStyle,
42
+ }: GradientTextProps) => {
43
+ const fontSize = Number(style?.fontSize ?? 32);
44
+ const fontFamily = style?.fontFamily ?? 'Arial';
45
+ const fontWeight =
46
+ style?.fontWeight === 'bold' ? 'bold' : 'normal';
47
+ const fontStyle =
48
+ style?.fontStyle === 'italic' ? 'italic' : 'normal';
49
+
50
+ const font = useMemo(
51
+ () =>
52
+ matchFont({
53
+ fontFamily,
54
+ fontSize,
55
+ fontWeight,
56
+ fontStyle,
57
+ }),
58
+ [fontFamily, fontSize, fontWeight, fontStyle],
59
+ );
60
+
61
+ const textWidth = Math.max(1, font.measureText(text).width);
62
+ const metrics = font.getMetrics();
63
+
64
+ const padding = Math.ceil(fontSize * 0.2);
65
+
66
+ const canvasHeight =
67
+ metrics.descent - metrics.ascent + padding * 2;
68
+
69
+ const baseline = -metrics.ascent + padding;
70
+
71
+ const progress = useSharedValue(0);
72
+
73
+ useEffect(() => {
74
+ progress.value = withRepeat(
75
+ withTiming(1, {
76
+ duration,
77
+ easing: Easing.linear,
78
+ }),
79
+ loop ? -1 : 1,
80
+ false,
81
+ );
82
+
83
+ return () => cancelAnimation(progress);
84
+ }, [duration, loop, progress]);
85
+
86
+ const gradientTransform = useDerivedValue(() => {
87
+ return [
88
+ {
89
+ translateX: -progress.value * textWidth,
90
+ },
91
+ ];
92
+ });
93
+
94
+ return (
95
+ <View style={containerStyle}>
96
+ <Canvas
97
+ style={{
98
+ width: textWidth + padding * 2,
99
+ height: canvasHeight,
100
+ }}
101
+ >
102
+ <SkiaText
103
+ x={padding}
104
+ y={baseline}
105
+ text={text}
106
+ font={font}
107
+ >
108
+ <LinearGradient
109
+ start={vec(-textWidth, 0)}
110
+ end={vec(textWidth * 2, 0)}
111
+ colors={colors}
112
+ transform={gradientTransform}
113
+ />
114
+ </SkiaText>
115
+ </Canvas>
116
+ </View>
117
+ );
118
+ };