react-native-effects 0.0.1 → 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.
- package/LICENSE +20 -0
- package/README.md +313 -0
- package/lib/module/components/Aurora.js +184 -0
- package/lib/module/components/Aurora.js.map +1 -0
- package/lib/module/components/CalicoSwirl.js +155 -0
- package/lib/module/components/CalicoSwirl.js.map +1 -0
- package/lib/module/components/Campfire.js +225 -0
- package/lib/module/components/Campfire.js.map +1 -0
- package/lib/module/components/CircularGradient.js +52 -0
- package/lib/module/components/CircularGradient.js.map +1 -0
- package/lib/module/components/Iridescence.js +57 -0
- package/lib/module/components/Iridescence.js.map +1 -0
- package/lib/module/components/LinearGradient.js +48 -0
- package/lib/module/components/LinearGradient.js.map +1 -0
- package/lib/module/components/LiquidChrome.js +75 -0
- package/lib/module/components/LiquidChrome.js.map +1 -0
- package/lib/module/components/ShaderView/index.js +252 -0
- package/lib/module/components/ShaderView/index.js.map +1 -0
- package/lib/module/components/ShaderView/types.js +4 -0
- package/lib/module/components/ShaderView/types.js.map +1 -0
- package/lib/module/components/ShaderViewWithPanGesture/index.js +196 -0
- package/lib/module/components/ShaderViewWithPanGesture/index.js.map +1 -0
- package/lib/module/components/Silk.js +83 -0
- package/lib/module/components/Silk.js.map +1 -0
- package/lib/module/consts.js +154 -0
- package/lib/module/consts.js.map +1 -0
- package/lib/module/hooks/useClock.js +15 -0
- package/lib/module/hooks/useClock.js.map +1 -0
- package/lib/module/hooks/useParamsSynchronizable.js +37 -0
- package/lib/module/hooks/useParamsSynchronizable.js.map +1 -0
- package/lib/module/hooks/useWGPUSetup.js +54 -0
- package/lib/module/hooks/useWGPUSetup.js.map +1 -0
- package/lib/module/index.js +15 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/module/shaders/TRIANGLE_VERTEX_SHADER.js +20 -0
- package/lib/module/shaders/TRIANGLE_VERTEX_SHADER.js.map +1 -0
- package/lib/module/shaders/uniforms.js +21 -0
- package/lib/module/shaders/uniforms.js.map +1 -0
- package/lib/module/utils/backgroundRuntime.js +12 -0
- package/lib/module/utils/backgroundRuntime.js.map +1 -0
- package/lib/module/utils/colors.js +94 -0
- package/lib/module/utils/colors.js.map +1 -0
- package/lib/module/utils/initWebGPU.js +40 -0
- package/lib/module/utils/initWebGPU.js.map +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/components/Aurora.d.ts +17 -0
- package/lib/typescript/src/components/Aurora.d.ts.map +1 -0
- package/lib/typescript/src/components/CalicoSwirl.d.ts +13 -0
- package/lib/typescript/src/components/CalicoSwirl.d.ts.map +1 -0
- package/lib/typescript/src/components/Campfire.d.ts +17 -0
- package/lib/typescript/src/components/Campfire.d.ts.map +1 -0
- package/lib/typescript/src/components/CircularGradient.d.ts +19 -0
- package/lib/typescript/src/components/CircularGradient.d.ts.map +1 -0
- package/lib/typescript/src/components/Iridescence.d.ts +11 -0
- package/lib/typescript/src/components/Iridescence.d.ts.map +1 -0
- package/lib/typescript/src/components/LinearGradient.d.ts +15 -0
- package/lib/typescript/src/components/LinearGradient.d.ts.map +1 -0
- package/lib/typescript/src/components/LiquidChrome.d.ts +17 -0
- package/lib/typescript/src/components/LiquidChrome.d.ts.map +1 -0
- package/lib/typescript/src/components/ShaderView/index.d.ts +3 -0
- package/lib/typescript/src/components/ShaderView/index.d.ts.map +1 -0
- package/lib/typescript/src/components/ShaderView/types.d.ts +35 -0
- package/lib/typescript/src/components/ShaderView/types.d.ts.map +1 -0
- package/lib/typescript/src/components/ShaderViewWithPanGesture/index.d.ts +35 -0
- package/lib/typescript/src/components/ShaderViewWithPanGesture/index.d.ts.map +1 -0
- package/lib/typescript/src/components/Silk.d.ts +17 -0
- package/lib/typescript/src/components/Silk.d.ts.map +1 -0
- package/lib/typescript/src/consts.d.ts +2 -0
- package/lib/typescript/src/consts.d.ts.map +1 -0
- package/lib/typescript/src/hooks/useClock.d.ts +3 -0
- package/lib/typescript/src/hooks/useClock.d.ts.map +1 -0
- package/lib/typescript/src/hooks/useParamsSynchronizable.d.ts +22 -0
- package/lib/typescript/src/hooks/useParamsSynchronizable.d.ts.map +1 -0
- package/lib/typescript/src/hooks/useWGPUSetup.d.ts +15 -0
- package/lib/typescript/src/hooks/useWGPUSetup.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +16 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/shaders/TRIANGLE_VERTEX_SHADER.d.ts +2 -0
- package/lib/typescript/src/shaders/TRIANGLE_VERTEX_SHADER.d.ts.map +1 -0
- package/lib/typescript/src/shaders/uniforms.d.ts +6 -0
- package/lib/typescript/src/shaders/uniforms.d.ts.map +1 -0
- package/lib/typescript/src/utils/backgroundRuntime.d.ts +3 -0
- package/lib/typescript/src/utils/backgroundRuntime.d.ts.map +1 -0
- package/lib/typescript/src/utils/colors.d.ts +22 -0
- package/lib/typescript/src/utils/colors.d.ts.map +1 -0
- package/lib/typescript/src/utils/initWebGPU.d.ts +23 -0
- package/lib/typescript/src/utils/initWebGPU.d.ts.map +1 -0
- package/package.json +175 -7
- package/src/components/Aurora.tsx +203 -0
- package/src/components/CalicoSwirl.tsx +167 -0
- package/src/components/Campfire.tsx +244 -0
- package/src/components/CircularGradient.tsx +76 -0
- package/src/components/Iridescence.tsx +67 -0
- package/src/components/LinearGradient.tsx +62 -0
- package/src/components/LiquidChrome.tsx +94 -0
- package/src/components/ShaderView/index.tsx +262 -0
- package/src/components/ShaderView/types.ts +36 -0
- package/src/components/ShaderViewWithPanGesture/index.tsx +225 -0
- package/src/components/Silk.tsx +102 -0
- package/src/consts.ts +152 -0
- package/src/hooks/useClock.ts +20 -0
- package/src/hooks/useParamsSynchronizable.ts +52 -0
- package/src/hooks/useWGPUSetup.tsx +73 -0
- package/src/index.tsx +32 -0
- package/src/shaders/TRIANGLE_VERTEX_SHADER.ts +17 -0
- package/src/shaders/uniforms.ts +18 -0
- package/src/utils/backgroundRuntime.ts +10 -0
- package/src/utils/colors.ts +117 -0
- package/src/utils/initWebGPU.ts +47 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { createWorkletRuntime, scheduleOnRuntime } from 'react-native-worklets';
|
|
2
|
+
|
|
3
|
+
export const BackgroundRuntime = createWorkletRuntime({
|
|
4
|
+
name: 'react-native-effects',
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
export function runOnBackground(callback: (...args: any[]) => void) {
|
|
8
|
+
'worklet';
|
|
9
|
+
return scheduleOnRuntime(BackgroundRuntime, callback);
|
|
10
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { NAMED_COLORS } from '../consts';
|
|
2
|
+
|
|
3
|
+
export type ColorInput = number | string;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* RGBA color values normalized to 0-1 range
|
|
7
|
+
*/
|
|
8
|
+
export type RGBA = {
|
|
9
|
+
r: number;
|
|
10
|
+
g: number;
|
|
11
|
+
b: number;
|
|
12
|
+
a: number;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Converts various color formats to RGBA object
|
|
17
|
+
* @param color - Can be:
|
|
18
|
+
* - Hex number (0xff0000)
|
|
19
|
+
* - Hex string ("#ff0000" or "ff0000")
|
|
20
|
+
* - RGB string ("rgb(255, 0, 0)")
|
|
21
|
+
* - RGBA string ("rgba(255, 0, 0, 0.5)")
|
|
22
|
+
* - Named color ("red", "blue", "purple", etc.)
|
|
23
|
+
* @returns RGBA object with normalized values (0-1), alpha defaults to 1.0 for non-rgba formats
|
|
24
|
+
*/
|
|
25
|
+
export function colorToVec4(color: ColorInput): RGBA {
|
|
26
|
+
'worklet';
|
|
27
|
+
// Handle hex number
|
|
28
|
+
if (typeof color === 'number') {
|
|
29
|
+
return {
|
|
30
|
+
r: ((color >> 16) & 0xff) / 255,
|
|
31
|
+
g: ((color >> 8) & 0xff) / 255,
|
|
32
|
+
b: (color & 0xff) / 255,
|
|
33
|
+
a: 1.0,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (typeof color === 'string') {
|
|
38
|
+
const trimmed = color.trim();
|
|
39
|
+
|
|
40
|
+
// Handle rgba() format
|
|
41
|
+
if (trimmed.startsWith('rgba(') && trimmed.endsWith(')')) {
|
|
42
|
+
const rgbaMatch = trimmed.match(
|
|
43
|
+
/rgba\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*([0-9.]+)\s*\)/
|
|
44
|
+
);
|
|
45
|
+
if (!rgbaMatch) {
|
|
46
|
+
throw new Error(
|
|
47
|
+
`[react-native-effects] Invalid RGBA format: ${color}. Expected rgba(r, g, b, a).`
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
r: Math.max(0, Math.min(255, parseInt(rgbaMatch[1]!, 10))) / 255,
|
|
52
|
+
g: Math.max(0, Math.min(255, parseInt(rgbaMatch[2]!, 10))) / 255,
|
|
53
|
+
b: Math.max(0, Math.min(255, parseInt(rgbaMatch[3]!, 10))) / 255,
|
|
54
|
+
a: Math.max(0, Math.min(1, parseFloat(rgbaMatch[4]!))),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Handle rgb() format
|
|
59
|
+
if (trimmed.startsWith('rgb(') && trimmed.endsWith(')')) {
|
|
60
|
+
const rgbMatch = trimmed.match(
|
|
61
|
+
/rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/
|
|
62
|
+
);
|
|
63
|
+
if (!rgbMatch) {
|
|
64
|
+
throw new Error(
|
|
65
|
+
`[react-native-effects] Invalid RGB format: ${color}. Expected rgb(r, g, b).`
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
r: Math.max(0, Math.min(255, parseInt(rgbMatch[1]!, 10))) / 255,
|
|
70
|
+
g: Math.max(0, Math.min(255, parseInt(rgbMatch[2]!, 10))) / 255,
|
|
71
|
+
b: Math.max(0, Math.min(255, parseInt(rgbMatch[3]!, 10))) / 255,
|
|
72
|
+
a: 1.0,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const lowerColor = trimmed.toLowerCase();
|
|
77
|
+
|
|
78
|
+
// Handle named colors
|
|
79
|
+
if (lowerColor in NAMED_COLORS) {
|
|
80
|
+
const hexValue = NAMED_COLORS[lowerColor];
|
|
81
|
+
if (hexValue !== undefined) {
|
|
82
|
+
return {
|
|
83
|
+
r: ((hexValue >> 16) & 0xff) / 255,
|
|
84
|
+
g: ((hexValue >> 8) & 0xff) / 255,
|
|
85
|
+
b: (hexValue & 0xff) / 255,
|
|
86
|
+
a: 1.0,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Handle hex string
|
|
92
|
+
const hex = trimmed.replace('#', '');
|
|
93
|
+
if (hex.length !== 6) {
|
|
94
|
+
throw new Error(
|
|
95
|
+
`[react-native-effects] Invalid hex color: ${color}. Must be 6 characters.`
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const num = parseInt(hex, 16);
|
|
100
|
+
if (isNaN(num)) {
|
|
101
|
+
throw new Error(
|
|
102
|
+
`[react-native-effects] Invalid hex color: ${color}. Must be valid hexadecimal.`
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
r: ((num >> 16) & 0xff) / 255,
|
|
108
|
+
g: ((num >> 8) & 0xff) / 255,
|
|
109
|
+
b: (num & 0xff) / 255,
|
|
110
|
+
a: 1.0,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
throw new Error(
|
|
115
|
+
`[react-native-effects] Invalid color format: ${color}. Expected hex number, hex string, rgb() string, or named color.`
|
|
116
|
+
);
|
|
117
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { scheduleOnRuntime, type WorkletRuntime } from 'react-native-worklets';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Installs WebGPU globals on the given worklet runtime.
|
|
5
|
+
*
|
|
6
|
+
* Worklet runtimes run on a separate JS context that lacks browser-like globals.
|
|
7
|
+
* This function captures WebGPU constants and `navigator.gpu` from the RN thread
|
|
8
|
+
* and injects them into the worklet's `globalThis` so that WebGPU code can run
|
|
9
|
+
* as if it were in a browser environment.
|
|
10
|
+
*
|
|
11
|
+
* @param runtime - The worklet runtime to initialize
|
|
12
|
+
*/
|
|
13
|
+
export function initWebGPU(runtime: WorkletRuntime) {
|
|
14
|
+
const navigator = globalThis.navigator as NavigatorGPU;
|
|
15
|
+
const GPUBufferUsage = globalThis.GPUBufferUsage;
|
|
16
|
+
const GPUColorWrite = globalThis.GPUColorWrite;
|
|
17
|
+
const GPUMapMode = globalThis.GPUMapMode;
|
|
18
|
+
const GPUShaderStage = globalThis.GPUShaderStage;
|
|
19
|
+
const GPUTextureUsage = globalThis.GPUTextureUsage;
|
|
20
|
+
|
|
21
|
+
scheduleOnRuntime(runtime, () => {
|
|
22
|
+
'worklet';
|
|
23
|
+
|
|
24
|
+
if (globalThis.self) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
globalThis.self = globalThis;
|
|
28
|
+
globalThis.navigator = { gpu: navigator.gpu } as unknown as Navigator;
|
|
29
|
+
globalThis.GPUBufferUsage = GPUBufferUsage;
|
|
30
|
+
globalThis.GPUColorWrite = GPUColorWrite;
|
|
31
|
+
globalThis.GPUMapMode = GPUMapMode;
|
|
32
|
+
globalThis.GPUShaderStage = GPUShaderStage;
|
|
33
|
+
globalThis.GPUTextureUsage = GPUTextureUsage;
|
|
34
|
+
globalThis.setImmediate =
|
|
35
|
+
globalThis.requestAnimationFrame as typeof setImmediate;
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
declare global {
|
|
40
|
+
var self: typeof globalThis;
|
|
41
|
+
var _WORKLET: boolean | undefined;
|
|
42
|
+
var performance: { now(): number };
|
|
43
|
+
|
|
44
|
+
interface Navigator {
|
|
45
|
+
gpu: GPU;
|
|
46
|
+
}
|
|
47
|
+
}
|