Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"scripts": {
"build": "vite build",
"dev": "vite build --watch",
"dev:demo": "bun run --cwd ./demo dev",
"test": "vitest",
"test:run": "vitest run",
"test:coverage": "vitest run --coverage",
Expand Down
8 changes: 8 additions & 0 deletions src/core/simulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export class FluidSimulation {

#config: FluidConfig;
#mouse: MouseState = { x: 0, y: 0, dx: 0, dy: 0, targetX: 0, targetY: 0, moved: false };
#mouseSeeded = false;

#source: Source | null = null;

Expand Down Expand Up @@ -219,6 +220,13 @@ export class FluidSimulation {
}

handleMove(x: number, y: number, strength = 1): void {
if (!this.#mouseSeeded) {
// Seed position on first call so the initial delta isn't measured from (0,0).
this.#mouse.x = this.#mouse.targetX = x;
this.#mouse.y = this.#mouse.targetY = y;
this.#mouseSeeded = true;
return;
}
this.#mouse.moved = true;
this.#mouse.dx = (x - this.#mouse.targetX) * strength;
this.#mouse.dy = (y - this.#mouse.targetY) * strength;
Expand Down
2 changes: 1 addition & 1 deletion src/fluid-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export class FluidController {
if (this.#worker) {
this.#worker.postMessage({ type: 'resize', width, height, dpr: effectiveDpr });
} else {
this.#sim!.resize(width, height, effectiveDpr);
this.#sim?.resize(width, height, effectiveDpr);
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/react/useFluid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ export function useFluid(
});
controllerRef.current = controller;

// Immediately sync dimensions so #dpr is correct from the first frame.
// The ResizeObserver only fires when the container *size* changes, so it
// won't re-fire on an alphaEnabled/webGPUEnabled toggle where the container
// hasn't moved. Without this call the simulation keeps #dpr=1 and all
// pointer-to-UV coordinate transforms are wrong until something else
// triggers a resize.
if (initW > 0 && initH > 0) controller.resize(initW, initH);

// Forward container resizes to the simulation — reads clampedDprRef so pixelRatio changes are picked up
const ro = new ResizeObserver((entries) => {
for (const entry of entries) {
Expand Down
Loading