Blog · Frontend · · 4 min read
Animating tree connectors without a frame loop
Every branch line in Ptree's Tree view bends smoothly when a clade expands, collapses, pages, or folds into the left gutter. There is no requestAnimationFrame loop anywhere in the viewer. The browser's own CSS transition engine interpolates SVG d paths, group translations, and camera motion from one DOM write per layout.
Matching path topology so d can interpolate
CSS transition: d only interpolates between two SVG paths when they have the exact same sequence of path commands. If a straight horizontal link uses M ... L ... while a curved link uses M ... C ..., the browser snaps between them instead of morphing.
The connector builder generates every standard branch curve with the exact same command structure. Each curve splits vertically at half the row pitch and converts quadratic arcs to cubic Béziers by placing cubic control points two-thirds of the way toward the quadratic corner. Endpoints are formatted to three decimals and control points to six, with STEEP = 0.5 controlling how sharply steep fans leave the parent stem. Even a perfectly horizontal link to a child on the same row emits the same cubic command skeleton with zero vertical offset, so moving that child up or down on the next layout interpolates cleanly without snapping.
Detour paths around folded strips use rounded elbows of radius R = pitch / 2 and share their own consistent command template.
M C L C command skeleton so CSS interpolates d directly with no frame loop.One-column SVG coordinates
Each parent node's outgoing edges sit inside one <g> element inside .tree .plane > svg. Sizing the <svg> to the whole tree width would change what 100% means every time a column is added and shift every existing group. Instead, the <svg> is fixed to one column wide (width: var(--colw)) with overflow: visible:
.tree .plane > svg {
position: absolute;
inset-block-start: 0;
inset-inline-start: calc(-1 * var(--colw));
width: var(--colw);
height: calc(var(--extent) * var(--lh));
overflow: visible;
pointer-events: none;
}
Every <g> translates by --vc in column percentages and --i in row pitches:
.tree .plane > svg > g {
--fan-origin: calc(var(--vc) * 100%), calc(var(--i) * var(--lh) + var(--sp) * (var(--colw) - var(--colw-0)));
transform: translate(var(--fan-origin));
transition: transform var(--glide) var(--glide-ease);
}
Because each <path> draws in local coordinates relative to its parent's attachment point, shifting an entire subtree horizontally or vertically updates only --vc and --i on the <g> without touching any path string inside it.
Arrival and retraction with @starting-style
New fans and edges enter without a two-frame JavaScript dance. When the layout creates a <g> or <path>, it writes --start-translate, --start-d, and --born inline, and CSS reads them inside @starting-style:
.tree .plane > svg path {
fill: none;
stroke: var(--branch-color);
stroke-width: var(--line-w);
stroke-linecap: square;
transition: d var(--glide) var(--glide-ease),
transform var(--glide) var(--glide-ease),
opacity var(--glide) var(--glide-ease);
}
@starting-style {
.tree .plane > svg > g { --fan-origin: var(--start-translate); }
.tree .plane > svg path { opacity: 0; d: var(--start-d); }
.tree .plane > svg > g:not(.fold) > path { transform: scaleY(var(--born)); }
}
For an ordinary newly opened fan, --born is 0, so the curves unroll vertically from the parent's row via scaleY(0) to scaleY(1) while fading in. For a thicker .spine path drawn over an edge that was already on screen, --born is 1, so the highlight fades in at full length without unrolling a second time.
When a branch collapses, _reap() adds .leaving instead of removing the element immediately:
.tree .plane > svg > g:not(.fold) > path.leaving,
.tree .plane > svg > g.leaving > path {
transform: scaleY(0);
opacity: 0;
}
The edges fold back onto their parent's row on the exact reverse of entry. Once the view's transition counter returns to zero, _sweep() removes every .leaving element and any emptied <g> or <ul> container.
Keeping SVG lines locked to HTML tiles on high-DPI screens
Early versions translated the <svg> and the <ul> node layer separately when panning sideways. On high-DPI screens, Chrome composites an HTML box's transform at any device pixel ratio, but only composites an SVG element's own transform at an effective zoom of 1 (Chromium bug 40172437). The connectors ran on the main thread and visibly trailed behind the tiles during a glide.
The fix is two lines of markup: both the <svg> and the tile <ul> layer sit inside one HTML <div class="plane">, and .plane carries the horizontal translateX. Because the HTML wrapper owns the composited transform, the SVG lines and HTML tiles pan as one surface at every pixel ratio.
Smooth vertical camera handoff
Expanding a column above or below the clicked tile changes total tree height and moves rows vertically. Calling scrollTo({ behavior: "smooth" }) fights with simultaneous CSS transitions on tiles and stops abruptly when another click arrives.
Instead, Branching._camera() simulates vertical scrolling by setting a transitioned transform: translateY() on .scene equal to the distance between the current scrollTop and the target scroll offset. It computes that offset purely from tracked scroll, tracked viewport height, and CSS-resolved pitch (--label-metrics), with zero DOM layout reads during the layout task. When all transitions settle (or immediately if the user touches the wheel, scrollbar, pointer, or keyboard), _rebaseCamera() freezes transform at its computed offset, applies the exact opposite untransitioned translate, and transfers the delta into native scrollTop:
const offset = new DOMMatrixReadOnly(getComputedStyle(this._scene).transform).m42;
this._scene.style.transform = `translateY(${offset}px)`;
this._scene.style.translate = `0 ${-offset}px`;
this.root.scrollTop = this._scroll = this.root.scrollTop - offset - base;
Nothing moves on screen during the handoff, native scrolling resumes from the exact pixel where the glide stopped, and interrupting a glide mid-flight cancels smoothly without snapping.