react-native-theme-transition 1.0.6 → 2.0.0

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 (66) hide show
  1. package/README.md +55 -43
  2. package/README.npm.md +55 -43
  3. package/dist/constants.d.ts +1 -2
  4. package/dist/constants.d.ts.map +1 -1
  5. package/dist/constants.js +4 -0
  6. package/dist/constants.js.map +1 -1
  7. package/dist/createThemeTransition.d.ts +6 -18
  8. package/dist/createThemeTransition.d.ts.map +1 -1
  9. package/dist/createThemeTransition.js +37 -35
  10. package/dist/createThemeTransition.js.map +1 -1
  11. package/dist/index.d.ts +9 -9
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +8 -8
  14. package/dist/index.js.map +1 -1
  15. package/dist/overlay/SkiaOverlay.d.ts +17 -0
  16. package/dist/overlay/SkiaOverlay.d.ts.map +1 -0
  17. package/dist/overlay/SkiaOverlay.js +297 -0
  18. package/dist/overlay/SkiaOverlay.js.map +1 -0
  19. package/dist/overlay/captureView.d.ts +7 -0
  20. package/dist/overlay/captureView.d.ts.map +1 -0
  21. package/dist/overlay/captureView.js +31 -0
  22. package/dist/overlay/captureView.js.map +1 -0
  23. package/dist/overlay/resolveOrigin.d.ts +8 -0
  24. package/dist/overlay/resolveOrigin.d.ts.map +1 -0
  25. package/dist/overlay/resolveOrigin.js +68 -0
  26. package/dist/overlay/resolveOrigin.js.map +1 -0
  27. package/dist/overlay/types.d.ts +15 -0
  28. package/dist/overlay/types.d.ts.map +1 -0
  29. package/dist/overlay/types.js +61 -0
  30. package/dist/overlay/types.js.map +1 -0
  31. package/dist/setThemeDecision.d.ts +14 -0
  32. package/dist/setThemeDecision.d.ts.map +1 -0
  33. package/dist/setThemeDecision.js +27 -0
  34. package/dist/setThemeDecision.js.map +1 -0
  35. package/dist/transitionEngine.d.ts +48 -17
  36. package/dist/transitionEngine.d.ts.map +1 -1
  37. package/dist/transitionEngine.js +279 -208
  38. package/dist/transitionEngine.js.map +1 -1
  39. package/dist/transitionMeta.d.ts +136 -0
  40. package/dist/transitionMeta.d.ts.map +1 -0
  41. package/dist/transitionMeta.js +131 -0
  42. package/dist/transitionMeta.js.map +1 -0
  43. package/dist/types.d.ts +254 -193
  44. package/dist/types.d.ts.map +1 -1
  45. package/dist/validateSetThemeOptions.d.ts +15 -0
  46. package/dist/validateSetThemeOptions.d.ts.map +1 -0
  47. package/dist/validateSetThemeOptions.js +54 -0
  48. package/dist/validateSetThemeOptions.js.map +1 -0
  49. package/package.json +14 -6
  50. package/src/constants.ts +6 -0
  51. package/src/createThemeTransition.ts +57 -39
  52. package/src/index.ts +12 -9
  53. package/src/overlay/SkiaOverlay.tsx +629 -0
  54. package/src/overlay/captureView.ts +32 -0
  55. package/src/overlay/resolveOrigin.ts +81 -0
  56. package/src/overlay/types.ts +83 -0
  57. package/src/setThemeDecision.ts +48 -0
  58. package/src/transitionEngine.tsx +390 -247
  59. package/src/transitionMeta.ts +176 -0
  60. package/src/types.ts +272 -193
  61. package/src/validateSetThemeOptions.ts +76 -0
  62. package/dist/hooks/useTheme.d.ts +0 -14
  63. package/dist/hooks/useTheme.d.ts.map +0 -1
  64. package/dist/hooks/useTheme.js +0 -46
  65. package/dist/hooks/useTheme.js.map +0 -1
  66. package/src/hooks/useTheme.ts +0 -72
