Skip to content

fix(editor): reject non-finite zoom in setZoom - #1230

Merged
snapotter-hq merged 2 commits into
snapotter-hq:mainfrom
drakeo338:fix/issue-1213
Sep 25, 2026
Merged

snapotter-hq merged 2 commits into
snapotter-hq:mainfrom
drakeo338:fix/issue-1213

Conversation

@drakeo338

Copy link
Copy Markdown
Contributor

Fixes #1213

A zero-distance pinch (two fingers landing on the same pixel for a frame) makes use-canvas-zoom divide 0 / 0, and the resulting NaN flows into setZoom. The clamp there doesn't stop it, because Math.min(64, NaN) is NaN and Math.max(0.01, NaN) is NaN too, so NaN ends up in the store. From there it's invisible: comparisons against NaN are always false and any arithmetic on it stays NaN, so consumers like the first-vertex close test and the close-target radius just quietly stop working with nothing in the console to trace it back to.

I went with the store guard the issue sketches: bail out of setZoom when the value isn't finite, keeping the existing clamp for the normal case. I liked this over patching the pinch handler because every producer funnels through setZoom, so guarding there keeps zoom finite no matter where a bad value comes from, and it's the smallest change that makes the state itself trustworthy. My one hesitation was whether silently dropping the frame is the right call versus clamping to a bound, but there's no sensible finite value to map NaN or Infinity onto, and holding the last good zoom is what a user would expect from a junk frame mid-pinch, so returning early is what I settled on.

I deliberately kept this to the store. The issue also mentions optionally skipping the pinch frame in use-canvas-zoom.ts when lastDistRef.current === 0, which is the actual root cause and worth doing, but it's a separate change to a different file, and the store guard already makes the stored zoom trustworthy for every consumer that reads it. The on-canvas stage transform is still set straight from the producer for that one bad frame and then self-heals on the next, which is exactly what the producer follow-up would smooth out. I'd rather send that as its own small follow-up than widen this PR; happy to open it if you'd like.

I added two cases to tests/unit/web/editor-store.test.ts next to the existing clamp tests: setZoom(NaN) and setZoom(Infinity) both leave zoom at its previous valid value. I checked they discriminate by reverting the guard and rerunning them, and both fail without it (the NaN case on the finite check, the Infinity case because the old clamp stored 64). Locally pnpm lint, pnpm typecheck, and pnpm build pass, and the editor-store unit suite is green.

A zero-distance pinch makes use-canvas-zoom compute 0 / 0, so setZoom
receives NaN. The existing clamp does not catch it: Math.min(64, NaN) is
NaN and Math.max(0.01, NaN) is NaN, so NaN is stored. Consumers that read
zoom then fail silently -- comparisons against NaN are always false and
arithmetic yields NaN -- with no error to trace it back to.

Guard the store boundary so a non-finite value is dropped and the last
good zoom is kept, which keeps zoom finite regardless of the producer.
The store guard stops NaN reaching zoom, but handleTouchMove still
applied the bad frame everywhere else. With both fingers on one pixel,
0 / 0 wrote NaN into panOffset and the Konva stage, and every later
frame read that NaN pan back, so the canvas stayed blank until a fit or
reset. A zero distance on only one side of the ratio snapped zoom to
64x or 0.01x instead.

Skip the frame when either distance is zero and let the refs advance,
so the next frame resumes from a real distance.
@snapotter-hq
snapotter-hq merged commit e86f2ae into snapotter-hq:main Sep 25, 2026
19 checks passed
@snapotter-hq

Copy link
Copy Markdown
Owner

Merged. Thanks @drakeo338, the store guard went in as you wrote it.

Review turned up one thing the guard couldn't reach, so I pushed 98d07dd onto your branch. On a 0/0 frame, handleTouchMove also computes newPos from NaN, so setPanOffset stored {NaN, NaN} and the stage got a NaN scale and position. The next frame reads that pan back, so it never recovered on its own: the canvas stayed blank until Fit or a reset. Coincident fingers spreading apart (n/0) also snapped zoom to 64x through the hook's own clamp, and 0/n snapped it to 0.01x. The hook now skips any frame where either distance is 0, which is the producer follow-up you offered. tests/unit/web/use-canvas-zoom-pinch.test.ts covers all three plus resuming on the next frame. Four of its five cases failed before the change.

Looked at and left alone:

  • setZoom drops non-finite input without logging. With the producer fixed it's a backstop, and we found nothing else that feeds it NaN.
  • setZoom(Infinity) now keeps the old zoom instead of clamping to 64. Your test pins that, and it's the better behaviour. The only visible effect is that typing 1e999 into the status bar zoom box does nothing.
  • No -Infinity case, since it takes the same branch as Infinity.
  • Fit to screen divides by canvasSize, so a zero-width canvas would produce a NaN pan. DEFAULT_CANVAS_SIZE isn't zero and I couldn't find a path that makes it zero, so there's no issue for it.

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.

setZoom stores NaN from a zero-distance pinch, which silently breaks zoom consumers

2 participants