DEV Community

Cover image for Building a transit map editor in the browser: 45 routing, rounded corners and parallel line bundling
transitmapper
transitmapper

Posted on Edited on AI-assisted

Building a transit map editor in the browser: 45 routing, rounded corners and parallel line bundling

Microsoft Publisher is being discontinued at the end of October 2026. For years I used it for a hobby: designing transit network maps, the kind with colored lines, 45° bends and round stop symbols. I don’t need InDesign for that, and I couldn’t find a simple replacement. So I built one.

It’s called Liniennetz-Editor, it’s free and open source (MIT), and it runs completely in the browser with no backend. Here’s how it works and what turned out to be tricky.

The idea: Describe, not draw

Graphics programs make you draw every line segment. For a network with 50 lines and 300 stations that is miserable. The editor flips this around:

  1. Describe the network: stops, lines, categories (e.g. “frequent” vs. “rare”), which line serves which stops
  2. Place the stops on a grid
  3. The program computes everything else: connections, corners, parallel lanes, stop symbols, key

The result is a modern-looking map that only uses 0°, 45° and 90°, exported as SVG, PNG or PDF.

Stack

  • React and Fluent UI for the surrounding interface
  • Every edit clones the project state, which gives undo/redo almost for free
  • The map itself is plain SVG generated as a string in a separate engine module with no UI dependency
  • Projects are saved as JSON files, no server involved

Keeping the engine separate from the UI was the best early decision. The same function renders the interactive canvas and the export.

1. Routing between two grid points

Between two stops on a grid there are three cases: same row or column, exactly diagonal, or neither. The third case is the interesting one. You need a diagonal part of length min(|dx|, |dy|) plus a straight part for the rest.

Where the straight part goes matters visually. Putting it all at one end looks clumsy, so I split it between both ends and let the diagonal sit in the middle:

function route(a, b) {
  const dx = b.x - a.x, dy = b.y - a.y;
  const ax = Math.abs(dx), ay = Math.abs(dy);
  if (!ax || !ay || ax === ay) return [a, b];      // straight or pure diagonal

  const d  = Math.min(ax, ay);                      // diagonal length
  const n  = Math.abs(ax - ay);                     // straight remainder
  const n1 = Math.ceil(n / 2);                      // straight part at the start
  const sx = Math.sign(dx), sy = Math.sign(dy);
  const horizontal = ax > ay;

  const p1 = { x: a.x + (horizontal ? sx * n1 : 0),
               y: a.y + (horizontal ? 0 : sy * n1) };
  const p2 = { x: p1.x + sx * d, y: p1.y + sy * d };
  return [a, p1, p2, b];
}
Enter fullscreen mode Exit fullscreen mode

The real version also handles odd remainders (a flip toggle lets the user choose which end gets the larger part) and removes degenerate points.

One design rule matters a lot: routes are computed per edge, not per line. If two lines share a section, they automatically share the same geometry, which makes bundling possible later.

2. Rounded corners

Corners are replaced with circular arcs. The tangent length depends on the turning angle, and the radius is clamped so arcs never overlap on short segments:

function fillet(a, v, b, r) {
  const l1 = Math.hypot(v.x - a.x, v.y - a.y);
  const l2 = Math.hypot(b.x - v.x, b.y - v.y);
  const u1 = { x: (v.x - a.x) / l1, y: (v.y - a.y) / l1 };
  const u2 = { x: (b.x - v.x) / l2, y: (b.y - v.y) / l2 };

  const theta = Math.acos(u1.x * u2.x + u1.y * u2.y);  // turning angle
  const k = Math.tan(theta / 2);
  const t = Math.min(r * k, l1 / 2, l2 / 2);           // tangent length, clamped
  const rr = t / k;                                    // effective radius
  const sweep = (u1.x * u2.y - u1.y * u2.x) > 0 ? 1 : 0;

  return `L${v.x - u1.x * t} ${v.y - u1.y * t}` +
         `A${rr} ${rr} 0 0 ${sweep} ${v.x + u2.x * t} ${v.y + u2.y * t}`;
}
Enter fullscreen mode Exit fullscreen mode

That string goes straight into an SVG <path d="...">.

3. The hard part: bundling parallel lines

When three lines run along the same track, they should appear as three parallel strokes with a constant gap, including through bends.

My approach:

  • Break every line into unit grid steps. Each step gets a key (its two endpoints).
  • For each step, collect all lines that use it. Each line’s lane is its index in that list, centered: offset = (index - (count - 1) / 2) * gap.
  • At every vertex, compute the offset path’s corner by intersecting the two offset lines. That keeps the gap constant through a bend.
  • For radii, give the innermost line the base radius and let the outer lines run concentrically around it, so strokes don’t cross inside a curve.

The nastiest bug class here is lane order. If two lines share a stretch, they must keep the same relative order along the whole shared run, otherwise they cross each other for no reason. The engine decides the order once per contiguous shared run, using the geometry (which side each line turns toward) as the tiebreaker. Users can override the order per connection by clicking it.

Where a lane count changes (a line joins or leaves a bundle), the path needs a 45° shift so the gap is preserved. Currently the whole bundle re-centers when that happens, which creates a visible kink in otherwise straight lines. A Reddit user rightly pointed out that straightness should beat symmetry here, so that’s next on my list.

4. Stop symbols

A stop served by one line is a circle. A stop served by several parallel lines becomes a pill spanning all lanes, oriented across the bundle direction (including diagonals). If lines cross at different angles, the symbol turns into a rounded rectangle. Stops where only some lines call get a gap in the pill, drawn as separate parts joined by a thin connector.

5. Performance

With about 300 stops and 50 lines, recomputing everything on each mouse move would be slow. A cheap fix works well: the engine builds a cache key from the relevant parts of the state and returns the previous result if nothing that affects geometry has changed. Dragging a label doesn’t recompute the routing.

What I’d do differently

Add automated tests for the geometry earlier. I verified the routing visually, which doesn’t scale.
Decide on the data model for lane order from day one instead of retrofitting manual overrides.

Example map: Ostthüringen regional rail, Germany

Ostthüringen regional rail transit map

Try it

Editor and examples: https://transitmapper.github.io/liniennetzeditor/en/
Source (MIT): https://github.com/transitmapper/liniennetzeditor

The interface is German only for now. If you make transit maps, or just like grid geometry, I’d love feedback. What’s missing? What’s confusing?

Top comments (0)