Build a draggable node diagram without a library

By

Tagged: JavaScript

Build a Railway-style draggable infrastructure canvas with vanilla JS. Positioned nodes, SVG edges, pointer events, no diagram library needed.

Railway’s stack canvas looks like a full diagram tool, with draggable nodes, connecting edges, and live cost numbers.

You don’t need React Flow or D3 to build something like that. I built one in vanilla TypeScript for one of my apps, using plain divs, an SVG layer, and pointer events instead of a diagram library.

Two layers in one container

The canvas is a relative container with a fixed height. Nodes are absolute divs inside it, while the edges live in an SVG that covers the whole area with pointer-events: none.

Splitting them like this keeps HTML cards easy to style and read, while letting SVG redraw the connecting lines whenever a node moves. Here’s how the two layers fit together:

<div class="canvas relative" style="width:900px;height:560px">
  <svg class="absolute inset-0 pointer-events-none">
    <line x1="172" y1="280" x2="452" y2="120" stroke="#94a3b8" />
  </svg>
  <div class="node absolute" style="left:60px;top:232px;width:224px">
    Cloudflare Workers
  </div>
</div>

Each node reads its left and top values from a { x, y } map. When you drag a card, you update that map and let Alpine (or any reactive layer) re-bind the styles.

Drag with pointer events

Mouse events tend to break when the cursor leaves the element mid-drag, so use pointer events instead.

On pointerdown, store the node id along with the offset between the cursor and the node’s top-left corner, then call setPointerCapture on the target so you keep receiving events even if the pointer moves outside the card:

onPointerDown(e, nodeId) {
  this.draggingId = nodeId
  const rect = canvas.getBoundingClientRect()
  const pos = positions[nodeId]
  this.dragOffset = {
    x: e.clientX - rect.left - pos.x,
    y: e.clientY - rect.top - pos.y,
  }
  e.currentTarget.setPointerCapture(e.pointerId)
}

onPointerMove(e) {
  if (!this.draggingId) return
  const rect = canvas.getBoundingClientRect()
  positions[this.draggingId] = {
    x: e.clientX - rect.left - this.dragOffset.x,
    y: e.clientY - rect.top - this.dragOffset.y,
  }
}

onPointerUp(e) {
  e.currentTarget.releasePointerCapture(e.pointerId)
  this.draggingId = null
}

My advice is to add a small movement threshold before treating a press as a drag. That way a quick tap can open a detail drawer, while an actual drag moves the card.

Recompute edges on every move

Each edge stores from and to node ids along with x1, y1, x2, and y2. Whenever positions change, recompute those coordinates from the center of each card:

function nodeCenter(pos) {
  return { x: pos.x + cardW / 2, y: pos.y + cardH / 2 }
}

function updateEdgeGeometry(edges, positions) {
  for (const edge of edges) {
    const c1 = nodeCenter(positions[edge.from])
    const c2 = nodeCenter(positions[edge.to])
    edge.x1 = c1.x
    edge.y1 = c1.y
    edge.x2 = c2.x
    edge.y2 = c2.y
    edge.len = Math.hypot(c2.x - c1.x, c2.y - c1.y)
  }
}

Watch the positions object and run this every time it updates so the SVG lines follow the cards frame by frame.

Animate cost numbers with requestAnimationFrame

When a user switches tiers, the monthly price jumps, and snapping straight to the new number feels jarring. Animating the transition with a short tween makes the change much easier to follow.

Keep a display value separate from the target value, then interpolate between them each frame with an ease-out curve and round to cents:

function animateNumber(from, to, onUpdate, durationMs = 600) {
  const start = performance.now()

  function tick(now) {
    const t = Math.min(1, (now - start) / durationMs)
    const eased = 1 - Math.pow(1 - t, 3)
    onUpdate(t >= 1 ? to : Math.round(from + (to - from) * eased))
    if (t < 1) requestAnimationFrame(tick)
  }

  requestAnimationFrame(tick)
}

Run one tween per visible node plus one for the stack total, and make sure to cancel the previous tween if a new target arrives mid-animation.

Also check prefers-reduced-motion and skip the tween when it’s enabled, setting the final value right away.

What you get

This setup skips a dependency graph and a layout solver altogether. Starting with nodes placed in columns is enough for the base canvas, and most of the remaining work goes into polish like staggered enter animations, edge draw-ins, and debounced layout saves.

Once you need more than draggable cards and connecting lines, such as freehand drawing, undo history, or multiplayer, a dedicated library starts to pay off. The tldraw SDK covers all of that if you’re working in React.

Want me to talk about your product? You can sponsor this site.

Related posts about js: