react-native-effects 0.1.0 → 0.2.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 (51) hide show
  1. package/README.md +75 -13
  2. package/lib/module/components/ShaderView/index.js +33 -5
  3. package/lib/module/components/ShaderView/index.js.map +1 -1
  4. package/lib/module/components/ShaderViewWithPanGesture/index.js +196 -0
  5. package/lib/module/components/ShaderViewWithPanGesture/index.js.map +1 -0
  6. package/lib/module/hooks/useParamsSynchronizable.js +37 -0
  7. package/lib/module/hooks/useParamsSynchronizable.js.map +1 -0
  8. package/lib/module/hooks/useWGPUSetup.js +1 -1
  9. package/lib/module/hooks/useWGPUSetup.js.map +1 -1
  10. package/lib/module/index.js +3 -1
  11. package/lib/module/index.js.map +1 -1
  12. package/lib/module/shaders/uniforms.js +4 -3
  13. package/lib/module/shaders/uniforms.js.map +1 -1
  14. package/lib/typescript/src/components/Aurora.d.ts +1 -1
  15. package/lib/typescript/src/components/Aurora.d.ts.map +1 -1
  16. package/lib/typescript/src/components/CalicoSwirl.d.ts +1 -1
  17. package/lib/typescript/src/components/CalicoSwirl.d.ts.map +1 -1
  18. package/lib/typescript/src/components/Campfire.d.ts +1 -1
  19. package/lib/typescript/src/components/Campfire.d.ts.map +1 -1
  20. package/lib/typescript/src/components/CircularGradient.d.ts +1 -1
  21. package/lib/typescript/src/components/CircularGradient.d.ts.map +1 -1
  22. package/lib/typescript/src/components/Iridescence.d.ts +1 -1
  23. package/lib/typescript/src/components/Iridescence.d.ts.map +1 -1
  24. package/lib/typescript/src/components/LinearGradient.d.ts +1 -1
  25. package/lib/typescript/src/components/LinearGradient.d.ts.map +1 -1
  26. package/lib/typescript/src/components/LiquidChrome.d.ts +1 -1
  27. package/lib/typescript/src/components/LiquidChrome.d.ts.map +1 -1
  28. package/lib/typescript/src/components/ShaderView/index.d.ts +1 -1
  29. package/lib/typescript/src/components/ShaderView/index.d.ts.map +1 -1
  30. package/lib/typescript/src/components/ShaderView/types.d.ts +20 -0
  31. package/lib/typescript/src/components/ShaderView/types.d.ts.map +1 -1
  32. package/lib/typescript/src/components/ShaderViewWithPanGesture/index.d.ts +35 -0
  33. package/lib/typescript/src/components/ShaderViewWithPanGesture/index.d.ts.map +1 -0
  34. package/lib/typescript/src/components/Silk.d.ts +1 -1
  35. package/lib/typescript/src/components/Silk.d.ts.map +1 -1
  36. package/lib/typescript/src/hooks/useParamsSynchronizable.d.ts +22 -0
  37. package/lib/typescript/src/hooks/useParamsSynchronizable.d.ts.map +1 -0
  38. package/lib/typescript/src/hooks/useWGPUSetup.d.ts +1 -1
  39. package/lib/typescript/src/hooks/useWGPUSetup.d.ts.map +1 -1
  40. package/lib/typescript/src/index.d.ts +6 -2
  41. package/lib/typescript/src/index.d.ts.map +1 -1
  42. package/lib/typescript/src/shaders/uniforms.d.ts +3 -3
  43. package/lib/typescript/src/shaders/uniforms.d.ts.map +1 -1
  44. package/package.json +31 -30
  45. package/src/components/ShaderView/index.tsx +42 -5
  46. package/src/components/ShaderView/types.ts +21 -0
  47. package/src/components/ShaderViewWithPanGesture/index.tsx +225 -0
  48. package/src/hooks/useParamsSynchronizable.ts +52 -0
  49. package/src/hooks/useWGPUSetup.tsx +1 -1
  50. package/src/index.tsx +10 -1
  51. package/src/shaders/uniforms.ts +4 -3