@@ -0,0 +1,629 @@
1
+ /**
2
+ * Skia-powered overlay for theme transitions.
3
+ *
4
+ * @remarks
5
+ * Top-level `SkiaOverlay` keeps a `<Canvas>` permanently mounted (so
6
+ * starting a transition never pays for a native-view layout pass on
7
+ * Android) and dispatches to a per-mode component that owns the
8
+ * `useDerivedValue` hooks for that mode only. The active mode's hooks
9
+ * mount when the transition starts and unmount when it ends. `fade`
10
+ * never pays for `slide`'s slot machine, `pixelize` never pays for
11
+ * `heart`'s path interpolation, etc.
12
+ *
13
+ * @module
14
+ * @internal
15
+ */
16
+
17
+ import type { SkImage, SkPath } from '@shopify/react-native-skia'
18
+ import {
19
+ Canvas,
20
+ Circle,
21
+ Fill,
22
+ Group,
23
+ ImageShader,
24
+ Rect,
25
+ Shader,
26
+ Skia,
27
+ Image as SkiaImage,
28
+ usePathInterpolation,
29
+ } from '@shopify/react-native-skia'
30
+ import { useMemo } from 'react'
31
+ import type { SharedValue } from 'react-native-reanimated'
32
+ import { useDerivedValue } from 'react-native-reanimated'
33
+ import { ABSOLUTE_FILL } from '../constants'
34
+ import type { WipeDirection } from '../transitionMeta'
35
+ import type { OverlayParams } from './types'
36
+
37
+ /** @internal Frozen style for the Canvas. Hoisted so JSX never reallocates. */
38
+ const CANVAS_STYLE = [ABSOLUTE_FILL, { pointerEvents: 'none' as const }]
39
+ /** @internal Static input domain for `usePathInterpolation`. */
40
+ const KEYFRAME_INPUT = [0, 1]
41
+
42
+ // ───────────────────────────── Path builders ──────────────────────────────
43
+
44
+ /**
45
+ * @internal Empirically tuned heart bezier reach factors. The bounding
46
+ * math in `useHeartSize` reads these to compute the smallest scale at
47
+ * which the path covers every screen corner. Kept next to the path
48
+ * builder so the bounds and the curve stay in sync when either is
49
+ * tweaked.
50
+ */
51
+ const HEART_REACH = {
52
+ /** Bottom tip y-distance from `cy`, in heart-units. */
53
+ bottomTip: 0.7,
54
+ /** Top notch dip from `cy`, in heart-units (negative y). */
55
+ topNotch: 0.55,
56
+ /** Outer lobe x-reach in heart-units. */
57
+ lobeX: 1.2,
58
+ /** Outer lobe y-reach above `cy`, in heart-units. */
59
+ lobeY: 0.9,
60
+ /** Bezier control x for the inner curve into the bottom tip. */
61
+ innerLobeX: 0.7,
62
+ /** Y-reach factor for the bottom corners (`+y` direction). */
63
+ bottomCornerY: 0.65,
64
+ /** Y-reach factor for the top corners (`-y` direction, lobe rises higher). */
65
+ topCornerY: 0.75,
66
+ } as const
67
+
68
+ function makeHeartPath(cx: number, cy: number, w: number, h: number): SkPath {
69
+ const p = Skia.Path.Make()
70
+ p.moveTo(cx, cy + h * HEART_REACH.bottomTip)
71
+ p.cubicTo(
72
+ cx - w * HEART_REACH.lobeX,
73
+ cy + h * 0.1,
74
+ cx - w * HEART_REACH.innerLobeX,
75
+ cy - h * HEART_REACH.lobeY,
76
+ cx,
77
+ cy - h * HEART_REACH.topNotch,
78
+ )
79
+ p.cubicTo(
80
+ cx + w * HEART_REACH.innerLobeX,
81
+ cy - h * HEART_REACH.lobeY,
82
+ cx + w * HEART_REACH.lobeX,
83
+ cy + h * 0.1,
84
+ cx,
85
+ cy + h * HEART_REACH.bottomTip,
86
+ )
87
+ p.close()
88
+ return p
89
+ }
90
+
91
+ /**
92
+ * @internal Star geometry. `INNER_RATIO` is the valley-to-tip ratio
93
+ * (5-point star with deep valleys). `OUTER_PADDING` over-extends the
94
+ * tips past the screen corner so the inner valleys still cover them.
95
+ */
96
+ const STAR_GEOMETRY = { points: 5, innerRatio: 0.4, outerPadding: 1.05 } as const
97
+
98
+ function makeStarPath(cx: number, cy: number, rOuter: number, rInner: number): SkPath {
99
+ const p = Skia.Path.Make()
100
+ const step = Math.PI / STAR_GEOMETRY.points
101
+ const start = -Math.PI / 2
102
+ for (let i = 0; i < STAR_GEOMETRY.points * 2; i++) {
103
+ const r = i % 2 === 0 ? rOuter : rInner
104
+ const a = start + i * step
105
+ const x = cx + Math.cos(a) * r
106
+ const y = cy + Math.sin(a) * r
107
+ if (i === 0) p.moveTo(x, y)
108
+ else p.lineTo(x, y)
109
+ }
110
+ p.close()
111
+ return p
112
+ }
113
+
114
+ /** @internal Order keyframes so `progress` 0 → 1 grows the shape (or shrinks if inverted). */
115
+ function orderKeyframes(inverted: boolean, tiny: SkPath, full: SkPath): SkPath[] {
116
+ return inverted ? [full, tiny] : [tiny, full]
117
+ }
118
+
119
+ // ──────────────────────────── Runtime shaders ─────────────────────────────
120
+
121
+ /**
122
+ * @internal Pixelize shader. Samples both the old and new snapshots at
123
+ * a shared block-cell center and crossfades them. `blockSize` follows a
124
+ * triangle curve so both images are chunkiest at the midpoint and clear
125
+ * at the endpoints; `blend` slides linearly from 0 (all old) to 1 (all
126
+ * new).
127
+ */
128
+ const pixelizeShader = Skia.RuntimeEffect.Make(`
129
+ uniform shader imgOld;
130
+ uniform shader imgNew;
131
+ uniform float blockSize;
132
+ uniform float blend;
133
+ uniform float2 size;
134
+
135
+ half4 main(float2 xy) {
136
+ float2 cell = (floor(xy / blockSize) + 0.5) * blockSize;
137
+ cell = clamp(cell, float2(0.5), size - 0.5);
138
+ half4 a = imgOld.eval(cell);
139
+ half4 b = imgNew.eval(cell);
140
+ return a * (1.0 - blend) + b * blend;
141
+ }
142
+ `)
143
+
144
+ /**
145
+ * @internal Dissolve shader. Discards fragments whose deterministic
146
+ * noise value is below the current threshold, so the image disintegrates
147
+ * as `progress` rises.
148
+ */
149
+ const dissolveShader = Skia.RuntimeEffect.Make(`
150
+ uniform shader img;
151
+ uniform float threshold;
152
+ uniform float grain;
153
+
154
+ half4 main(float2 xy) {
155
+ float2 cell = floor(xy / grain);
156
+ float n = fract(sin(dot(cell, float2(12.9898, 78.233))) * 43758.5453);
157
+ if (n < threshold) return half4(0.0);
158
+ return img.eval(xy);
159
+ }
160
+ `)
161
+
162
+ // ─────────────────────────────── Top level ────────────────────────────────
163
+
164
+ /** @internal */
165
+ interface OverlayProps {
166
+ /**
167
+ * Snapshot of the OLD theme. `null` between transitions; the Canvas
168
+ * stays mounted but renders nothing so the next transition doesn't
169
+ * pay for a native-view layout pass.
170
+ */
171
+ image: SkImage | null
172
+ /**
173
+ * Optional snapshot of the NEW theme captured after the color swap.
174
+ * Only provided for transitions that render both themes at once
175
+ * (`slide`, `pixelize`).
176
+ */
177
+ imageNew: SkImage | null
178
+ progress: SharedValue<number>
179
+ params: OverlayParams
180
+ }
181
+
182
+ /** @internal Renders the active-mode subtree. Hooks are scoped per mode. */
183
+ export function SkiaOverlay({ image, imageNew, progress, params }: OverlayProps) {
184
+ return (
185
+ <Canvas style={CANVAS_STYLE}>
186
+ {image && (
187
+ <ActiveMode image={image} imageNew={imageNew} progress={progress} params={params} />
188
+ )}
189
+ </Canvas>
190
+ )
191
+ }
192
+
193
+ interface ActiveModeProps {
194
+ image: SkImage
195
+ imageNew: SkImage | null
196
+ progress: SharedValue<number>
197
+ params: OverlayParams
198
+ }
199
+
200
+ function ActiveMode({ image, imageNew, progress, params }: ActiveModeProps) {
201
+ switch (params.mode) {
202
+ case 'fade':
203
+ return <FadeMode image={image} progress={progress} params={params} />
204
+ case 'circularReveal':
205
+ return <CircularRevealMode image={image} progress={progress} params={params} />
206
+ case 'wipe':
207
+ return <WipeMode image={image} progress={progress} params={params} />
208
+ case 'slide':
209
+ return <SlideMode image={image} imageNew={imageNew} progress={progress} params={params} />
210
+ case 'split':
211
+ return <SplitMode image={image} progress={progress} params={params} />
212
+ case 'heart':
213
+ return <HeartMode image={image} progress={progress} params={params} />
214
+ case 'star':
215
+ return <StarMode image={image} progress={progress} params={params} />
216
+ case 'pixelize':
217
+ return <PixelizeMode image={image} imageNew={imageNew} progress={progress} params={params} />
218
+ case 'dissolve':
219
+ return <DissolveMode image={image} progress={progress} params={params} />
220
+ }
221
+ }
222
+
223
+ // ─────────────────────────── Per-mode components ──────────────────────────
224
+
225
+ interface ModeProps {
226
+ image: SkImage
227
+ progress: SharedValue<number>
228
+ params: OverlayParams
229
+ }
230
+
231
+ interface DualModeProps extends ModeProps {
232
+ imageNew: SkImage | null
233
+ }
234
+
235
+ function FadeMode({ image, progress, params }: ModeProps) {
236
+ const { screenWidth, screenHeight } = params
237
+ const opacity = useDerivedValue(() => 1 - progress.value)
238
+ return (
239
+ <Group opacity={opacity}>
240
+ <SkiaImage image={image} x={0} y={0} width={screenWidth} height={screenHeight} fit="cover" />
241
+ </Group>
242
+ )
243
+ }
244
+
245
+ function CircularRevealMode({ image, progress, params }: ModeProps) {
246
+ const { origin, maxRadius, screenWidth, screenHeight, inverted } = params
247
+ const radius = useDerivedValue(() =>
248
+ inverted ? (1 - progress.value) * maxRadius : progress.value * maxRadius,
249
+ )
250
+ if (inverted) {
251
+ return (
252
+ <Circle cx={origin.x} cy={origin.y} r={radius}>
253
+ <ImageShader image={image} fit="cover" width={screenWidth} height={screenHeight} />
254
+ </Circle>
255
+ )
256
+ }
257
+ return (
258
+ <>
259
+ <SkiaImage image={image} x={0} y={0} width={screenWidth} height={screenHeight} fit="cover" />
260
+ <Circle cx={origin.x} cy={origin.y} r={radius} blendMode="clear" color="black" />
261
+ </>
262
+ )
263
+ }
264
+
265
+ function WipeMode({ image, progress, params }: ModeProps) {
266
+ const { screenWidth, screenHeight, direction } = params
267
+ const horizontal = direction === 'left' || direction === 'right'
268
+ const vertical = direction === 'up' || direction === 'down'
269
+
270
+ const x = useDerivedValue(() => (direction === 'left' ? screenWidth * (1 - progress.value) : 0))
271
+ const y = useDerivedValue(() => (direction === 'up' ? screenHeight * (1 - progress.value) : 0))
272
+ const width = useDerivedValue(() => (horizontal ? screenWidth * progress.value : screenWidth))
273
+ const height = useDerivedValue(() => (vertical ? screenHeight * progress.value : screenHeight))
274
+
275
+ return (
276
+ <>
277
+ <SkiaImage image={image} x={0} y={0} width={screenWidth} height={screenHeight} fit="cover" />
278
+ <Rect x={x} y={y} width={width} height={height} blendMode="clear" color="black" />
279
+ </>
280
+ )
281
+ }
282
+
283
+ function SlideMode({ image, imageNew, progress, params }: DualModeProps) {
284
+ const { screenWidth, screenHeight, direction } = params
285
+ const oldOffset = useSlideOffset(progress, direction, screenWidth, screenHeight, 'old')
286
+ const newOffset = useSlideOffset(progress, direction, screenWidth, screenHeight, 'new')
287
+
288
+ // Before the second snapshot lands we render only the old image so
289
+ // the inner tree (already swapped) stays hidden.
290
+ if (!imageNew) {
291
+ return (
292
+ <SkiaImage
293
+ image={image}
294
+ x={oldOffset.x}
295
+ y={oldOffset.y}
296
+ width={screenWidth}
297
+ height={screenHeight}
298
+ fit="cover"
299
+ />
300
+ )
301
+ }
302
+ return (
303
+ <>
304
+ <SkiaImage
305
+ image={image}
306
+ x={oldOffset.x}
307
+ y={oldOffset.y}
308
+ width={screenWidth}
309
+ height={screenHeight}
310
+ fit="cover"
311
+ />
312
+ <SkiaImage
313
+ image={imageNew}
314
+ x={newOffset.x}
315
+ y={newOffset.y}
316
+ width={screenWidth}
317
+ height={screenHeight}
318
+ fit="cover"
319
+ />
320
+ </>
321
+ )
322
+ }
323
+
324
+ function useSlideOffset(
325
+ progress: SharedValue<number>,
326
+ direction: WipeDirection,
327
+ screenWidth: number,
328
+ screenHeight: number,
329
+ which: 'old' | 'new',
330
+ ) {
331
+ const x = useDerivedValue(() => {
332
+ if (direction !== 'left' && direction !== 'right') return 0
333
+ const sign = direction === 'right' ? 1 : -1
334
+ return which === 'old'
335
+ ? sign * progress.value * screenWidth
336
+ : sign * -(1 - progress.value) * screenWidth
337
+ })
338
+ const y = useDerivedValue(() => {
339
+ if (direction !== 'up' && direction !== 'down') return 0
340
+ const sign = direction === 'down' ? 1 : -1
341
+ return which === 'old'
342
+ ? sign * progress.value * screenHeight
343
+ : sign * -(1 - progress.value) * screenHeight
344
+ })
345
+ return { x, y }
346
+ }
347
+
348
+ function SplitMode({ image, progress, params }: ModeProps) {
349
+ const { screenWidth, screenHeight, splitMode, inverted } = params
350
+ if (splitMode === 'top-bottom') {
351
+ return (
352
+ <SplitTopBottom
353
+ image={image}
354
+ progress={progress}
355
+ screenWidth={screenWidth}
356
+ screenHeight={screenHeight}
357
+ inverted={inverted}
358
+ />
359
+ )
360
+ }
361
+ return (
362
+ <SplitLeftRight
363
+ image={image}
364
+ progress={progress}
365
+ screenWidth={screenWidth}
366
+ screenHeight={screenHeight}
367
+ inverted={inverted}
368
+ />
369
+ )
370
+ }
371
+
372
+ interface SplitAxisProps {
373
+ image: SkImage
374
+ progress: SharedValue<number>
375
+ screenWidth: number
376
+ screenHeight: number
377
+ inverted: boolean
378
+ }
379
+
380
+ function SplitTopBottom({ image, progress, screenWidth, screenHeight, inverted }: SplitAxisProps) {
381
+ const halfH = screenHeight / 2
382
+ const stripH = useDerivedValue(() => halfH * progress.value)
383
+ const topY = useDerivedValue(() => (inverted ? 0 : halfH - halfH * progress.value))
384
+ const bottomY = useDerivedValue(() => (inverted ? screenHeight - halfH * progress.value : halfH))
385
+ return (
386
+ <>
387
+ <SkiaImage image={image} x={0} y={0} width={screenWidth} height={screenHeight} fit="cover" />
388
+ <Rect x={0} y={topY} width={screenWidth} height={stripH} blendMode="clear" color="black" />
389
+ <Rect x={0} y={bottomY} width={screenWidth} height={stripH} blendMode="clear" color="black" />
390
+ </>
391
+ )
392
+ }
393
+
394
+ function SplitLeftRight({ image, progress, screenWidth, screenHeight, inverted }: SplitAxisProps) {
395
+ const halfW = screenWidth / 2
396
+ const stripW = useDerivedValue(() => halfW * progress.value)
397
+ const leftX = useDerivedValue(() => (inverted ? 0 : halfW - halfW * progress.value))
398
+ const rightX = useDerivedValue(() => (inverted ? screenWidth - halfW * progress.value : halfW))
399
+ return (
400
+ <>
401
+ <SkiaImage image={image} x={0} y={0} width={screenWidth} height={screenHeight} fit="cover" />
402
+ <Rect x={leftX} y={0} width={stripW} height={screenHeight} blendMode="clear" color="black" />
403
+ <Rect x={rightX} y={0} width={stripW} height={screenHeight} blendMode="clear" color="black" />
404
+ </>
405
+ )
406
+ }
407
+
408
+ function HeartMode({ image, progress, params }: ModeProps) {
409
+ const { origin, screenWidth, screenHeight, inverted } = params
410
+ const path = useHeartPath(origin.x, origin.y, screenWidth, screenHeight, inverted, progress)
411
+ return (
412
+ <ShapeClip
413
+ image={image}
414
+ path={path}
415
+ inverted={inverted}
416
+ screenWidth={screenWidth}
417
+ screenHeight={screenHeight}
418
+ />
419
+ )
420
+ }
421
+
422
+ function StarMode({ image, progress, params }: ModeProps) {
423
+ const { origin, maxRadius, screenWidth, screenHeight, inverted } = params
424
+ const path = useStarPath(origin.x, origin.y, maxRadius, inverted, progress)
425
+ return (
426
+ <ShapeClip
427
+ image={image}
428
+ path={path}
429
+ inverted={inverted}
430
+ screenWidth={screenWidth}
431
+ screenHeight={screenHeight}
432
+ />
433
+ )
434
+ }
435
+
436
+ interface ShapeClipProps {
437
+ image: SkImage
438
+ path: SharedValue<SkPath>
439
+ inverted: boolean
440
+ screenWidth: number
441
+ screenHeight: number
442
+ }
443
+
444
+ function ShapeClip({ image, path, inverted, screenWidth, screenHeight }: ShapeClipProps) {
445
+ if (inverted) {
446
+ return (
447
+ <Group clip={path}>
448
+ <SkiaImage
449
+ image={image}
450
+ x={0}
451
+ y={0}
452
+ width={screenWidth}
453
+ height={screenHeight}
454
+ fit="cover"
455
+ />
456
+ </Group>
457
+ )
458
+ }
459
+ return (
460
+ <>
461
+ <SkiaImage image={image} x={0} y={0} width={screenWidth} height={screenHeight} fit="cover" />
462
+ <Group clip={path}>
463
+ <Rect
464
+ x={0}
465
+ y={0}
466
+ width={screenWidth}
467
+ height={screenHeight}
468
+ blendMode="clear"
469
+ color="black"
470
+ />
471
+ </Group>
472
+ </>
473
+ )
474
+ }
475
+
476
+ function useHeartPath(
477
+ originX: number,
478
+ originY: number,
479
+ screenWidth: number,
480
+ screenHeight: number,
481
+ inverted: boolean,
482
+ progress: SharedValue<number>,
483
+ ) {
484
+ // Smallest heart scale that covers all four corners AND the top
485
+ // notch from a low origin. Reach factors live next to the path
486
+ // builder so the math stays in sync with the curve.
487
+ const heartSize = useMemo(() => {
488
+ const cornerSize = (dx: number, dy: number) => {
489
+ const fy = dy < 0 ? HEART_REACH.topCornerY : HEART_REACH.bottomCornerY
490
+ return Math.abs(dx) + Math.abs(dy) / fy
491
+ }
492
+ const notch = originY / HEART_REACH.topNotch
493
+ return (
494
+ Math.max(
495
+ cornerSize(-originX, -originY),
496
+ cornerSize(screenWidth - originX, -originY),
497
+ cornerSize(-originX, screenHeight - originY),
498
+ cornerSize(screenWidth - originX, screenHeight - originY),
499
+ notch,
500
+ ) * 1.05
501
+ )
502
+ }, [originX, originY, screenWidth, screenHeight])
503
+
504
+ const keyframes = useMemo(
505
+ () =>
506
+ orderKeyframes(
507
+ inverted,
508
+ makeHeartPath(originX, originY, 1, 1),
509
+ makeHeartPath(originX, originY, heartSize, heartSize),
510
+ ),
511
+ [originX, originY, heartSize, inverted],
512
+ )
513
+ return usePathInterpolation(progress, KEYFRAME_INPUT, keyframes)
514
+ }
515
+
516
+ function useStarPath(
517
+ originX: number,
518
+ originY: number,
519
+ maxRadius: number,
520
+ inverted: boolean,
521
+ progress: SharedValue<number>,
522
+ ) {
523
+ const { outer, inner } = useMemo(() => {
524
+ const outerR = (maxRadius / STAR_GEOMETRY.innerRatio) * STAR_GEOMETRY.outerPadding
525
+ return { outer: outerR, inner: outerR * STAR_GEOMETRY.innerRatio }
526
+ }, [maxRadius])
527
+ const keyframes = useMemo(
528
+ () =>
529
+ orderKeyframes(
530
+ inverted,
531
+ makeStarPath(originX, originY, 1, STAR_GEOMETRY.innerRatio),
532
+ makeStarPath(originX, originY, outer, inner),
533
+ ),
534
+ [originX, originY, outer, inner, inverted],
535
+ )
536
+ return usePathInterpolation(progress, KEYFRAME_INPUT, keyframes)
537
+ }
538
+
539
+ function PixelizeMode({ image, imageNew, progress, params }: DualModeProps) {
540
+ const { screenWidth, screenHeight, blockSize } = params
541
+ const screenSize = useMemo(() => [screenWidth, screenHeight], [screenWidth, screenHeight])
542
+
543
+ // Triangle curve on `blockSize`: linear ramp up to the midpoint, cubic
544
+ // ease-out on the way back so the back half spends more time near
545
+ // "clear" than near "chunky" - masks the disappearance pop.
546
+ const uniforms = useDerivedValue(() => {
547
+ const p = progress.value
548
+ let tri: number
549
+ if (p < 0.5) {
550
+ tri = p / 0.5
551
+ } else {
552
+ const t = (p - 0.5) / 0.5
553
+ const inv = 1 - t
554
+ tri = inv * inv * inv
555
+ }
556
+ return {
557
+ blockSize: 2 + tri * (blockSize - 2),
558
+ blend: p,
559
+ size: screenSize,
560
+ }
561
+ })
562
+
563
+ // Linear opacity fade over the last 15% so the unmount at p=1 doesn't
564
+ // pop a frame between the shader output and the real underlying view.
565
+ const opacity = useDerivedValue(() => {
566
+ if (progress.value <= 0.85) return 1
567
+ return 1 - (progress.value - 0.85) / 0.15
568
+ })
569
+
570
+ // Until the second snapshot lands we hold the old image at full
571
+ // opacity - the inner tree has already been swapped, so it must stay
572
+ // hidden under the overlay.
573
+ if (!imageNew || !pixelizeShader) {
574
+ return (
575
+ <SkiaImage image={image} x={0} y={0} width={screenWidth} height={screenHeight} fit="cover" />
576
+ )
577
+ }
578
+ return (
579
+ <Group opacity={opacity}>
580
+ <Fill>
581
+ <Shader source={pixelizeShader} uniforms={uniforms}>
582
+ <ImageShader
583
+ image={image}
584
+ fit="cover"
585
+ width={screenWidth}
586
+ height={screenHeight}
587
+ tx="clamp"
588
+ ty="clamp"
589
+ />
590
+ <ImageShader
591
+ image={imageNew}
592
+ fit="cover"
593
+ width={screenWidth}
594
+ height={screenHeight}
595
+ tx="clamp"
596
+ ty="clamp"
597
+ />
598
+ </Shader>
599
+ </Fill>
600
+ </Group>
601
+ )
602
+ }
603
+
604
+ function DissolveMode({ image, progress, params }: ModeProps) {
605
+ const { screenWidth, screenHeight, noiseSize } = params
606
+ const uniforms = useDerivedValue(() => ({
607
+ threshold: progress.value,
608
+ grain: noiseSize,
609
+ }))
610
+ if (!dissolveShader) {
611
+ return (
612
+ <SkiaImage image={image} x={0} y={0} width={screenWidth} height={screenHeight} fit="cover" />
613
+ )
614
+ }
615
+ return (
616
+ <Fill>
617
+ <Shader source={dissolveShader} uniforms={uniforms}>
618
+ <ImageShader
619
+ image={image}
620
+ fit="cover"
621
+ width={screenWidth}
622
+ height={screenHeight}
623
+ tx="clamp"
624
+ ty="clamp"
625
+ />
626
+ </Shader>
627
+ </Fill>
628
+ )
629
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * View-snapshot capture via Skia's `makeImageFromView`.
3
+ * @module
4
+ * @internal
5
+ */
6
+
7
+ import type { SkImage } from '@shopify/react-native-skia'
8
+ import { makeImageFromView } from '@shopify/react-native-skia'
9
+ import type { RefObject } from 'react'
10
+ import type { View } from 'react-native'
11
+
12
+ /**
13
+ * Captures a CPU-backed snapshot of the given view. Returns `null` if the
14
+ * capture fails (e.g. the view has unmounted or Skia is unavailable).
15
+ * @internal
16
+ */
17
+ export async function captureView(ref: RefObject<View | null>): Promise<SkImage | null> {
18
+ try {
19
+ const gpuImage = await makeImageFromView(ref)
20
+ if (!gpuImage) return null
21
+ // Convert to CPU-backed so the snapshot renders in a separate GL
22
+ // context (Skia Canvas). `finally` guarantees the GPU image is
23
+ // released even if the conversion throws.
24
+ try {
25
+ return gpuImage.makeNonTextureImage()
26
+ } finally {
27
+ gpuImage.dispose()
28
+ }
29
+ } catch {
30
+ return null
31
+ }
32
+ }