d3 / d3-zoom

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

Allow a reference point to be specified for interactive zooming #211

Open cpietsch opened 4 years ago

cpietsch commented 4 years ago

In d3 v3 there used to be a zoom.center() to adjust the target point while zooming via mousewheel / touch. Basically you could define a (center) target and when the user does a zoom input the zoom would steer in that direction. This was useful to avoid zooming into dead space of a scatter plot.

I made a workaround for this feature by modifying the zoom transform in the zoom callback and @Fil made a nice hack to override the mouse position

Probably the location to add this feature would be here somewhere here. Instead of using the mouse position we could use a reference point which needs to be declared before.

mbostock commented 4 years ago

Sounds reasonable. These are supported in the programmatic interface #178 #184, but not yet in the wheel and dblclick listeners.

https://github.com/d3/d3-zoom/blob/c609de484668ae318176da7011476154e8185d7e/src/zoom.js#L219 https://github.com/d3/d3-zoom/blob/c609de484668ae318176da7011476154e8185d7e/src/zoom.js#L284

Presumably we could allow a function or a constant, and have it default to d3.mouse (d3.pointer in d3-selection@2) for the current behavior.

cpietsch commented 4 years ago

It could be also used for the touch input. Suggesting zoom.reference(point/function) that replaces the centroid function for non interactive zoom and mouse for interactive zoom ? Or just have one interface for the interactive reference.

Fil commented 4 years ago

More remarks after a first (failed) attempt:

scatterplot example:

complete example now at https://observablehq.com/d/9035ed7049aaa8c6

target example:

(issue visible at https://bl.ocks.org/mbostock/6226534, which is d3v3)

notebook example with d3@6 (alpha) at https://observablehq.com/d/48ff4a64c477cc79

cpietsch commented 4 years ago

Wow, nice work! Looks like the issue is quite interwoven. The scatterplot example works great. One suggestion would be to use event.deltaY < 0 for the wheel-in detection. The sequence on d3v3 is working fine with my Firefox, but on the d3@6 notebook it swallow some translations (only if I move very fast).

filipinascimento commented 1 year ago

Hey any updates on that? I think Fil's PR does not work anymore with the latest d3. Having the center functionality would be great for data snap on zoom and for some pseudo-3D interactions.

ptgamr commented 1 year ago

I am in desparate need for this center() function

Fil commented 1 year ago

I've refreshed #212 (there was a small conflict on README.md), and upgraded the test notebooks.