Skip to main content
100%

d3v4 - zoom and drag circles - drag adjusted for zoom

✓ Published0🌍 Public
TThanaporn-sk
Last edited Apr 20, 2017
Created on Apr 7, 2017

This example demonstrates how to zoom and drag circles in d3 v4 while ensuring the drag positions correctly match the mouse location after zooming. A drag handler, built with `d3.drag`, adjusts the circle’s new coordinates by dividing the mouse movement by the current zoom scale, retrieved from the element’s transform attribute. A separate `d3.zoom` handler applies the transform to all SVG circles. The visualization renders 50 randomly positioned yellow circles on a blue rectangle using SVG elements, with data generated via `d3.range` and `Math.random`.

AI-generated description

Example of zoom and drag in d3 v4.

This example has a modified drag function so that when you zoom in, you're still able to drag the circles to the correct mouse location. Here's an example of the drag problem.

forked from <a href='http://bl.ocks.org/puzzler10/'>puzzler10</a>'s block: <a href='http://bl.ocks.org/puzzler10/49f13307e818ea9a909ba5adba5b6ed9'>d3v4 - zoom and drag circles - drag adjusted for zoom </a>

mit Licensed

Similar vizzes