Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:777genius Agent teams ai Drag Threshold

From Leeroopedia
Knowledge Sources
Domains UI_Interaction, Optimization
Last Updated 2026-10-22 00:00 GMT

Overview

Require a minimum mouse movement (5px) before treating a mousedown as a drag, distinguishing clicks from drags on the agent graph canvas.

Description

The agent graph interaction hook tracks the mousedown position and only activates drag mode when the mouse moves more than `dragThresholdPx` (5 pixels) from the initial position. This prevents accidental drags when the user intends to click (e.g., selecting a node). Below the threshold, mouseup is treated as a click. Above the threshold, the node follows the cursor. The threshold is measured in world coordinates using squared distance (`dx*dx + dy*dy > threshold*threshold`) to avoid a square root call on every mousemove.

Usage

Use this heuristic for any canvas-based UI where both click and drag interactions are available on the same element. This applies to the agent graph node interaction — clicking selects/opens a node, dragging repositions it.

The Insight (Rule of Thumb)

  • Action: On `mousedown`, record position. On `mousemove`, compare squared distance to threshold. Only set `isDragging = true` if distance exceeds threshold.
  • Value: `dragThresholdPx: 5` (pixels in world space). Uses squared comparison: `dx*dx + dy*dy > 25`.
  • Trade-off: Very small movements (< 5px) are treated as clicks, not drags. This is intentional — pixel-perfect dragging is less useful than reliable click detection.
  • Compatibility: Works with any pointer-based input. Threshold is in world coordinates, so it scales with zoom.

Reasoning

Without a drag threshold, any mouse movement during a click would initiate a drag, making it nearly impossible to click nodes (especially small ones). The 5px threshold provides a dead zone where minor mouse jitter doesn't trigger a drag. Using squared distance avoids a `Math.sqrt()` call on every mousemove event, which matters for 60fps interaction.

Code Evidence

Threshold constant from `canvas-constants.ts:155`:

export const ANIM = {
  // ...
  dragThresholdPx: 5,
  // ...
} as const;

Threshold check from `useGraphInteraction.ts:54-61`:

const handleMouseMove = useCallback(
  (wx: number, wy: number, nodes: GraphNode[], zoom = 1) => {
    // Check drag threshold
    if (mouseDownPos.current && dragNodeId.current) {
      const dx = wx - mouseDownPos.current.x;
      const dy = wy - mouseDownPos.current.y;
      if (dx * dx + dy * dy > ANIM.dragThresholdPx * ANIM.dragThresholdPx) {
        isDragging.current = true;
      }
    }

Click vs drag resolution from `useGraphInteraction.ts:76-80`:

const handleMouseUp = useCallback((): string | null => {
  const wasDragging = isDragging.current;
  const nodeId = clickedNodeId.current;

  isDragging.current = false;

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment