Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions draftlogs/XXXX_add_alwaysontop.md
Original file line number Diff line number Diff line change
@@ -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)]
12 changes: 12 additions & 0 deletions src/traces/scatter3d/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions src/traces/scatter3d/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/traces/scatter3d/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
5 changes: 5 additions & 0 deletions src/types/generated/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 29 additions & 6 deletions stackgl_modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}
Expand All @@ -20187,7 +20187,7 @@ function createScene(options) {
var legacyObjs = false
for(var i=0; i<numObjs; ++i) {
var obj = objects[i]
if(obj.isTransparent && obj.isTransparent()) {
if(obj.isTransparent && obj.isTransparent() && !obj.alwaysOnTop) {
if(obj.drawOIT) {
oitObjs.push(obj)
} else {
Expand Down Expand Up @@ -20218,7 +20218,7 @@ function createScene(options) {
}
for(var i=0; i<numObjs; ++i) {
var obj = objects[i]
if(obj.isOpaque && obj.isOpaque()) {
if(obj.isOpaque && obj.isOpaque() && !obj.alwaysOnTop) {
obj.draw(cameraParams)
}
}
Expand All @@ -20238,7 +20238,7 @@ function createScene(options) {

for(var i=0; i<numObjs; ++i) {
var obj = objects[i]
if(obj.isTransparent && obj.isTransparent() && !(oitUse && obj.drawOIT)) {
if(obj.isTransparent && obj.isTransparent() && !obj.alwaysOnTop && !(oitUse && obj.drawOIT)) {
obj.drawTransparent(cameraParams)
}
}
Expand Down Expand Up @@ -20268,6 +20268,29 @@ function createScene(options) {
oitSetStatus(OIT_NONE)
}

// objects flagged alwaysOnTop: drawn last, over the composite, no depth test, premultiplied blending
var topDrawn = false
for(var i=0; i<numObjs; ++i) {
var obj = objects[i]
if(obj.alwaysOnTop) {
if(!topDrawn) {
topDrawn = true
gl.bindFramebuffer(gl.FRAMEBUFFER, null)
gl.viewport(0, 0, width, height)
gl.colorMask(true, true, true, true)
gl.disable(gl.DEPTH_TEST)
gl.enable(gl.BLEND)
gl.blendEquation(gl.FUNC_ADD)
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
}
obj.draw(cameraParams)
obj.drawTransparent(cameraParams)
}
}
if(topDrawn) {
gl.disable(gl.BLEND)
}

//Clear dirty flags
dirty = false
for(var i=0; i<numObjs; ++i) {
Expand Down
4 changes: 2 additions & 2 deletions stackgl_modules/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion stackgl_modules/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"gl-error3d": "^1.0.17",
"gl-line3d": "^1.2.2",
"gl-mesh3d": "^2.3.2",
"gl-plot3d": "github:settylab/gl-plot3d#bb627feee546a8caf13f21e4c91271643f85c1fa",
"gl-plot3d": "github:settylab/gl-plot3d#4666692d0e37bba36d76df8ef12ddfce1c7f9a70",
"gl-scatter3d": "github:settylab/gl-scatter3d#c557d17091c96b5c674a1c67d10db378f3ae54b3",
"gl-shader": "4.3.1",
"gl-spikes3d": "^1.0.11",
Expand Down
152 changes: 152 additions & 0 deletions test/jasmine/tests/scatter3d_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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'});

Expand Down Expand Up @@ -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);
});
});
});
6 changes: 6 additions & 0 deletions test/plot-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading