diff --git a/internal/gui/assets/plugins/routing/client.js b/internal/gui/assets/plugins/routing/client.js index 6c9407f5f..67a3c21c4 100644 --- a/internal/gui/assets/plugins/routing/client.js +++ b/internal/gui/assets/plugins/routing/client.js @@ -1491,6 +1491,71 @@ function routingPage(part) { return s + ` C${b[0]} ${b[1]} ${c[0]} ${c[1]} ${x3} ${y3}`; } + // a flowing wire is dashed only near what is in sight (#3164): one to a + // key hundreds of rows down is tens of thousands of pixels long, and + // WebKit stroked every dash of it again on each frame it flowed, under + // 50 fps with two groups open. Its dashes from a screen above to a screen + // below what is in sight are kept, from a multiple of their 8px so they + // stay in step with the whole wire's; the rest of it is a gap, out of + // sight (dashFor). Nothing is laid out for it: where the + // stage and what is in sight were is kept by layout, which reads them + // anyway (sight), and the view's scroll since by its scroll events. + // Reading them here laid the stage out again after each update + const flowing = new Map(); + let flowAsked = 0, sight = null, scrolled = 0; + const flowSoon = () => { if (!flowAsked) flowAsked = requestAnimationFrame(() => { flowAsked = 0; flowNear(); }); }; + function flowNear() { + const live = new Set([...wires.querySelectorAll("path.live[d]")].filter((p) => !p.classList.contains("rest"))); + for (const p of flowing.keys()) if (!live.has(p)) { p.style.removeProperty("stroke-dasharray"); flowing.delete(p); } + if (!live.size || !sight) return; + // in the stage's own terms, a screen more each way, in steps of 200px + // so a scroll redashes a wire only now and then + const at = sight.at - (scrolled - sight.scroll), m = Math.max(sight.hi - sight.lo, 200); + const top = Math.floor((sight.lo - m - at) / 200) * 200, bottom = Math.ceil((sight.hi + m - at) / 200) * 200; + for (const p of live) { + // the same wire with the same in sight is dashed as it was: this is + // asked after every update + const d = p.getAttribute("d"), key = `${d}|${top}|${bottom}`; + if (flowing.get(p)?.key === key) continue; + const dash = dashFor(d, top, bottom); + const was = flowing.get(p)?.dash; + flowing.set(p, { key, dash }); + if (was === dash) continue; + if (dash) p.style.setProperty("stroke-dasharray", dash); + else p.style.removeProperty("stroke-dasharray"); + } + } + // dashFor is a wire's dashes from top to bottom, measured from its d, + // which is magpie's own M, L, Q and C: WebKit's getPointAtLength took + // milliseconds a wire, and a wire's d changes with most updates. Each + // bend is walked in 16 straight steps, a little shorter than the bend, + // so its ends are moved out by 64px each way, more than that is off + const dashFor = (d, top, bottom) => { + const n = (d.match(/-?[\d.]+(?:e-?\d+)?/g) || []).map(Number), cmd = d.match(/[MLQC]/g) || []; + let x = n[0], y = n[1], k = 2, len = 0, s0 = Infinity, s1 = -Infinity; + const mark = (l) => { s0 = Math.min(s0, l); s1 = Math.max(s1, l); }; + if (y >= top && y <= bottom) mark(0); + for (const c of cmd.slice(1)) { + const q = c === "L" ? 2 : c === "Q" ? 4 : 6, v = n.slice(k, k += q), steps = c === "L" ? 1 : 16; + for (let i = 1; i <= steps; i++) { + const t = i / steps, u = 1 - t; + const [px, py] = c === "L" ? [v[0], v[1]] + : c === "Q" ? [u * u * x + 2 * u * t * v[0] + t * t * v[2], u * u * y + 2 * u * t * v[1] + t * t * v[3]] + : [u * u * u * x + 3 * u * u * t * v[0] + 3 * u * t * t * v[2] + t * t * t * v[4], u * u * u * y + 3 * u * u * t * v[1] + 3 * u * t * t * v[3] + t * t * t * v[5]]; + // where a step crosses an edge of the band, a straight one + // thousands of pixels long too + const l = Math.hypot(px - x, py - y); + for (const e of [top, bottom]) if (py !== y && (y - e) * (py - e) <= 0) mark(len + l * (e - y) / (py - y)); + len += l; x = px; y = py; + if (y >= top && y <= bottom) mark(len); + } + } + if (s0 === Infinity) return `0 ${Math.ceil(len) + 64}`; + const a = Math.max(0, Math.floor(s0 / 8) * 8 - 64), z = Math.ceil(s1 / 8) * 8 + 64; + return a <= 0 && z >= len ? "" : `0 ${a} ${"3 5 ".repeat((z - a) / 8)}0 ${Math.ceil(len) + 64}`; + }; + $("#view-routing").addEventListener("scroll", (e) => { scrolled = e.currentTarget.scrollTop; flowSoon(); }, { passive: true }); + function layout() { const r = stage.getBoundingClientRect(); if (!r.width) return; @@ -1547,12 +1612,17 @@ function routingPage(part) { // set after this frame: the keys not drawn changing size resize the // stage, which in its own ResizeObserver WebKit reports as a loop if (rowH && list.style.getPropertyValue("--rt-row") !== rowH + "px") requestAnimationFrame(() => list.style.setProperty("--rt-row", rowH + "px")); + // read before the wires are set, which lays the stage out again + const view = $("#view-routing"), v = view.getBoundingClientRect(); + scrolled = view.scrollTop; + sight = { at: r.top, scroll: scrolled, lo: Math.max(0, v.top), hi: Math.min(innerHeight, v.bottom) }; wires.setAttribute("viewBox", `0 0 ${r.width} ${r.height}`); sky.setAttribute("viewBox", `0 0 ${r.width} ${r.height}`); for (const [p, d] of sets) { if (d === null) { if (p.hasAttribute("d")) p.removeAttribute("d"); } else if (p.getAttribute("d") !== d) p.setAttribute("d", d); } + flowSoon(); holdAll(); } @@ -2191,6 +2261,7 @@ function routingPage(part) { if (lit) s.wire.style.setProperty("--agent", lit.wire.style.getPropertyValue("--agent")); } for (const [id, a] of agents) a.wire.classList.toggle("live", busy.has(id)); + flowSoon(); } // ---------- the log: how one request was routed ----------