From f39fb94e097ea23ac7ea185cfa61e5dc1b0585f4 Mon Sep 17 00:00:00 2001 From: Dominik Date: Fri, 4 Dec 2026 00:24:50 -0800 Subject: [PATCH 1/2] Pin gl-plot3d to the fork that draws alwaysOnTop objects last The fork skips objects with alwaysOnTop in the depth, opaque and transparent passes and draws them after the composite. Rebuild stackgl_modules/index.js with npm run bundle-stackgl. --- stackgl_modules/index.js | 35 +++++++++++++++++++++++++------ stackgl_modules/package-lock.json | 4 ++-- stackgl_modules/package.json | 2 +- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/stackgl_modules/index.js b/stackgl_modules/index.js index e984b19404e..fea9559400f 100644 --- a/stackgl_modules/index.js +++ b/stackgl_modules/index.js @@ -19901,7 +19901,7 @@ function createScene(options) { } for(var i = 0; i < numObjsNow(); ++i) { var o = objects[i] - if(o.isOpaque && o.isOpaque()) o.draw(cameraParams) + if(o.isOpaque && o.isOpaque() && !o.alwaysOnTop) o.draw(cameraParams) } gl.colorMask(true, true, true, true) @@ -20173,10 +20173,10 @@ function createScene(options) { var obj = objects[i] obj.axes = axes obj.pixelRatio = scene.pixelRatio - if(obj.isOpaque && obj.isOpaque()) { + if(obj.isOpaque && obj.isOpaque() && !obj.alwaysOnTop) { obj.draw(cameraParams) } - if(obj.isTransparent && obj.isTransparent()) { + if(obj.isTransparent && obj.isTransparent() && !obj.alwaysOnTop) { hasTransparent = true } } @@ -20187,7 +20187,7 @@ function createScene(options) { var legacyObjs = false for(var i=0; i Date: Fri, 4 Dec 2026 00:24:50 -0800 Subject: [PATCH 2/2] Add alwaysontop to scatter3d traces to keep a highlight visible Points in front hide a highlighted marker inside a dense cloud. The markers of a trace with alwaysontop true draw last, over everything else, with no depth test. The default is false and changes no pixel. The markers stay pickable. --- draftlogs/XXXX_add_alwaysontop.md | 1 + src/traces/scatter3d/attributes.js | 12 +++ src/traces/scatter3d/convert.js | 1 + src/traces/scatter3d/defaults.js | 1 + src/types/generated/schema.d.ts | 5 + test/jasmine/tests/scatter3d_test.js | 152 +++++++++++++++++++++++++++ test/plot-schema.json | 6 ++ 7 files changed, 178 insertions(+) create mode 100644 draftlogs/XXXX_add_alwaysontop.md diff --git a/draftlogs/XXXX_add_alwaysontop.md b/draftlogs/XXXX_add_alwaysontop.md new file mode 100644 index 00000000000..5aee154ea17 --- /dev/null +++ b/draftlogs/XXXX_add_alwaysontop.md @@ -0,0 +1 @@ +- Add `alwaysontop` to `scatter3d` traces to draw their markers last, over everything else [[#XXXX](https://github.com/plotly/plotly.js/pull/XXXX)] diff --git a/src/traces/scatter3d/attributes.js b/src/traces/scatter3d/attributes.js index 7191d73c1b6..70dd21209a8 100644 --- a/src/traces/scatter3d/attributes.js +++ b/src/traces/scatter3d/attributes.js @@ -172,6 +172,18 @@ var attrs = (module.exports = overrideAll( opacity: baseAttrs.opacity, + alwaysontop: { + valType: 'boolean', + dflt: false, + editType: 'calc', + description: [ + 'If *true*, the markers of this trace are drawn last, on top of everything else', + 'in the scene, without depth testing, in trace order.', + 'Use it for a highlight that must never be hidden by other points.', + 'The markers stay pickable.' + ].join(' ') + }, + hoverinfo: extendFlat({}, baseAttrs.hoverinfo) }, 'calc', diff --git a/src/traces/scatter3d/convert.js b/src/traces/scatter3d/convert.js index 019d362e5fe..bbd875522bf 100644 --- a/src/traces/scatter3d/convert.js +++ b/src/traces/scatter3d/convert.js @@ -436,6 +436,7 @@ proto.update = function (data) { this.scatterPlot.highlightScale = 1; this.scene.glplot.add(this.scatterPlot); } + this.scatterPlot.alwaysOnTop = !!data.alwaysontop; } else if (this.scatterPlot) { this.scene.glplot.remove(this.scatterPlot); this.scatterPlot.dispose(); diff --git a/src/traces/scatter3d/defaults.js b/src/traces/scatter3d/defaults.js index eb366090612..73e10033819 100644 --- a/src/traces/scatter3d/defaults.js +++ b/src/traces/scatter3d/defaults.js @@ -30,6 +30,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout coerce('zhoverformat'); coerce('mode'); + coerce('alwaysontop'); if (subTypes.hasMarkers(traceOut)) { handleMarkerDefaults(traceIn, traceOut, defaultColor, layout, coerce, { diff --git a/src/types/generated/schema.d.ts b/src/types/generated/schema.d.ts index d47452f3464..6056000dce8 100644 --- a/src/types/generated/schema.d.ts +++ b/src/types/generated/schema.d.ts @@ -7447,6 +7447,11 @@ export interface ScatterData { } export interface Scatter3dData { + /** + * If *true*, the markers of this trace are drawn last, on top of everything else in the scene, without depth testing, in trace order. Use it for a highlight that must never be hidden by other points. The markers stay pickable. + * @default false + */ + alwaysontop?: boolean; /** * Determines whether or not gaps (i.e. {nan} or missing values) in the provided data arrays are connected. * @default false diff --git a/test/jasmine/tests/scatter3d_test.js b/test/jasmine/tests/scatter3d_test.js index 96f6e818d0e..597316cda6c 100644 --- a/test/jasmine/tests/scatter3d_test.js +++ b/test/jasmine/tests/scatter3d_test.js @@ -9,6 +9,7 @@ var createGraphDiv = require('../assets/create_graph_div'); var destroyGraphDiv = require('../assets/destroy_graph_div'); var delay = require('../assets/delay'); +var mouseEvent = require('../assets/mouse_event'); function countCanvases() { return d3SelectAll('canvas').size(); @@ -77,6 +78,11 @@ describe('Scatter3D defaults', function() { expect(out.marker.line.color).toBe(Color.defaultLine); }); + it('should default alwaysontop to false', function() { + expect(_supply(Lib.extendFlat({}, base)).alwaysontop).toBe(false); + expect(_supply(Lib.extendFlat({ alwaysontop: true }, base)).alwaysontop).toBe(true); + }); + it('should inherit layout.calendar', function() { var out = _supply(base, {calendar: 'islamic'}); @@ -542,4 +548,150 @@ describe('Test scatter3d interactions:', function() { .then(done, done.fail); }); }); + + describe('alwaysontop', function() { + const W = 300; + const H = 300; + + // 3000 translucent red markers around the origin and one opaque black marker behind most of them + function traces(top, onlyMarker) { + const x = []; + const y = []; + const z = []; + let seed = 7; + const rnd = () => { + seed = (seed * 16807) % 2147483647; + return seed / 2147483647 - 0.5; + }; + for (let i = 0; i < 3000; i++) { + x.push(rnd() * 1.6); + y.push(rnd() * 1.6); + z.push(rnd() * 1.6); + } + const cloud = { + type: 'scatter3d', + mode: 'markers', + x: x, + y: y, + z: z, + hoverinfo: 'skip', + marker: { size: 8, color: 'rgb(220,40,40)', opacity: 0.6, line: { width: 0 } } + }; + const focus = { + type: 'scatter3d', + mode: 'markers', + x: [0], + y: [0], + z: [-0.3], + alwaysontop: top, + marker: { size: 30, color: 'black', line: { width: 0 } } + }; + return onlyMarker ? [focus] : [cloud, focus]; + } + + function plot(data, oit) { + return Plotly.react(gd, data, { + width: W, + height: H, + margin: { l: 0, r: 0, t: 0, b: 0 }, + paper_bgcolor: 'white', + showlegend: false, + uirevision: Math.random(), + scene: { + oit: oit, + bgcolor: 'white', + xaxis: { range: [-1, 1], visible: false }, + yaxis: { range: [-1, 1], visible: false }, + zaxis: { range: [-1, 1], visible: false }, + aspectmode: 'cube', + camera: { + eye: { x: 0, y: 0, z: 2.5 }, + up: { x: 0, y: 1, z: 0 }, + projection: { type: 'orthographic' } + } + } + }).then(delay(400)); + } + + function pixels() { + const glplot = gd._fullLayout.scene._scene.glplot; + const gl = glplot.gl; + glplot.redraw(); + const px = new Uint8Array(gl.drawingBufferWidth * gl.drawingBufferHeight * 4); + gl.readPixels(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight, gl.RGBA, gl.UNSIGNED_BYTE, px); + return { w: gl.drawingBufferWidth, h: gl.drawingBufferHeight, px: px }; + } + + const isBlack = (p, i) => p.px[4 * i] === 0 && p.px[4 * i + 1] === 0 && p.px[4 * i + 2] === 0; + + ['off', 'auto'].forEach(function(oit) { + it('@gl draws the marker on top of the translucent cloud with scene.oit ' + oit, function(done) { + let without; + let footprint; + plot(traces(false), oit) + .then(function() { + without = pixels(); + return plot(traces(false, true), oit); + }) + .then(function() { + const p = pixels(); + footprint = []; + for (let i = 0; i < p.w * p.h; i++) footprint.push(isBlack(p, i)); + const covered = footprint.filter(Boolean).length; + expect(covered).toBeGreaterThan(400); + const hidden = footprint.filter((f, i) => f && !isBlack(without, i)).length; + expect(hidden).toBeGreaterThan(covered * 0.5); + return plot(traces(true), oit); + }) + .then(function() { + const withTop = pixels(); + const notBlack = footprint.filter((f, i) => f && !isBlack(withTop, i)).length; + expect(notBlack).toBe(0, 'every pixel of the marker shows its own color'); + + // outside the marker, dilated by 2 px for the soft edge, nothing changes + let changed = 0; + for (let i = 0; i < withTop.w * withTop.h; i++) { + const cx = i % withTop.w; + const cy = (i / withTop.w) | 0; + let near = false; + for (let dy = -2; dy <= 2 && !near; dy++) { + for (let dx = -2; dx <= 2; dx++) { + if (footprint[(cy + dy) * withTop.w + cx + dx]) { + near = true; + break; + } + } + } + if (near) continue; + for (let c = 0; c < 3; c++) { + if (withTop.px[4 * i + c] !== without.px[4 * i + c]) { + changed++; + break; + } + } + } + expect(changed).toBe(0, 'pixels outside the marker are unchanged'); + }) + .then(done, done.fail); + }); + }); + + it('@gl a marker with alwaysontop stays pickable', function(done) { + const hovered = []; + plot(traces(true, true), 'off') + .then(function() { + gd.on('plotly_hover', (e) => hovered.push(e.points[0].curveNumber)); + }) + .then(delay(20)) + .then(function() { + mouseEvent('mouseover', 0, 0); + mouseEvent('mouseover', W / 2, H / 2); + }) + .then(delay(100)) + .then(function() { + expect(hovered.length).toBeGreaterThan(0); + }) + .then(done, done.fail); + }); + }); }); diff --git a/test/plot-schema.json b/test/plot-schema.json index 6d57eef60ff..c70d9443716 100644 --- a/test/plot-schema.json +++ b/test/plot-schema.json @@ -55204,6 +55204,12 @@ "scatter3d": { "animatable": false, "attributes": { + "alwaysontop": { + "description": "If *true*, the markers of this trace are drawn last, on top of everything else in the scene, without depth testing, in trace order. Use it for a highlight that must never be hidden by other points. The markers stay pickable.", + "dflt": false, + "editType": "calc", + "valType": "boolean" + }, "connectgaps": { "description": "Determines whether or not gaps (i.e. {nan} or missing values) in the provided data arrays are connected.", "dflt": false,