@jayf0x/fluidity-js 0.1.3 → 0.1.4
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/README.md +104 -119
- package/dist/globals.d.ts +63 -0
- package/dist/index.d.ts +65 -0
- package/dist/index.js +1012 -0
- package/package.json +7 -7
- package/dist/fluidity-js.js +0 -956
- package/types/index.d.ts +0 -290
package/types/index.d.ts
DELETED
|
@@ -1,290 +0,0 @@
|
|
|
1
|
-
import type { CSSProperties, ForwardRefExoticComponent, RefAttributes } from 'react';
|
|
2
|
-
|
|
3
|
-
// ---------------------------------------------------------------------------
|
|
4
|
-
// Algorithm
|
|
5
|
-
// ---------------------------------------------------------------------------
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Named rendering algorithm for the display shader.
|
|
9
|
-
*
|
|
10
|
-
* - `standard` — fluid colour overlay + gentle refraction (default)
|
|
11
|
-
* - `glass` — strong UV distortion only, no colour, bent-glass look
|
|
12
|
-
* - `ink` — dense opaque pigment that accumulates and stains
|
|
13
|
-
* - `aurora` — velocity-field UV warp, liquid-metal / lava-lamp feel
|
|
14
|
-
* - `ripple` — exaggerated normals + Fresnel rim, still-water surface
|
|
15
|
-
*/
|
|
16
|
-
export type FluidAlgorithm = 'standard' | 'glass' | 'ink' | 'aurora' | 'ripple';
|
|
17
|
-
|
|
18
|
-
// ---------------------------------------------------------------------------
|
|
19
|
-
// Configuration
|
|
20
|
-
// ---------------------------------------------------------------------------
|
|
21
|
-
|
|
22
|
-
/** Full simulation configuration. All values have sensible defaults. */
|
|
23
|
-
export interface FluidConfig {
|
|
24
|
-
/** How long density (ink) lingers before fading. Range: 0–1. Default: 0.992 */
|
|
25
|
-
densityDissipation: number;
|
|
26
|
-
/** Viscosity — how quickly velocity decays. Range: 0–1. Default: 0.93 */
|
|
27
|
-
velocityDissipation: number;
|
|
28
|
-
/** Jacobi iterations for the pressure solve. Higher = more accurate but slower. Default: 25 */
|
|
29
|
-
pressureIterations: number;
|
|
30
|
-
/** Vorticity confinement — swirl factor. Higher creates eddies. Default: 0.0001 */
|
|
31
|
-
curl: number;
|
|
32
|
-
/** Brush radius. Default: 0.004 */
|
|
33
|
-
splatRadius: number;
|
|
34
|
-
/** Force with which the brush pushes the liquid. Default: 0.91 */
|
|
35
|
-
splatForce: number;
|
|
36
|
-
/** Optical warping of the background through the fluid. Default: 0.25 */
|
|
37
|
-
refraction: number;
|
|
38
|
-
/** Specular highlight exponent (lower = broader "paint" look). Default: 1.01 */
|
|
39
|
-
specularExp: number;
|
|
40
|
-
/** Highlight intensity. Default: 0.01 */
|
|
41
|
-
shine: number;
|
|
42
|
-
/** Base fluid colour [R, G, B] in 0–1 range. Default: [0, 0, 0] */
|
|
43
|
-
waterColor: [number, number, number];
|
|
44
|
-
/** Glossy reflection / glow colour [R, G, B]. Default: [0.7, 0.85, 1.0] */
|
|
45
|
-
glowColor: [number, number, number];
|
|
46
|
-
/**
|
|
47
|
-
* Display rendering algorithm. Each algorithm has a distinct visual character.
|
|
48
|
-
* Default: 'standard'
|
|
49
|
-
*/
|
|
50
|
-
algorithm: FluidAlgorithm;
|
|
51
|
-
/**
|
|
52
|
-
* UV warp intensity for the `aurora` algorithm.
|
|
53
|
-
* Controls how far velocity displaces background pixels. Default: 0.015
|
|
54
|
-
*/
|
|
55
|
-
warpStrength: number;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// ---------------------------------------------------------------------------
|
|
59
|
-
// Presets
|
|
60
|
-
// ---------------------------------------------------------------------------
|
|
61
|
-
|
|
62
|
-
export type PresetKey = 'calm' | 'storm' | 'wave' | 'neon' | 'smoke';
|
|
63
|
-
|
|
64
|
-
/** Named preset configurations bundled with the library. */
|
|
65
|
-
export const PRESETS: Record<PresetKey, Partial<FluidConfig>>;
|
|
66
|
-
|
|
67
|
-
// ---------------------------------------------------------------------------
|
|
68
|
-
// Imperative handle (ref)
|
|
69
|
-
// ---------------------------------------------------------------------------
|
|
70
|
-
|
|
71
|
-
/** Methods exposed by FluidText and FluidImage via a forwarded ref. */
|
|
72
|
-
export interface FluidHandle extends Element {
|
|
73
|
-
/**
|
|
74
|
-
* Fully re-initialises the simulation and reloads the source.
|
|
75
|
-
* Use when you want a clean restart (e.g. after changing the src prop externally).
|
|
76
|
-
*/
|
|
77
|
-
reset(): void;
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Programmatic pointer input — e.g. from a custom mousemove handler.
|
|
81
|
-
* Coordinates are relative to the canvas element.
|
|
82
|
-
*/
|
|
83
|
-
updateLocation(opts: { x: number; y: number; strength?: number }): void;
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Immediately injects one fluid splat at pixel position (x, y) with an
|
|
87
|
-
* explicit velocity vector (vx, vy) in canvas-pixel units.
|
|
88
|
-
* Safe to call multiple times per frame — each call writes directly to the
|
|
89
|
-
* velocity and density FBOs without going through the mouse-state machine.
|
|
90
|
-
*
|
|
91
|
-
* Ideal for programmatic paths (particle systems, Lorenz attractors, etc.)
|
|
92
|
-
* where you need N independent injection points per frame.
|
|
93
|
-
*
|
|
94
|
-
* @param x Canvas-relative pixel X
|
|
95
|
-
* @param y Canvas-relative pixel Y
|
|
96
|
-
* @param vx Horizontal velocity (positive = right)
|
|
97
|
-
* @param vy Vertical velocity (positive = down)
|
|
98
|
-
* @param strength Force multiplier. Default: 1
|
|
99
|
-
*/
|
|
100
|
-
splat(x: number, y: number, vx: number, vy: number, strength?: number): void;
|
|
101
|
-
|
|
102
|
-
/** Merges a partial config update into the running simulation. */
|
|
103
|
-
updateConfig(config: Partial<FluidConfig>): void;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
// ---------------------------------------------------------------------------
|
|
107
|
-
// Shared props
|
|
108
|
-
// ---------------------------------------------------------------------------
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Boolean props follow the `is`/`has`/`can` naming convention.
|
|
112
|
-
*/
|
|
113
|
-
interface FluidBaseProps {
|
|
114
|
-
/** Additional CSS class applied to the container element. */
|
|
115
|
-
className?: string;
|
|
116
|
-
/** Inline styles merged with the default `display:block; width:100%; height:100%`. */
|
|
117
|
-
style?: CSSProperties;
|
|
118
|
-
/** Simulation configuration overrides. */
|
|
119
|
-
config?: Partial<FluidConfig>;
|
|
120
|
-
/**
|
|
121
|
-
* When `true` (default), mouse/touch events on the canvas are wired up automatically.
|
|
122
|
-
* Set to `false` and use `ref.updateLocation()` for custom event handling.
|
|
123
|
-
*/
|
|
124
|
-
isMouseEnabled?: boolean;
|
|
125
|
-
/**
|
|
126
|
-
* When `true` (default), the simulation runs in a Web Worker via OffscreenCanvas.
|
|
127
|
-
* Set to `false` for main-thread mode (required when using multiple instances).
|
|
128
|
-
*/
|
|
129
|
-
isWorkerEnabled?: boolean;
|
|
130
|
-
/**
|
|
131
|
-
* Apply a named preset as the base configuration.
|
|
132
|
-
* Any `config` values you provide override the preset.
|
|
133
|
-
*/
|
|
134
|
-
preset?: PresetKey;
|
|
135
|
-
/**
|
|
136
|
-
* Rendering algorithm that controls the visual character of the fluid effect.
|
|
137
|
-
* Default: 'standard'
|
|
138
|
-
*/
|
|
139
|
-
algorithm?: FluidAlgorithm;
|
|
140
|
-
/**
|
|
141
|
-
* CSS background colour applied to the container div behind the WebGL canvas.
|
|
142
|
-
* Visible through transparent canvas areas (e.g. empty space in FluidText,
|
|
143
|
-
* or around a partial-coverage image). Default: '#0a0a0a'
|
|
144
|
-
*/
|
|
145
|
-
backgroundColor?: string;
|
|
146
|
-
/**
|
|
147
|
-
* URL or local path to an image composited as the canvas background,
|
|
148
|
-
* behind the text or main image. Accepts any fetchable URL or relative path.
|
|
149
|
-
*/
|
|
150
|
-
backgroundSrc?: string;
|
|
151
|
-
/**
|
|
152
|
-
* Sizing mode for `backgroundSrc`. Accepts: 'cover' | 'contain' | '50%' | '200px' | number.
|
|
153
|
-
* Default: 'cover'
|
|
154
|
-
*/
|
|
155
|
-
backgroundSize?: string | number;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
// ---------------------------------------------------------------------------
|
|
159
|
-
// FluidText
|
|
160
|
-
// ---------------------------------------------------------------------------
|
|
161
|
-
|
|
162
|
-
export interface FluidTextProps extends FluidBaseProps {
|
|
163
|
-
/** The string to render. Changes trigger a texture rebuild. */
|
|
164
|
-
text: string;
|
|
165
|
-
/** Font size in pixels. Default: 100 */
|
|
166
|
-
fontSize?: number;
|
|
167
|
-
/** CSS colour string for the text. Default: '#ffffff' */
|
|
168
|
-
color?: string;
|
|
169
|
-
/** Font family. Default: 'sans-serif' */
|
|
170
|
-
fontFamily?: string;
|
|
171
|
-
/** Font weight. Default: 900 */
|
|
172
|
-
fontWeight?: string | number;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
export const FluidText: ForwardRefExoticComponent<FluidTextProps & RefAttributes<FluidHandle>>;
|
|
176
|
-
|
|
177
|
-
// ---------------------------------------------------------------------------
|
|
178
|
-
// FluidImage
|
|
179
|
-
// ---------------------------------------------------------------------------
|
|
180
|
-
|
|
181
|
-
export interface FluidImageProps extends FluidBaseProps {
|
|
182
|
-
/** URL of the image to display. Changing the URL reloads and re-initialises. */
|
|
183
|
-
src: string;
|
|
184
|
-
/**
|
|
185
|
-
* Obstacle brightness (0–1) controlling how strongly the image edges
|
|
186
|
-
* repel the fluid. Lower = weaker boundary, higher = stronger.
|
|
187
|
-
* Default: 0.0
|
|
188
|
-
*/
|
|
189
|
-
effect?: number;
|
|
190
|
-
/**
|
|
191
|
-
* How the image is sized within the canvas.
|
|
192
|
-
* Accepts: 'cover' | 'contain' | '50%' | '200px' | number (scale factor).
|
|
193
|
-
* Default: 'cover'
|
|
194
|
-
*/
|
|
195
|
-
imageSize?: string | number;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
export const FluidImage: ForwardRefExoticComponent<FluidImageProps & RefAttributes<FluidHandle>>;
|
|
199
|
-
|
|
200
|
-
// ---------------------------------------------------------------------------
|
|
201
|
-
// Hooks
|
|
202
|
-
// ---------------------------------------------------------------------------
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Low-level hook that creates a FluidController inside a container element.
|
|
206
|
-
*
|
|
207
|
-
* @example
|
|
208
|
-
* ```tsx
|
|
209
|
-
* const containerRef = useRef<HTMLDivElement>(null);
|
|
210
|
-
* const controllerRef = useFluid(containerRef, { isWorkerEnabled: false });
|
|
211
|
-
*
|
|
212
|
-
* useEffect(() => {
|
|
213
|
-
* controllerRef.current?.setTextSource({ text: 'Hello', fontSize: 80, color: '#fff' });
|
|
214
|
-
* }, []);
|
|
215
|
-
*
|
|
216
|
-
* return <div ref={containerRef} style={{ width: 400, height: 200 }} />;
|
|
217
|
-
* ```
|
|
218
|
-
*/
|
|
219
|
-
export function useFluid(
|
|
220
|
-
containerRef: React.RefObject<HTMLElement>,
|
|
221
|
-
opts?: { isWorkerEnabled?: boolean; config?: Partial<FluidConfig> }
|
|
222
|
-
): React.RefObject<FluidController | null>;
|
|
223
|
-
|
|
224
|
-
// ---------------------------------------------------------------------------
|
|
225
|
-
// Core classes (framework-agnostic)
|
|
226
|
-
// ---------------------------------------------------------------------------
|
|
227
|
-
|
|
228
|
-
export interface TextSourceOpts {
|
|
229
|
-
text: string;
|
|
230
|
-
fontSize: number;
|
|
231
|
-
color: string;
|
|
232
|
-
fontFamily?: string;
|
|
233
|
-
fontWeight?: string | number;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/** Low-level WebGL fluid simulation. Accepts HTMLCanvasElement or OffscreenCanvas. */
|
|
237
|
-
export class FluidSimulation {
|
|
238
|
-
constructor(canvas: HTMLCanvasElement | OffscreenCanvas, config?: Partial<FluidConfig>);
|
|
239
|
-
|
|
240
|
-
setTextSource(opts: TextSourceOpts): void;
|
|
241
|
-
setImageSource(src: string, effect?: number, size?: string | number): Promise<void>;
|
|
242
|
-
setImageBitmap(bitmap: ImageBitmap, effect?: number, size?: string | number): void;
|
|
243
|
-
setBackground(bitmap: ImageBitmap | null, size?: string | number): void;
|
|
244
|
-
|
|
245
|
-
handleMove(x: number, y: number, strength?: number): void;
|
|
246
|
-
splat(x: number, y: number, vx: number, vy: number, strength?: number): void;
|
|
247
|
-
|
|
248
|
-
resize(width?: number, height?: number): void;
|
|
249
|
-
updateConfig(config: Partial<FluidConfig>): void;
|
|
250
|
-
|
|
251
|
-
start(): void;
|
|
252
|
-
stop(): void;
|
|
253
|
-
destroy(): void;
|
|
254
|
-
|
|
255
|
-
readonly isRunning: boolean;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
/** Unified controller abstracting worker vs main-thread execution. */
|
|
259
|
-
export class FluidController {
|
|
260
|
-
constructor(canvas: HTMLCanvasElement, opts?: { isWorkerEnabled?: boolean; config?: Partial<FluidConfig> });
|
|
261
|
-
|
|
262
|
-
setTextSource(opts: TextSourceOpts): void;
|
|
263
|
-
setImageSource(src: string, effect?: number, size?: string | number): void;
|
|
264
|
-
setBackground(bitmap: ImageBitmap | null, size?: string | number): void;
|
|
265
|
-
|
|
266
|
-
handleMove(x: number, y: number, strength?: number): void;
|
|
267
|
-
splat(x: number, y: number, vx: number, vy: number, strength?: number): void;
|
|
268
|
-
resize(width: number, height: number): void;
|
|
269
|
-
updateConfig(config: Partial<FluidConfig>): void;
|
|
270
|
-
destroy(): void;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
// ---------------------------------------------------------------------------
|
|
274
|
-
// Config utilities
|
|
275
|
-
// ---------------------------------------------------------------------------
|
|
276
|
-
|
|
277
|
-
export const DEFAULT_CONFIG: FluidConfig;
|
|
278
|
-
|
|
279
|
-
/** Merges user config with defaults, optionally layering a named preset. */
|
|
280
|
-
export function mergeConfig(user?: Partial<FluidConfig>, preset?: PresetKey): FluidConfig;
|
|
281
|
-
|
|
282
|
-
// ---------------------------------------------------------------------------
|
|
283
|
-
// Utilities
|
|
284
|
-
// ---------------------------------------------------------------------------
|
|
285
|
-
|
|
286
|
-
/**
|
|
287
|
-
* Fetches a URL and returns an ImageBitmap.
|
|
288
|
-
* Works on both main thread and in workers.
|
|
289
|
-
*/
|
|
290
|
-
export function loadImageBitmap(src: string): Promise<ImageBitmap>;
|