@@ -0,0 +1,225 @@
1
+ import { useCallback, useRef } from 'react';
2
+ import { StyleSheet, View, type LayoutChangeEvent } from 'react-native';
3
+ import { Gesture, GestureDetector } from 'react-native-gesture-handler';
4
+ import {
5
+ createSynchronizable,
6
+ type Synchronizable,
7
+ } from 'react-native-worklets';
8
+ import ShaderView from '../ShaderView';
9
+ import { useParamsSynchronizable } from '../../hooks/useParamsSynchronizable';
10
+ import type { ShaderViewProps } from '../ShaderView/types';
11
+
12
+ /**
13
+ * A {@link ShaderView} that feeds touch input into the shader's `u.live`:
14
+ *
15
+ * - `live.x` → pointer X, normalized 0..1 (left → right)
16
+ * - `live.y` → pointer Y, normalized 0..1 (bottom → top, matching UV space)
17
+ * - `live.z` → 1.0 while touching, 0.0 when released
18
+ * - `live.w` → 0.0 (reserved)
19
+ *
20
+ * Dragging moves the pointer **relatively** — it pushes from where the pointer
21
+ * already is rather than jumping under the finger — and a fling lets it glide to
22
+ * a stop. The position is **remembered**: it stays wherever it ended and is
23
+ * never reset; only the "touched" flag (`live.z`) toggles on release. A
24
+ * shader can read `live.xy` as a stable resting position and use `live.z`
25
+ * purely for touch-driven emphasis, so the effect never snaps back.
26
+ *
27
+ * The resting value before the first touch is `[0, 0, 0, 0]` by default; pass
28
+ * `initialParamsSynchronizable` to seed it — e.g. `[0.5, 0.5, 0, 0]` to start a
29
+ * pointer at screen center.
30
+ *
31
+ * The drag runs as a **worklet on the UI thread** and writes the synchronizable
32
+ * directly, so pointer updates never hop to the JS thread — matching the rest of
33
+ * the library, which renders off the JS thread. The render runtime reads the
34
+ * same synchronizable each frame.
35
+ */
36
+ export type ShaderViewWithPanGestureProps = Omit<
37
+ ShaderViewProps,
38
+ 'paramsSynchronizable'
39
+ > & {
40
+ /**
41
+ * Initial value for the gesture channel (`u.live`) before the first touch.
42
+ * Defaults to `[0, 0, 0, 0]`. Use e.g. `[0.5, 0.5, 0, 0]` to rest a pointer at
43
+ * screen center.
44
+ */
45
+ initialParamsSynchronizable?: readonly [number, number, number, number];
46
+ };
47
+
48
+ export default function ShaderViewWithPanGesture({
49
+ style,
50
+ initialParamsSynchronizable = [0, 0, 0, 0],
51
+ ...props
52
+ }: ShaderViewWithPanGestureProps) {
53
+ const { paramsSynchronizable } = useParamsSynchronizable(
54
+ initialParamsSynchronizable
55
+ );
56
+
57
+ // View size, read inside the gesture worklets to normalize pointer coords.
58
+ const sizeRef = useRef<Synchronizable<Float64Array> | null>(null);
59
+ if (sizeRef.current === null) {
60
+ sizeRef.current = createSynchronizable<Float64Array>(Float64Array.of(1, 1));
61
+ }
62
+ const sizeSynchronizable = sizeRef.current;
63
+
64
+ // Generation of the current post-release glide; bumped to cancel an old one.
65
+ const momentumRef = useRef<Synchronizable<Float64Array> | null>(null);
66
+ if (momentumRef.current === null) {
67
+ momentumRef.current = createSynchronizable<Float64Array>(
68
+ Float64Array.of(0)
69
+ );
70
+ }
71
+ const momentumSynchronizable = momentumRef.current;
72
+
73
+ // Pointer position when the current drag began — the pan moves the pointer
74
+ // relative to this, so a drag pushes from where it was rather than jumping.
75
+ const panStartRef = useRef<Synchronizable<Float64Array> | null>(null);
76
+ if (panStartRef.current === null) {
77
+ panStartRef.current = createSynchronizable<Float64Array>(
78
+ Float64Array.of(0, 0)
79
+ );
80
+ }
81
+ const panStartSynchronizable = panStartRef.current;
82
+
83
+ const onLayout = useCallback(
84
+ (e: LayoutChangeEvent) => {
85
+ const { width, height } = e.nativeEvent.layout;
86
+ sizeSynchronizable.setBlocking(() =>
87
+ Float64Array.of(width || 1, height || 1)
88
+ );
89
+ },
90
+ [sizeSynchronizable]
91
+ );
92
+
93
+ // Worklet: runs on the UI thread and writes the normalized pointer straight
94
+ // into the synchronizable the render runtime reads, so a pointer move never
95
+ // touches the JS thread.
96
+ const writePointer = (nx: number, ny: number, active: number) => {
97
+ 'worklet';
98
+ const x = Math.min(1, Math.max(0, nx));
99
+ const y = Math.min(1, Math.max(0, ny));
100
+ paramsSynchronizable.setBlocking(() => Float64Array.of(x, y, active, 0));
101
+ };
102
+
103
+ const stopMomentum = () => {
104
+ 'worklet';
105
+ const next = (momentumSynchronizable.getDirty()[0] || 0) + 1;
106
+ momentumSynchronizable.setBlocking(() => Float64Array.of(next));
107
+ };
108
+
109
+ // Drop the touched flag in place — safety net for a gesture cancelled with no
110
+ // onEnd, so the flag never sticks.
111
+ const releaseFlag = () => {
112
+ 'worklet';
113
+ const p = paramsSynchronizable.getDirty();
114
+ const x = p[0] || 0;
115
+ const y = p[1] || 0;
116
+ paramsSynchronizable.setBlocking(() => Float64Array.of(x, y, 0, 0));
117
+ };
118
+
119
+ // After release, drift from the last position along the fling velocity and
120
+ // decay to a stop — a little inertia. Runs on the UI thread via rAF, like the
121
+ // render loop, writing each frame into the same synchronizable.
122
+ const startMomentum = (velX: number, velY: number) => {
123
+ 'worklet';
124
+ const s = sizeSynchronizable.getDirty();
125
+ const w = s[0] || 1;
126
+ const h = s[1] || 1;
127
+
128
+ // Flick speed in normalized units/sec, scaled to a subtle glide (Y flipped).
129
+ const SCALE = 0.12;
130
+ let vx = (velX / w) * SCALE;
131
+ let vy = (-velY / h) * SCALE;
132
+
133
+ const p = paramsSynchronizable.getDirty();
134
+ let x = p[0] || 0;
135
+ let y = p[1] || 0;
136
+
137
+ // Claim this glide; a newer one bumps the generation and this loop bails.
138
+ const gen = (momentumSynchronizable.getDirty()[0] || 0) + 1;
139
+ momentumSynchronizable.setBlocking(() => Float64Array.of(gen));
140
+
141
+ const FRICTION = 2; // 1/s — higher stops sooner
142
+ let last = -1;
143
+
144
+ // Plain closure (no 'worklet') so its accumulators and self-reference
145
+ // survive across frames; a serialized worklet would snapshot them by value.
146
+ const step = (now: number) => {
147
+ if ((momentumSynchronizable.getDirty()[0] || 0) !== gen) {
148
+ return;
149
+ }
150
+ const dt = last < 0 ? 0 : (now - last) / 1000;
151
+ last = now;
152
+
153
+ x = Math.min(1, Math.max(0, x + vx * dt));
154
+ y = Math.min(1, Math.max(0, y + vy * dt));
155
+ const decay = Math.exp(-FRICTION * dt);
156
+ vx = vx * decay;
157
+ vy = vy * decay;
158
+
159
+ paramsSynchronizable.setBlocking(() => Float64Array.of(x, y, 0, 0));
160
+
161
+ if (Math.abs(vx) + Math.abs(vy) > 0.0008) {
162
+ requestAnimationFrame(step);
163
+ }
164
+ };
165
+ requestAnimationFrame(step);
166
+ };
167
+
168
+ // Drag moves the pointer *relatively*: grab anywhere and push it from where it
169
+ // is, rather than snapping it under the finger. A plain tap leaves it put.
170
+ const pan = Gesture.Pan()
171
+ .onBegin(() => {
172
+ 'worklet';
173
+ stopMomentum();
174
+ const p = paramsSynchronizable.getDirty();
175
+ const sx = p[0] || 0;
176
+ const sy = p[1] || 0;
177
+ panStartSynchronizable.setBlocking(() => Float64Array.of(sx, sy));
178
+ writePointer(sx, sy, 1);
179
+ })
180
+ .onUpdate((e) => {
181
+ 'worklet';
182
+ const s = sizeSynchronizable.getDirty();
183
+ const w = s[0] || 1;
184
+ const h = s[1] || 1;
185
+ const start = panStartSynchronizable.getDirty();
186
+ // Add the drag delta; Y is flipped to match the shader's UV space.
187
+ writePointer(
188
+ (start[0] || 0) + e.translationX / w,
189
+ (start[1] || 0) - e.translationY / h,
190
+ 1
191
+ );
192
+ })
193
+ .onEnd((e) => {
194
+ 'worklet';
195
+ const p = paramsSynchronizable.getDirty();
196
+ writePointer(p[0] || 0, p[1] || 0, 0);
197
+ startMomentum(e.velocityX, e.velocityY);
198
+ })
199
+ .onFinalize(() => {
200
+ 'worklet';
201
+ releaseFlag();
202
+ });
203
+
204
+ return (
205
+ <GestureDetector gesture={pan}>
206
+ <View
207
+ style={[styles.fill, style]}
208
+ onLayout={onLayout}
209
+ collapsable={false}
210
+ >
211
+ <ShaderView
212
+ {...props}
213
+ paramsSynchronizable={paramsSynchronizable}
214
+ style={StyleSheet.absoluteFill}
215
+ />
216
+ </View>
217
+ </GestureDetector>
218
+ );
219
+ }
220
+
221
+ const styles = StyleSheet.create({
222
+ fill: {
223
+ flex: 1,
224
+ },
225
+ });
@@ -0,0 +1,52 @@
1
+ import { useCallback, useRef } from 'react';
2
+ import { createSynchronizable } from 'react-native-worklets';
3
+ import type { ParamsSynchronizable } from '../components/ShaderView/types';
4
+
5
+ /**
6
+ * Creates a {@link ParamsSynchronizable} — a 4-float channel written into the
7
+ * dedicated `u.live` slot of a {@link ShaderView} every frame. It has its own
8
+ * uniform slot, so it leaves all 8 static `params` untouched.
9
+ *
10
+ * The returned `setParamsSynchronizable` runs on the JS thread (call it from gesture or scroll
11
+ * handlers); the values are read by the off-thread render loop. By convention
12
+ * the four floats carry `(x, y, active, extra)` for pointer input, or
13
+ * `(progress, ...)` for scroll-driven effects — but the meaning is up to the
14
+ * shader consuming `u.live`.
15
+ *
16
+ * Pass `initial` to seed the channel's starting value (read once on first
17
+ * render), so the shader has a sane resting state before the first update —
18
+ * e.g. `[0.5, 0.5, 0, 0]` to start a pointer at screen center. Defaults to all
19
+ * zeros.
20
+ */
21
+ export function useParamsSynchronizable(
22
+ initial: readonly [number, number, number, number] = [0, 0, 0, 0]
23
+ ): {
24
+ paramsSynchronizable: ParamsSynchronizable;
25
+ setParamsSynchronizable: (
26
+ x: number,
27
+ y: number,
28
+ active: number,
29
+ extra: number
30
+ ) => void;
31
+ } {
32
+ // Lazily create once; `initial` is only a seed, so it is read on first render
33
+ // and ignored thereafter.
34
+ const ref = useRef<ParamsSynchronizable | null>(null);
35
+ if (ref.current === null) {
36
+ ref.current = createSynchronizable<Float64Array>(
37
+ Float64Array.of(initial[0], initial[1], initial[2], initial[3])
38
+ );
39
+ }
40
+ const paramsSynchronizable = ref.current;
41
+
42
+ const setParamsSynchronizable = useCallback(
43
+ (x: number, y: number, active: number, extra: number) => {
44
+ paramsSynchronizable.setBlocking(() =>
45
+ Float64Array.of(x, y, active, extra)
46
+ );
47
+ },
48
+ [paramsSynchronizable]
49
+ );
50
+
51
+ return { paramsSynchronizable, setParamsSynchronizable };
52
+ }
@@ -3,7 +3,7 @@ import {
3
3
  useCanvasRef,
4
4
  type CanvasRef,
5
5
  type RNCanvasContext,
6
- } from 'react-native-wgpu';
6
+ } from 'react-native-webgpu';
7
7
  import { useEffect, useState } from 'react';
