All examples By author By category About

mbostock

Circle Dragging II

This example demonstrates applying d3.drag for dragging circles in Canvas. A custom drag subject performs hit-testing to determine which circle to drag, if any. The circle’s position is updated during the drag event. The circle is raised to the foreground at the start of a drag by reordering the data, and a temporary stroke is applied during drag for visual feedback.

The render function is also registered as a listener to update the display after any drag event. The display will thus be redrawn once per active pointer, but since the number of active pointers is typically small (1, maybe 2), the cost is negligible.

The hit-testing here is simple: the topmost circle that contains the active pointer is returned. A better technique is to choose the closest circle to the active pointer, allowing accurate selection even if the pointer is outside the circle.

Compare this to dragging SVG circles.