Drag & Zoom
This example combines d3-drag and d3-zoom to let users pan and zoom a canvas of 2,000 points while also dragging individual points. Clicking and dragging on the background adjusts the view transform, whereas clicking and dragging on a point moves that point in world coordinates. The code uses `d3.zoomIdentity`, `transform.invertX`, and `transform.invertY` to convert between pointer and world spaces. It renders with the Canvas 2D API, using `context.translate` and `context.scale` to apply the current zoom transform.
AI-generated descriptionThis example shows how to combine d3-drag and d3-zoom to allow dragging of individual circles within a zoomable canvas. If you click and drag on the background, the view pans; if you click and drag on a circle, it moves.
The tricky part of this example is the need to distinguish between two coordinate spaces: the world coordinates used to position the circles, and the pointer coordinates representing the mouse or touches. The drag behavior doesn’t know the view is being transformed by the zoom behavior, so you must convert between the two coordinate spaces.
Compare this to the simpler SVG implementation.
forked from <a href='http://bl.ocks.org/mbostock/'>mbostock</a>'s block: <a href='http://bl.ocks.org/mbostock/2b534b091d80a8de39219dd076b316cd'>Drag & Zoom</a>