From b64fac1a02fbda1b9b0181acdd472b26d21b2801 Mon Sep 17 00:00:00 2001 From: yuxino Date: Sun, 15 Nov 2026 02:54:42 +0800 Subject: [PATCH] fix: preserve visible picks in pointer pools --- src/interactions/pointer.js | 6 +++--- test/pointer-test.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/interactions/pointer.js b/src/interactions/pointer.js index 7aa19f25f9..8ac7c43f4e 100644 --- a/src/interactions/pointer.js +++ b/src/interactions/pointer.js @@ -33,7 +33,7 @@ function pointerK(kx, ky, {x, y, px, py, maxRadius = 40, channels, render, ...op // the pointer (e.g., tips); only the closest point is shown. let state = states.get(svg); if (!state) { - state = {sticky: false, roots: [], renders: [], pool: this.pool ? {map: new Map()} : null}; + state = {sticky: false, roots: [], renders: []}; states.set(svg, state); } @@ -82,7 +82,7 @@ function pointerK(kx, ky, {x, y, px, py, maxRadius = 40, channels, render, ...op // only show the closest. We defer rendering using an animation frame to // allow all pointer events to be received before deciding which mark to // render; although when hiding, we render immediately. - const pool = state.pool ?? facetPool; + const pool = this.pool ? (state.pool ??= {map: new Map()}) : facetPool; function update(ii, ri) { if (!pool) return void render(ii); if (ii == null) render(ii); @@ -91,7 +91,7 @@ function pointerK(kx, ky, {x, y, px, py, maxRadius = 40, channels, render, ...op pool.frame = requestAnimationFrame(() => { pool.frame = undefined; let best = null; - for (const c of pool.map.values()) if (!best || c.ri < best.ri) best = c; + for (const c of pool.map.values()) if (c.ii != null && (!best || c.ri < best.ri)) best = c; for (const c of pool.map.values()) c.render(c === best ? c.ii : null); }); } diff --git a/test/pointer-test.js b/test/pointer-test.js index f00282f0ac..2406b49c77 100644 --- a/test/pointer-test.js +++ b/test/pointer-test.js @@ -79,6 +79,39 @@ describe("pointer pool", () => { assert.strictEqual(visibleTips(svg), 1); }); + it("shows the tip in range when another tip has a smaller radius and no point in range", () => { + const svg = Plot.plot({ + x: {domain: [0, 10]}, + y: {domain: [0, 10]}, + marks: [ + Plot.dot([{x: 2, y: 2, id: "near"}], {x: "x", y: "y", title: "id", tip: true}), + Plot.dot([{x: 9, y: 9, id: "far"}], {x: "x", y: "y", title: "id", tip: {maxRadius: 1}}) + ] + }); + pointer(svg, "pointermove", svg.scale("x").apply(2) + 10, svg.scale("y").apply(2)); + flushAnimationFrame(); + assert.strictEqual(visibleTips(svg), 1); + assert.include(svg.querySelector("[aria-label=tip] text").textContent, "near"); + }); + + for (const tipFirst of [true, false]) { + it(`pools only participating marks when the tip is ${tipFirst ? "first" : "last"}`, () => { + const data = [{x: 2, y: 2, id: "near"}]; + const first = Plot.tip(data, Plot.pointer({x: "x", y: "y", title: "id"})); + const second = Plot.tip([{x: 2.2, y: 2, id: "farther"}], Plot.pointer({x: "x", y: "y", title: "id"})); + const ring = Plot.dot([{x: 2.4, y: 2}], Plot.pointer({x: "x", y: "y", pool: false})); + const svg = Plot.plot({ + x: {domain: [0, 10]}, + y: {domain: [0, 10]}, + marks: tipFirst ? [first, second, ring] : [ring, first, second] + }); + pointer(svg, "pointermove", svg.scale("x").apply(2), svg.scale("y").apply(2)); + flushAnimationFrame(); + assert.strictEqual(visibleTips(svg), 1); + assert.isNotNull(svg.querySelector("[aria-label=dot] circle")); + }); + } + it("crosshair renders all sub-marks (does not pool)", () => { const svg = Plot.plot({ marks: [Plot.crosshair([{x: 1, y: 1}, {x: 2, y: 2}], {x: "x", y: "y"})] // prettier-ignore