Circle Dragging III
This example demonstrates an improved technique for dragging circles rendered on a Canvas, using a custom drag subject that selects the circle closest to the pointer rather than simply the topmost overlapping circle. The code defines a linear scan in `dragsubject` to find the nearest circle within double the radius, increasing the effective hit area for easier interaction. Twenty randomly positioned circles are drawn and updated via d3-drag’s event handlers, with active circles highlighted by a stroke. The implementation relies on d3.drag, d3.scaleOrdinal with d3.schemeCategory20, and the Canvas 2D API.
AI-generated descriptionThis is a variant of dragging Canvas circles that uses better hit-testing. Rather than choose the top circle that contains the active pointer, this custom drag subject chooses the circle closest to the active pointer. Even better, this allows increasing the search radius for the closest circle, such that the pointer doesn’t need to be inside a circle. This is equivalent to using an SVG Voronoi overlay, and is similar to Tovi Grossman’s bubble cursor.
This implementation uses a linear scan. For force-directed layouts, see simulation.find. See also quadtree.find, though note that a quadtree will only be faster than a linear scan if you can amortize the cost of building the quadtree, such as by using quadtree.remove and quadtree.add to update the circle that is being dragged.