Ejemplos
Ejemplos prácticos. Demos en vivo.
Elige un ejemplo para abrirlo a pantalla completa, con la demo en vivo y su código fuente lado a lado.
Elige un ejemplo para abrirlo a pantalla completa, con la demo en vivo y su código fuente lado a lado.
Los nodos se repelen, los enlaces los atraen y la simulación se va asentando poco a poco. Arrastra un nodo, o haz clic en uno para colgarle un nodo hijo.
import 'nuclo' type Node = { x: number; y: number; vx: number; vy: number; depth: number } const W = 320 const H = 220 const COLORS = ['#ff3f00', '#ff8a00', '#2563eb'] let nodes: Node[] = [] let links: [Node, Node][] = [] let heat = 1 // scales every force and cools each frame let held: Node | null = null let press = [0, 0] let moved = false const clamp = (v: number, min: number, max: number) => Math.max(min, Math.min(max, v)) const rand = (min: number, max: number) => Math.round(min + Math.random() * (max - min)) function grow(parent?: Node) { const a = nodes.length * 2.4 const node = { x: (parent?.x ?? W / 2) + Math.cos(a) * 24, y: (parent?.y ?? H / 2) + Math.sin(a) * 24, vx: 0, vy: 0, depth: parent ? parent.depth + 1 : 0, } nodes.push(node) if (parent) links.push([parent, node]) heat = 1 } function reset() { nodes = [] links = [] grow() for (let i = 1; i < 10; i++) grow(nodes[Math.floor((i - 1) / 2)]) } reset() // Every pair repels, every link pulls toward 32px, everything // drifts to the center. Once the heat is gone the loop stops // calling update(), so a settled graph costs nothing. function step() { if (heat < 0.005) return false for (const a of nodes) { for (const b of nodes) { if (a === b) continue const dx = b.x - a.x const dy = b.y - a.y const d2 = Math.max(dx * dx + dy * dy, 25) a.vx -= (dx / d2) * 14 * heat a.vy -= (dy / d2) * 14 * heat } } for (const [a, b] of links) { const dx = b.x - a.x const dy = b.y - a.y const dist = Math.hypot(dx, dy) || 1 const f = ((dist - 32) / dist) * 0.1 * heat a.vx += dx * f; a.vy += dy * f b.vx -= dx * f; b.vy -= dy * f } for (const n of nodes) { n.vx += (W / 2 - n.x) * 0.015 * heat n.vy += (H / 2 - n.y) * 0.015 * heat if (n !== held) { n.x = clamp(n.x + n.vx, 10, W - 10) n.y = clamp(n.y + n.vy, 10, H - 10) } n.vx *= 0.6 n.vy *= 0.6 } heat = held ? Math.max(heat, 0.3) : heat * 0.985 } // The pointer in viewBox units: the svg is scaled to its box. function pointer(e: PointerEvent) { const box = (e.currentTarget as Element).getBoundingClientRect() return [((e.clientX - box.left) / box.width) * W, ((e.clientY - box.top) / box.height) * H] } export const Graph = () => div( { style: { display: 'flex', flexDirection: 'column', alignItems: 'center', gap: '18px' } }, scope('graph'), svgSvg( { viewBox: `0 0 ${W} ${H}`, width: '100%', role: 'img', 'aria-label': 'Force-directed graph', style: { maxWidth: '380px', touchAction: 'none', overflow: 'visible' }, // simulate every frame, and update only this scope while it moves onMount: () => { let id = requestAnimationFrame(function frame() { if (step() !== false) update('graph') id = requestAnimationFrame(frame) }) return () => cancelAnimationFrame(id) }, onPointerMove: (e) => { if (!held) return if (Math.hypot(e.clientX - press[0], e.clientY - press[1]) > 4) moved = true if (moved) [held.x, held.y] = pointer(e) }, onPointerUp: () => { if (held && !moved && nodes.length < 40) grow(held) // a press that did not move is a click held = null }, }, list(() => links, ([a, b]) => lineSvg({ stroke: '#ddd', 'stroke-width': 1.5, x1: () => a.x, y1: () => a.y, x2: () => b.x, y2: () => b.y, }), ), list(() => nodes, (n) => circleSvg({ r: n.depth ? 7 : 10, fill: COLORS[n.depth % 3], stroke: '#fff', 'stroke-width': 2, cx: () => n.x, cy: () => n.y, style: { cursor: 'grab' }, onPointerDown: (e) => { e.currentTarget.setPointerCapture(e.pointerId) held = n press = [e.clientX, e.clientY] moved = false heat = Math.max(heat, 0.3) }, }), ), ), div( { style: { display: 'flex', gap: '8px' } }, button('Shake', { onClick: () => { for (const n of nodes) { n.x = rand(20, W - 20); n.y = rand(20, H - 20) } heat = 1 }, }), button('Reset', { onClick: reset }), ), div( { style: { fontSize: '0.75rem', color: '#8b949e' } }, () => `${nodes.length} nodes · click one to grow it, drag to move it`, ), )