8
8
  import type { WorkletRuntime } from 'react-native-worklets';
9
9
  import { initWebGPU } from '../utils/initWebGPU';
package/src/index.tsx CHANGED
@@ -1,23 +1,32 @@
1
1
  import CircularGradient from './components/CircularGradient';
2
2
  import LinearGradient from './components/LinearGradient';
3
3
  import ShaderView from './components/ShaderView';
4
+ import ShaderViewWithPanGesture from './components/ShaderViewWithPanGesture';
4
5
  import Iridescence from './components/Iridescence';
5
6
  import LiquidChrome from './components/LiquidChrome';
6
7
  import Silk from './components/Silk';
7
8
  import Campfire from './components/Campfire';
8
9
  import CalicoSwirl from './components/CalicoSwirl';
9
10
  import Aurora from './components/Aurora';
11
+ import { useParamsSynchronizable } from './hooks/useParamsSynchronizable';
10
12
 
11
- export type { ShaderViewProps } from './components/ShaderView/types';
13
+ export type {
14
+ ShaderViewProps,
15
+ ParamsSynchronizable,
16
+ } from './components/ShaderView/types';
17
+ export type { ShaderViewWithPanGestureProps } from './components/ShaderViewWithPanGesture';
18
+ export type { ColorInput } from './utils/colors';
12
19
 
