block by mgold f61420a6f02adb618a70

Zoom Buttons III

Full Screen

This example shows how to implement zoom in and zoom out buttons on a map using D3 and SVG transforms. Once the target scale and translation are computed, the map transitions over 100ms. Then it checks to see if the button is still held down, and if so continues zooming. Thus, zooming may continue for almost 100ms after the button is released; consider this “scroll with inertia”. (It’s not a bug, it’s a feature!) Control logic is in place to make the buttons feel natural and avoid snapping the map to a new translation instantly.

This is a fork Mike Bostock’s Map Pan & Zoom I; you can diff the two to see my changes. The scaling math (which is quite tricky!) is derived from Wil Linssen.

If zooming by a hard-coded factor would push the scale beyond the scale extent, it is clipped. Importantly, this clipping also affects the translation. If you scale by 1.047 but translate as if you had scaled by 1.2, bad things happen.

See also

index.html