Repository navigation
fix(editor): reject non-finite zoom in setZoom - #1230
Conversation
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.
|
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, Looked at and left alone:
|
Fixes #1213
A zero-distance pinch (two fingers landing on the same pixel for a frame) makes
use-canvas-zoomdivide0 / 0, and the resulting NaN flows intosetZoom. The clamp there doesn't stop it, becauseMath.min(64, NaN)is NaN andMath.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
setZoomwhen 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 throughsetZoom, so guarding there keepszoomfinite 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.tswhenlastDistRef.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.tsnext to the existing clamp tests:setZoom(NaN)andsetZoom(Infinity)both leavezoomat 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). Locallypnpm lint,pnpm typecheck, andpnpm buildpass, and the editor-store unit suite is green.