Principle:777genius Agent teams ai Graph Interaction
| Knowledge Sources | |
|---|---|
| Domains | Visualization, Interaction |
| Last Updated | 2026-10-22 00:00 GMT |
Overview
Pattern for handling mouse interactions (hover, drag, click, double-click) on a canvas graph with hit detection and drag thresholding.
Description
Graph Interaction is the process of translating raw mouse events on a canvas into graph-level actions (hover, drag, click) using hit detection. The interaction hook maintains refs for hovered node ID, dragged node ID, and drag state, exposing event handlers for mouse down, move, up, and double-click.
Key aspects:
- Hit detection: `findNodeAt()` performs point-in-shape testing against all graph nodes to identify which node the mouse is over.
- Drag threshold: A minimum drag distance (squared) must be exceeded before a mouse-down is treated as a drag rather than a click, preventing accidental drags.
- Member-only dragging: In stable-slot layout, the lead is fixed at center; only member nodes can be dragged between slots.
- Drag gate: An optional `canDragNode` callback gates whether a specific node can be dragged.
- Zoom-aware: Hit detection and drag threshold account for the current zoom level.
Usage
Use this principle when implementing interactive canvas-based graph manipulation, especially when nodes can be dragged between layout slots and click vs. drag must be disambiguated.
Theoretical Basis
# Abstract algorithm (NOT real implementation)
def handle_mouse_down(wx, wy, nodes, zoom):
mouse_down_pos = (wx, wy)
hit = find_node_at(wx, wy, nodes, zoom)
clicked_node = hit
if hit and hit.kind == 'member' and can_drag(hit):
drag_node = hit
def handle_mouse_move(wx, wy, nodes, zoom):
if mouse_down_pos and drag_node:
dx = wx - mouse_down_pos.x
dy = wy - mouse_down_pos.y
if dx*dx + dy*dy > DRAG_THRESHOLD**2:
is_dragging = True
if is_dragging:
on_drag_node(drag_node.id, wx, wy)
else:
hovered_node = find_node_at(wx, wy, nodes, zoom)