d3 / d3-zoom

Pan and zoom SVG, HTML or Canvas using mouse or touch input.
https://d3js.org/d3-zoom
ISC License
502 stars 143 forks source link

dblclick zooms results in NaN mouse values #202

Closed Fil closed 3 years ago

Fil commented 4 years ago

In https://observablehq.com/d/7a912fba32fbe641 we've noticed that, when we dblclick to zoom (or shift-dblclick to zoom out), the value of d3.mouse() is [NaN,NaN], which resulted in spurious rotations.

I've fixed it by relying on the previously recorded mouse position (the one from the first click), but it seems wrong that d3.mouse returns NaNs.

(I'm not sure if this belongs to d3-zoom or d3-selection)

mbostock commented 4 years ago

I think this is the expected behavior, unfortunately. The problem is that when you dblclick, the zoom behavior schedules a transition to start on the next animation frame. By the time the transition starts, and the zoom start event is dispatched, the dblclick event has expired and so the sourceEvent on the zoom start event is null causing d3.mouse to return [NaN, NaN].

If you want to get the mouse coordinates of the dblclick, you need to listen to the dblclick, not just the start event on the zoom behavior.

Alternatively, the zoom behavior would somehow need to set the sourceEvent of the zoom start event to the expired dblclick event.

Fil commented 4 years ago

With https://github.com/d3/d3-zoom/pull/205/commits/b56cc7e9373dff8454dbecfce5d8d30c43d58b81 the dblclick handler receives a sourceEvent — I've updated the test notebook.