Skip to main content
100%

Click to center & zoom

✓ Published0🌍 Public
ZZoeLeBlanc
Last edited Mar 14, 2018
Created on Mar 14, 2018

Ten colored rectangles are randomly arranged on a blank SVG canvas. Clicking any rectangle smoothly animates the view to center and zoom in on that rectangle, using D3 v4's zoom behavior and a custom transform computed from the rectangle's bounding box. The zoom transition is powered by `d3.zoom.transform`, with `d3.zoomIdentity` translated and scaled to fit the clicked shape within a margin, demonstrating programmatic zoom control with animated SVG transitions.

AI-generated description

This example uses the zoom behaviour of D3.js (version 4) for enabling "click-to-zoom" interactions on SVG objects such as rectangles, circles and so on.

The D3 zoom.transform function has been used in order to create an animated transition every time an object is clicked.

In order to translate and scale the visualization, it is necessary to use the d3.zoomTransform function. In order to translate and scale the visualization, since the x, y and k properties are read-only, d3.zoomIdentity must be translated and scaled without accessing them directly.

By refreshing the visualization, random rectangles are generated.

forked from <a href='http://bl.ocks.org/fabiovalse/'>fabiovalse</a>'s block: <a href='http://bl.ocks.org/fabiovalse/b9224bfd64ca96c47f8cdcb57b35b8e2'>Click to center & zoom</a>

forked from <a href='http://bl.ocks.org/christianbriggs/'>christianbriggs</a>'s block: <a href='http://bl.ocks.org/christianbriggs/a2a3821d1ada49c07b036c0f2f6a0248'>Click to center & zoom</a>

forked from <a href='http://bl.ocks.org/smithant/'>smithant</a>'s block: <a href='http://bl.ocks.org/smithant/664d6cf86e53442d09687b154a9a411d'>Click to center & zoom</a>

mit Licensed

Similar vizzes