13
20
  export {
14
21
  CircularGradient,
15
22
  LinearGradient,
16
23
  ShaderView,
24
+ ShaderViewWithPanGesture,
17
25
  Iridescence,
18
26
  LiquidChrome,
19
27
  Silk,
20
28
  Campfire,
21
29
  CalicoSwirl,
22
30
  Aurora,
31
+ useParamsSynchronizable,
23
32
  };
@@ -1,8 +1,8 @@
1
- /** 96 bytes = 6 × vec4<f32> */
2
- export const UNIFORM_BUFFER_SIZE = 96;
1
+ /** 112 bytes = 7 × vec4<f32> */
2
+ export const UNIFORM_BUFFER_SIZE = 112;
3
3
 
4
4
  /** Number of float32 values in the uniform buffer */
5
- export const UNIFORM_FLOAT_COUNT = UNIFORM_BUFFER_SIZE / 4; // 24
5
+ export const UNIFORM_FLOAT_COUNT = UNIFORM_BUFFER_SIZE / 4; // 28
6
6
 
7
7
  export const UNIFORMS_WGSL = /* wgsl */ `
8
8
  struct Uniforms {
@@ -12,6 +12,7 @@ struct Uniforms {
12
12
  color1: vec4<f32>, // colors[1] RGBA
13
13
  params0: vec4<f32>, // params[0..3]
14
14
  params1: vec4<f32>, // params[4..7]
15
+ live: vec4<f32>, // paramsSynchronizable (touch/scroll/audio); (0,0,0,0) when unused
15
16
  };
16
17
  @group(0) @binding(0) var<uniform> u: Uniforms;
17
18
  `;