Skip to content

fix: sync #dpr immediately after controller reinit on alphaEnabled toggle#23

Merged
jayf0x merged 3 commits into
mainfrom
fix/issue-16-alpha-cursor-offset
Jun 5, 2026
Merged

fix: sync #dpr immediately after controller reinit on alphaEnabled toggle#23
jayf0x merged 3 commits into
mainfrom
fix/issue-16-alpha-cursor-offset

Conversation

@jayf0x

@jayf0x jayf0x commented Jun 5, 2026

Copy link
Copy Markdown
Owner

Summary

  • Fixes cursor position offset when alphaEnabled (or webGPUEnabled) is toggled
  • After a toggle the init effect creates a new FluidController, but the ResizeObserver only fires when the container dimensions change — not when only alphaEnabled changes. This left simulation.#dpr = 1 (the default) so the pointer-to-UV coordinate transform was wrong until something else triggered a resize
  • Fix: call controller.resize(initW, initH) right after construction in useFluid.ts to push the correct DPR into the simulation from frame 0
  • Also null-guards FluidController#resize (!?) for the async WebGPU path where #sim is temporarily null while the adapter request is in flight

Test plan

  • All 92 existing tests pass (npm test -- --run)
  • Build succeeds (npm run build)
  • Load demo, toggle alphaEnabled off — cursor and splat should be co-located
  • Toggle alphaEnabled back on — still correct
  • Verify no regression on pixelRatio / simResolution quality changes

Closes #16

🤖 Generated with Claude Code

jayf0x and others added 3 commits June 5, 2026 10:51
…ggle

When alphaEnabled (or webGPUEnabled) changes, the init effect tears down the
old controller and creates a new one. The ResizeObserver only fires when the
container *size* changes, so it never re-fires for a toggle — leaving the
simulation with the default #dpr=1 until something else triggers a resize.

With wrong #dpr the pointer-to-UV coordinate transform is off:
  px = (cssX * 1) / (cssW * DPR * qualityDpr)   -- wrong
  px = (cssX * DPR * qualityDpr) / (cssW * DPR * qualityDpr)  -- correct

Fix: call controller.resize(initW, initH) immediately after construction so
the correct DPR is pushed into the simulation from frame 0, matching the
behaviour the user already sees when changing pixelRatio.

Also null-guard FluidController#resize for the async WebGPU path where #sim
is temporarily null while the adapter request is in flight.

Closes #16

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
A fresh FluidSimulation starts with #mouse.{x,y,targetX,targetY} = 0.
The first handleMove call computes dx = currentX - 0, producing a large
velocity vector that draws a fluid streak from the top-left corner to the
cursor on every controller reinit (alphaEnabled toggle, etc.).

Fix: skip the dx/dy calculation on the very first handleMove and just seed
the mouse position instead, so subsequent moves only measure real deltas.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@jayf0x jayf0x merged commit 55a7516 into main Jun 5, 2026
1 check passed
@jayf0x jayf0x deleted the fix/issue-16-alpha-cursor-offset branch June 5, 2026 09:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: cursor position offset when alphaEnabled is toggled

1 participant