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,489 @@
|
|
|
1
|
+
|
|
2
|
+
import React, { useEffect, useMemo, useState } from 'react';
|
|
3
|
+
import { PixelRatio, type TextStyle, type ViewStyle } from 'react-native';
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
AlphaType,
|
|
7
|
+
Atlas,
|
|
8
|
+
BlurStyle,
|
|
9
|
+
Canvas,
|
|
10
|
+
ColorType,
|
|
11
|
+
FilterMode,
|
|
12
|
+
Skia,
|
|
13
|
+
useCanvasRef,
|
|
14
|
+
useRSXformBuffer,
|
|
15
|
+
type SkPaint,
|
|
16
|
+
} from '@shopify/react-native-skia';
|
|
17
|
+
|
|
18
|
+
import {
|
|
19
|
+
cancelAnimation,
|
|
20
|
+
Easing,
|
|
21
|
+
useSharedValue,
|
|
22
|
+
withDelay,
|
|
23
|
+
withRepeat,
|
|
24
|
+
withSequence,
|
|
25
|
+
withTiming,
|
|
26
|
+
} from 'react-native-reanimated';
|
|
27
|
+
|
|
28
|
+
export interface ParticleTextProps {
|
|
29
|
+
text: string;
|
|
30
|
+
fontSize?: number;
|
|
31
|
+
particleSize?: number;
|
|
32
|
+
duration?: number;
|
|
33
|
+
/** Milliseconds to hold the starting pose before moving. Defaults to 750 for disperse and 0 for assemble. */
|
|
34
|
+
delay?: number;
|
|
35
|
+
stagger?: number;
|
|
36
|
+
loop?: boolean;
|
|
37
|
+
direction?: 'assemble' | 'disperse';
|
|
38
|
+
particleColor?: string;
|
|
39
|
+
|
|
40
|
+
particleShape?: 'circle' | 'square' | 'diamond' | 'mixed';
|
|
41
|
+
|
|
42
|
+
particleGlow?: boolean;
|
|
43
|
+
glowRadius?: number;
|
|
44
|
+
glowColor?: string;
|
|
45
|
+
|
|
46
|
+
style?: TextStyle;
|
|
47
|
+
containerStyle?: ViewStyle;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface ParticleData {
|
|
51
|
+
x: number;
|
|
52
|
+
y: number;
|
|
53
|
+
startX: number;
|
|
54
|
+
startY: number;
|
|
55
|
+
delay: number;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
interface ParticleLayout {
|
|
59
|
+
particles: ParticleData[];
|
|
60
|
+
width: number;
|
|
61
|
+
height: number;
|
|
62
|
+
totalDuration: number;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const PADDING = 16;
|
|
66
|
+
const MASK_SCALE = 2;
|
|
67
|
+
const MIXED_SHAPES = ['circle', 'square', 'diamond'] as const;
|
|
68
|
+
const ATLAS_SAMPLING = { filter: FilterMode.Linear };
|
|
69
|
+
|
|
70
|
+
const createParticleAtlas = (
|
|
71
|
+
shape: NonNullable<ParticleTextProps['particleShape']>,
|
|
72
|
+
size: number,
|
|
73
|
+
color: string,
|
|
74
|
+
glowRadius: number,
|
|
75
|
+
glowColor: string,
|
|
76
|
+
) => {
|
|
77
|
+
const shapes = shape === 'mixed' ? MIXED_SHAPES : [shape];
|
|
78
|
+
const pixelRatio = PixelRatio.get();
|
|
79
|
+
// Leave room for the blur's visible falloff and the sampling gutter.
|
|
80
|
+
const halfCell = Math.ceil((size + 3 * glowRadius + 1) * pixelRatio);
|
|
81
|
+
const cellSize = halfCell * 2;
|
|
82
|
+
const surface = Skia.Surface.Make(shapes.length * cellSize, cellSize);
|
|
83
|
+
const sprites = shapes.map((_, index) =>
|
|
84
|
+
Skia.XYWHRect(index * cellSize, 0, cellSize, cellSize),
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
if (!surface) {
|
|
88
|
+
return { image: null, sprites, scale: 1 / pixelRatio, inset: halfCell / pixelRatio };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const canvas = surface.getCanvas();
|
|
92
|
+
canvas.clear(Skia.Color('transparent'));
|
|
93
|
+
canvas.scale(pixelRatio, pixelRatio);
|
|
94
|
+
|
|
95
|
+
const paint = Skia.Paint();
|
|
96
|
+
paint.setAntiAlias(true);
|
|
97
|
+
paint.setColor(Skia.Color(color));
|
|
98
|
+
|
|
99
|
+
const glowPaint = Skia.Paint();
|
|
100
|
+
glowPaint.setAntiAlias(true);
|
|
101
|
+
glowPaint.setColor(Skia.Color(glowColor));
|
|
102
|
+
if (glowRadius > 0) {
|
|
103
|
+
glowPaint.setMaskFilter(Skia.MaskFilter.MakeBlur(BlurStyle.Normal, glowRadius, true));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
shapes.forEach((particleShape, index) => {
|
|
107
|
+
const x = (index * cellSize + halfCell) / pixelRatio;
|
|
108
|
+
const y = halfCell / pixelRatio;
|
|
109
|
+
|
|
110
|
+
const draw = (brush: SkPaint) => {
|
|
111
|
+
if (particleShape === 'circle') {
|
|
112
|
+
canvas.drawCircle(x, y, size, brush);
|
|
113
|
+
} else if (particleShape === 'square') {
|
|
114
|
+
canvas.drawRect(Skia.XYWHRect(x - size, y - size, size * 2, size * 2), brush);
|
|
115
|
+
} else {
|
|
116
|
+
const path = Skia.PathBuilder.Make();
|
|
117
|
+
path.moveTo(x, y - size);
|
|
118
|
+
path.lineTo(x + size, y);
|
|
119
|
+
path.lineTo(x, y + size);
|
|
120
|
+
path.lineTo(x - size, y);
|
|
121
|
+
path.close();
|
|
122
|
+
canvas.drawPath(path.detach(), brush);
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
if (glowRadius > 0) {
|
|
127
|
+
draw(glowPaint);
|
|
128
|
+
}
|
|
129
|
+
draw(paint);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
surface.flush();
|
|
133
|
+
return {
|
|
134
|
+
image: surface.makeImageSnapshot(),
|
|
135
|
+
sprites,
|
|
136
|
+
scale: 1 / pixelRatio,
|
|
137
|
+
inset: halfCell / pixelRatio,
|
|
138
|
+
};
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
const random = (seed: number): number => {
|
|
142
|
+
const value = Math.sin(seed * 127.1 + 311.7) * 43758.5453;
|
|
143
|
+
return value - Math.floor(value);
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const getFontWeight = (weight?: TextStyle['fontWeight']): number => {
|
|
147
|
+
if (weight === 'bold' || weight === '900' || weight === '800') {
|
|
148
|
+
return 700;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (weight === '700') return 700;
|
|
152
|
+
if (weight === '600') return 600;
|
|
153
|
+
if (weight === '500') return 500;
|
|
154
|
+
if (weight === '300') return 300;
|
|
155
|
+
|
|
156
|
+
return 400;
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const createParticles = (
|
|
160
|
+
text: string,
|
|
161
|
+
fontSize: number,
|
|
162
|
+
particleSize: number,
|
|
163
|
+
stagger: number,
|
|
164
|
+
duration: number,
|
|
165
|
+
fontFamily: string,
|
|
166
|
+
fontWeight: number,
|
|
167
|
+
): ParticleLayout => {
|
|
168
|
+
const empty: ParticleLayout = {
|
|
169
|
+
particles: [],
|
|
170
|
+
width: 0,
|
|
171
|
+
height: 0,
|
|
172
|
+
totalDuration: duration,
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
if (!text.trim()) {
|
|
176
|
+
return empty;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const builder = Skia.ParagraphBuilder.Make();
|
|
180
|
+
|
|
181
|
+
builder
|
|
182
|
+
.pushStyle({
|
|
183
|
+
color: Skia.Color('#FFFFFF'),
|
|
184
|
+
fontFamilies: [fontFamily],
|
|
185
|
+
fontSize,
|
|
186
|
+
fontStyle: {
|
|
187
|
+
weight: fontWeight,
|
|
188
|
+
},
|
|
189
|
+
})
|
|
190
|
+
.addText(text)
|
|
191
|
+
.pop();
|
|
192
|
+
|
|
193
|
+
const paragraph = builder.build();
|
|
194
|
+
|
|
195
|
+
paragraph.layout(10000);
|
|
196
|
+
|
|
197
|
+
// Sample the painted paragraph to include every line and resolved glyph.
|
|
198
|
+
const maskPadding = Math.ceil(fontSize / 2);
|
|
199
|
+
const maskWidth = Math.ceil((paragraph.getLongestLine() + maskPadding * 2) * MASK_SCALE);
|
|
200
|
+
const maskHeight = Math.ceil((paragraph.getHeight() + maskPadding * 2) * MASK_SCALE);
|
|
201
|
+
const surface = Skia.Surface.Make(maskWidth, maskHeight);
|
|
202
|
+
if (!surface) {
|
|
203
|
+
return empty;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const canvas = surface.getCanvas();
|
|
207
|
+
canvas.clear(Skia.Color('transparent'));
|
|
208
|
+
canvas.scale(MASK_SCALE, MASK_SCALE);
|
|
209
|
+
paragraph.paint(canvas, maskPadding, maskPadding);
|
|
210
|
+
const pixels = canvas.readPixels(0, 0, {
|
|
211
|
+
width: maskWidth,
|
|
212
|
+
height: maskHeight,
|
|
213
|
+
colorType: ColorType.RGBA_8888,
|
|
214
|
+
alphaType: AlphaType.Premul,
|
|
215
|
+
});
|
|
216
|
+
if (!pixels) {
|
|
217
|
+
return empty;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const alphaAt = (x: number, y: number) => pixels[(y * maskWidth + x) * 4 + 3];
|
|
221
|
+
let left = maskWidth;
|
|
222
|
+
let right = -1;
|
|
223
|
+
let top = maskHeight;
|
|
224
|
+
let bottom = -1;
|
|
225
|
+
for (let y = 0; y < maskHeight; y++) {
|
|
226
|
+
for (let x = 0; x < maskWidth; x++) {
|
|
227
|
+
if (alphaAt(x, y) >= 64) {
|
|
228
|
+
left = Math.min(left, x);
|
|
229
|
+
right = Math.max(right, x);
|
|
230
|
+
top = Math.min(top, y);
|
|
231
|
+
bottom = Math.max(bottom, y);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
if (right < left || bottom < top) {
|
|
236
|
+
return empty;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const cellSize = Math.round(Math.max(3, particleSize * 2.5) * MASK_SCALE);
|
|
240
|
+
const width = Math.ceil((right - left + 1) / MASK_SCALE + PADDING * 2);
|
|
241
|
+
const height = Math.ceil((bottom - top + 1) / MASK_SCALE + PADDING * 2);
|
|
242
|
+
const particles: ParticleData[] = [];
|
|
243
|
+
|
|
244
|
+
for (let y = top; y <= bottom; y += cellSize) {
|
|
245
|
+
const endY = Math.min(y + cellSize, bottom + 1);
|
|
246
|
+
for (let x = left; x <= right; x += cellSize) {
|
|
247
|
+
const endX = Math.min(x + cellSize, right + 1);
|
|
248
|
+
let sampleX = (x + endX) / 2;
|
|
249
|
+
let sampleY = (y + endY) / 2;
|
|
250
|
+
|
|
251
|
+
if (alphaAt(Math.floor(sampleX), Math.floor(sampleY)) < 128) {
|
|
252
|
+
// A thin stroke can cross a cell without touching its center.
|
|
253
|
+
let coverage = 0;
|
|
254
|
+
let inkX = 0;
|
|
255
|
+
let inkY = 0;
|
|
256
|
+
for (let row = y; row < endY; row++) {
|
|
257
|
+
for (let column = x; column < endX; column++) {
|
|
258
|
+
const alpha = alphaAt(column, row);
|
|
259
|
+
coverage += alpha;
|
|
260
|
+
inkX += (column + 0.5) * alpha;
|
|
261
|
+
inkY += (row + 0.5) * alpha;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
if (coverage < 0.18 * 255 * (endX - x) * (endY - y)) {
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
sampleX = inkX / coverage;
|
|
268
|
+
sampleY = inkY / coverage;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const seed = particles.length;
|
|
272
|
+
const targetX = (sampleX - left) / MASK_SCALE + PADDING;
|
|
273
|
+
const targetY = (sampleY - top) / MASK_SCALE + PADDING;
|
|
274
|
+
const spreadX = (random(seed + 1) - 0.5) * width * 1.5;
|
|
275
|
+
const spreadY = (random(seed + 2) - 0.5) * height * 3;
|
|
276
|
+
|
|
277
|
+
particles.push({
|
|
278
|
+
x: targetX,
|
|
279
|
+
y: targetY,
|
|
280
|
+
startX: targetX + spreadX,
|
|
281
|
+
startY: targetY + spreadY,
|
|
282
|
+
delay: stagger * seed,
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
const maxDelay =
|
|
288
|
+
Math.max(0, particles.length - 1) * stagger;
|
|
289
|
+
|
|
290
|
+
return {
|
|
291
|
+
particles,
|
|
292
|
+
width,
|
|
293
|
+
height,
|
|
294
|
+
totalDuration: duration + maxDelay,
|
|
295
|
+
};
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
export const ParticleText: React.FC<ParticleTextProps> = ({
|
|
299
|
+
text,
|
|
300
|
+
fontSize = 56,
|
|
301
|
+
particleSize = 2,
|
|
302
|
+
duration = 2500,
|
|
303
|
+
delay,
|
|
304
|
+
stagger = 0,
|
|
305
|
+
loop = false,
|
|
306
|
+
direction = 'assemble',
|
|
307
|
+
particleColor = '#A855F7',
|
|
308
|
+
|
|
309
|
+
particleShape = 'circle',
|
|
310
|
+
|
|
311
|
+
particleGlow = false,
|
|
312
|
+
glowRadius = 8,
|
|
313
|
+
glowColor,
|
|
314
|
+
|
|
315
|
+
style,
|
|
316
|
+
containerStyle,
|
|
317
|
+
}) => {
|
|
318
|
+
const progress = useSharedValue(
|
|
319
|
+
direction === 'assemble' ? 0 : 1,
|
|
320
|
+
);
|
|
321
|
+
|
|
322
|
+
const [canvasReady, setCanvasReady] = useState(false);
|
|
323
|
+
const canvasRef = useCanvasRef();
|
|
324
|
+
|
|
325
|
+
const safeDuration = Math.max(1, duration);
|
|
326
|
+
const assembling = direction === 'assemble';
|
|
327
|
+
const startDelay = Math.max(0, delay ?? (assembling ? 0 : 750));
|
|
328
|
+
const repeatDelay = delay === undefined && assembling ? 400 : startDelay;
|
|
329
|
+
const safeStagger = Math.max(0, stagger);
|
|
330
|
+
const safeParticleSize = Math.max(0.5, particleSize);
|
|
331
|
+
const safeGlowRadius = Math.max(0, glowRadius);
|
|
332
|
+
|
|
333
|
+
const fontFamily = style?.fontFamily ?? 'Arial';
|
|
334
|
+
const fontWeight = getFontWeight(style?.fontWeight);
|
|
335
|
+
|
|
336
|
+
const layout = useMemo(
|
|
337
|
+
() =>
|
|
338
|
+
createParticles(
|
|
339
|
+
text,
|
|
340
|
+
fontSize,
|
|
341
|
+
safeParticleSize,
|
|
342
|
+
safeStagger,
|
|
343
|
+
safeDuration,
|
|
344
|
+
fontFamily,
|
|
345
|
+
fontWeight,
|
|
346
|
+
),
|
|
347
|
+
[
|
|
348
|
+
text,
|
|
349
|
+
fontSize,
|
|
350
|
+
safeParticleSize,
|
|
351
|
+
safeStagger,
|
|
352
|
+
safeDuration,
|
|
353
|
+
fontFamily,
|
|
354
|
+
fontWeight,
|
|
355
|
+
],
|
|
356
|
+
);
|
|
357
|
+
const { particles, totalDuration } = layout;
|
|
358
|
+
const hasParticles = particles.length > 0;
|
|
359
|
+
const atlasGlowRadius = particleGlow ? safeGlowRadius : 0;
|
|
360
|
+
const atlasGlowColor = glowColor ?? particleColor;
|
|
361
|
+
const { image, sprites, scale, inset } = useMemo(
|
|
362
|
+
() => createParticleAtlas(
|
|
363
|
+
particleShape,
|
|
364
|
+
safeParticleSize,
|
|
365
|
+
particleColor,
|
|
366
|
+
atlasGlowRadius,
|
|
367
|
+
atlasGlowColor,
|
|
368
|
+
),
|
|
369
|
+
[particleShape, safeParticleSize, particleColor, atlasGlowRadius, atlasGlowColor],
|
|
370
|
+
);
|
|
371
|
+
const particleCount = particles.length;
|
|
372
|
+
const particleSprites = useMemo(
|
|
373
|
+
() => Array.from({ length: particleCount }, (_, index) => sprites[index % sprites.length]),
|
|
374
|
+
[particleCount, sprites],
|
|
375
|
+
);
|
|
376
|
+
const transforms = useRSXformBuffer(particleCount, (transform, index) => {
|
|
377
|
+
'worklet';
|
|
378
|
+
const particle = particles[index];
|
|
379
|
+
const elapsed = progress.value * totalDuration;
|
|
380
|
+
const phase = Math.max(0, Math.min(1, (elapsed - particle.delay) / safeDuration));
|
|
381
|
+
transform.set(
|
|
382
|
+
scale,
|
|
383
|
+
0,
|
|
384
|
+
particle.startX + (particle.x - particle.startX) * phase - inset,
|
|
385
|
+
particle.startY + (particle.y - particle.startY) * phase - inset,
|
|
386
|
+
);
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
useEffect(() => {
|
|
390
|
+
if (!hasParticles) {
|
|
391
|
+
setCanvasReady(false);
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
let cancelled = false;
|
|
396
|
+
let frame: number;
|
|
397
|
+
const measureCanvas = () => {
|
|
398
|
+
canvasRef.current?.measure((_x, _y, width, height) => {
|
|
399
|
+
if (cancelled) {
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
if (width > 0 && height > 0) {
|
|
403
|
+
setCanvasReady(true);
|
|
404
|
+
} else {
|
|
405
|
+
frame = requestAnimationFrame(measureCanvas);
|
|
406
|
+
}
|
|
407
|
+
});
|
|
408
|
+
};
|
|
409
|
+
frame = requestAnimationFrame(measureCanvas);
|
|
410
|
+
return () => {
|
|
411
|
+
cancelled = true;
|
|
412
|
+
cancelAnimationFrame(frame);
|
|
413
|
+
};
|
|
414
|
+
}, [canvasRef, hasParticles]);
|
|
415
|
+
|
|
416
|
+
useEffect(() => {
|
|
417
|
+
cancelAnimation(progress);
|
|
418
|
+
|
|
419
|
+
if (!canvasReady || !layout.particles.length) {
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
const start = assembling ? 0 : 1;
|
|
424
|
+
const end = 1 - start;
|
|
425
|
+
|
|
426
|
+
progress.value = start;
|
|
427
|
+
|
|
428
|
+
const forward = withTiming(end, {
|
|
429
|
+
duration: layout.totalDuration,
|
|
430
|
+
easing: assembling ? Easing.out(Easing.cubic) : Easing.inOut(Easing.cubic),
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
const backward = withTiming(start, {
|
|
434
|
+
duration: layout.totalDuration,
|
|
435
|
+
easing: Easing.inOut(Easing.cubic),
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
const animation = loop
|
|
439
|
+
? withRepeat(
|
|
440
|
+
withSequence(
|
|
441
|
+
forward,
|
|
442
|
+
withDelay(400, backward),
|
|
443
|
+
withTiming(start, { duration: Math.max(1, repeatDelay) }),
|
|
444
|
+
),
|
|
445
|
+
-1,
|
|
446
|
+
false,
|
|
447
|
+
)
|
|
448
|
+
: forward;
|
|
449
|
+
progress.value = startDelay > 0 ? withDelay(startDelay, animation) : animation;
|
|
450
|
+
|
|
451
|
+
return () => {
|
|
452
|
+
cancelAnimation(progress);
|
|
453
|
+
};
|
|
454
|
+
}, [
|
|
455
|
+
canvasReady,
|
|
456
|
+
assembling,
|
|
457
|
+
loop,
|
|
458
|
+
layout,
|
|
459
|
+
progress,
|
|
460
|
+
repeatDelay,
|
|
461
|
+
startDelay,
|
|
462
|
+
]);
|
|
463
|
+
|
|
464
|
+
if (!hasParticles) {
|
|
465
|
+
return null;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
return (
|
|
469
|
+
<Canvas
|
|
470
|
+
ref={canvasRef}
|
|
471
|
+
style={[
|
|
472
|
+
{
|
|
473
|
+
width: layout.width,
|
|
474
|
+
height: layout.height,
|
|
475
|
+
},
|
|
476
|
+
containerStyle,
|
|
477
|
+
]}
|
|
478
|
+
>
|
|
479
|
+
<Atlas
|
|
480
|
+
image={image}
|
|
481
|
+
sprites={particleSprites}
|
|
482
|
+
transforms={transforms}
|
|
483
|
+
sampling={ATLAS_SAMPLING}
|
|
484
|
+
/>
|
|
485
|
+
</Canvas>
|
|
486
|
+
);
|
|
487
|
+
};
|
|
488
|
+
|
|
489
|
+
export default ParticleText;
|