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_change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Draw the gl3d pick pass when a pointer event needs it instead of in every frame that moves the camera, skip it when no trace of the scene can be picked, draw no frame for a hover that changes nothing in the picture, and make `scene.hovermode: false` pick only on a button press without emitting hover events [[#XXXX](https://github.com/plotly/plotly.js/pull/XXXX)]
1 change: 1 addition & 0 deletions draftlogs/XXXX_fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix hover and click on `scatter3d` traces with more than 16,777,215 points naming other points: such a trace is drawn but not picked, with a console warning [[#XXXX](https://github.com/plotly/plotly.js/pull/XXXX)]
10 changes: 8 additions & 2 deletions src/plots/gl3d/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ proto.prepareOptions = function() {
axes: scene.axesOptions,
spikes: scene.spikeOptions,
pickRadius: 10,
// Nothing queries a static scene, so no pick pass runs before a query
pickIdleDelay: scene.staticMode ? 0 : 120,
snapToData: true,
autoScale: true,
autoBounds: false,
Expand Down Expand Up @@ -456,13 +458,14 @@ proto.render = function() {
pointData.bbox = bbox[0];
}

const hoverOn = !!scene.fullSceneLayout.hovermode;
if(selection.distance < 5 && (selection.buttons || tabletmode)) {
gd.emit('plotly_click', eventData);
} else {
} else if(hoverOn) {
gd.emit('plotly_hover', eventData);
}

this.oldEventData = eventData;
this.oldEventData = hoverOn ? eventData : undefined;
} else {
Fx.loneUnhover(svgContainer);
if(this.oldEventData) gd.emit('plotly_unhover', this.oldEventData);
Expand Down Expand Up @@ -1054,6 +1057,9 @@ proto.updateFx = function(dragmode, hovermode) {

// to put dragmode and hovermode on the same grounds from relayout
scene.fullSceneLayout.hovermode = hovermode;

// Without hover, only a button press queries the pick buffer, for plotly_click
if (scene.glplot) scene.glplot.pickOnPress = !hovermode;
};

function flipPixels(pixels, w, h) {
Expand Down
25 changes: 25 additions & 0 deletions src/traces/scatter3d/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,27 @@ function formatParam(paramIn, len, calculate, dflt, extraFn) {
return paramOut;
}

/**
* Tell if hover and click can never select a point of the trace.
*
* The pick buffer holds 24 bits of the point index. The ids of a longer trace
* name other points, so such a trace gets no hover and no click. The function logs a warning for it.
*
* @param data - the full trace
* @param len - the number of points
* @returns true for `hoverinfo: 'skip'` and for a trace above `maxPickPoints`
*/
function neverPicked(data, len) {
if (data.hoverinfo === 'skip') return true;
const max = createLineWithMarkers.maxPickPoints;
if (len <= max) return false;
Lib.warn(
`scatter3d trace ${data.index} has ${len} points. Hover and click cannot tell more than ${max} apart, ` +
'so the trace gets neither. Split the trace, or set hoverinfo to *skip*.'
);
return true;
}

function convertPlotlyOptions(scene, data) {
var points = [];
var sceneLayout = scene.fullSceneLayout;
Expand Down Expand Up @@ -421,6 +442,7 @@ proto.update = function (data) {
glyph: options.scatterMarker,
opacity: scatterOpacity,
orthographic: true,
pick: !neverPicked(data, (data.x || []).length),
lineWidth: options.scatterLineWidth,
lineColor: options.scatterLineColor,
project: options.project,
Expand Down Expand Up @@ -547,4 +569,7 @@ function createLineWithMarkers(scene, data) {
return plot;
}

// The pick ids of one trace: 0 to 2^24 - 2. The last value means no point.
createLineWithMarkers.maxPickPoints = 16777215;

module.exports = createLineWithMarkers;
157 changes: 152 additions & 5 deletions stackgl_modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18881,6 +18881,14 @@ function createScene(options) {
mouseListener: null,
_stopped: false,

// The pick pass. 'lazy': drawn when a pointer event queries it, or pickIdleDelay ms after the last
// frame that changed it (0: never ahead of the query). 'eager': drawn in every frame that changed it (upstream).
pickMode: options.pickMode || 'lazy',
pickIdleDelay: options.pickIdleDelay === undefined ? 120 : options.pickIdleDelay,
// Query the pick buffers only when the main button goes down (hover is off): pointer moves draw nothing
pickOnPress: false,
pickStats: {draws: 0},

getAspectratio: function() {
return {
x: this.aspect[0],
Expand Down Expand Up @@ -18982,6 +18990,7 @@ function createScene(options) {
}
//Create new pick buffer
var nbuffer = createSelect(gl, viewShape)
pickDrawn = false
pickBufferIds[i] = numPick
pickBuffers.push(nbuffer)
pickBufferCount.push(pickCount)
Expand Down Expand Up @@ -19013,6 +19022,7 @@ function createScene(options) {
pickBufferIds.push(-1)
dirty = true
pickDirty = true
pickNew = true
reallocPickIds()
}

Expand All @@ -19028,6 +19038,7 @@ function createScene(options) {
pickBufferIds.pop()
dirty = true
pickDirty = true
pickNew = true
reallocPickIds()
}

Expand All @@ -19037,6 +19048,14 @@ function createScene(options) {
}

scene._stopped = true
if(pickTimer) {
clearTimeout(pickTimer)
pickTimer = null
}
if(pickWait) {
clearTimeout(pickWait)
pickWait = null
}

window.removeEventListener('resize', resizeListener)
canvas.removeEventListener('webglcontextlost', checkContextLoss)
Expand Down Expand Up @@ -19101,7 +19120,12 @@ function createScene(options) {
}
scene._mouseRotating = false

for(var i=0; i<numPick; ++i) {
var pickNow = !scene.pickOnPress || ((buttons & 1) && !(scene._prevButtons & 1))
if(pickNow && scene.pickMode === 'lazy') {
ensurePick()
}

for(var i=0; i<numPick && pickNow && !pickEmpty && pickDrawn; ++i) {
var result = pickBuffers[i].query(x, pickShape[1] - y - 1, scene.pickRadius)
if(result) {
if(result.distance > selection.distance) {
Expand Down Expand Up @@ -19129,17 +19153,27 @@ function createScene(options) {
}
}

// A hover that changes nothing in the picture needs no frame: the axis spikes are off and the
// hovered cloud is not drawn larger (highlightScale 1, as plotly sets it). The render callback alone
// then gives the hover label and events, as it does from the frame.
var e = spikes.enabled
var still = !e[0] && !e[1] && !e[2] && !!scene.onrender && pickFrameSeen &&
(!prevObj || prevObj.highlightScale === 1) &&
(!selection.object || selection.object.highlightScale === 1)
if(prevObj && prevObj !== selection.object) {
if(prevObj.highlight) {
prevObj.highlight(null)
}
dirty = true
dirty = dirty || !still
}
if(selection.object) {
if(selection.object.highlight) {
selection.object.highlight(selection.data)
}
dirty = true
dirty = dirty || !still
}
if(still && !dirty && (prevObj || selection.object)) {
scene.onrender()
}

change = change || (selection.object !== prevObj)
Expand Down Expand Up @@ -19170,12 +19204,89 @@ function createScene(options) {

canvas.addEventListener('webglcontextlost', checkContextLoss)

var pickTimer = null
var pickFrameSeen = false
var pickEmpty = false
// False until a pick pass has drawn every pick buffer. A new buffer holds memory from the pool, not a picture,
// so no query reads the buffers before that.
var pickDrawn = false
// The pick pass of a frame was read back 1 ms after the frame, so a pointer event right after a frame saw the
// picture of the frame before. A pass on demand waits as long: pickWait is set for 1 ms after a frame that changed it.
var pickWait = null
// True from a change of the objects (add, remove) to the next frame. Only that frame starts the wait: after a frame
// that moves the camera, the pass on demand answers at once, where the frame pass gave the picture of the frame before.
var pickNew = true

// True if some object that is drawn into a pick buffer can be picked
function anyPickable() {
for(var i=0; i<objects.length; ++i) {
if(pickBufferIds[i] >= 0 && objects[i].drawPick && !objects[i].noPick) {
return true
}
}
return false
}

// Draw the pick buffers and read them back now, if they are out of date.
// Not while a change waits for its frame (an object added, removed or updated, a new size): a pass on demand
// shows what the last frame drew, as the pass in the frame did. That frame starts the idle timer again.
function ensurePick() {
if(pickTimer) {
clearTimeout(pickTimer)
pickTimer = null
}
if(!pickDirty || !pickFrameSeen || dirty || pickWait || scene._stopped || scene.contextLost) {
return
}
pickDirty = false
renderPick()
// the pick pass draws at pixel ratio 1; objects read their own ratio when a query follows (mesh pick)
for(var i=0; i<objects.length; ++i) {
objects[i].pixelRatio = scene.pixelRatio
}
if(scene.contextLost || pickEmpty) {
return
}
for(var j=0; j<pickBuffers.length; ++j) {
pickBuffers[j].sync()
}
}
scene.ensurePick = ensurePick

// The pick pass ahead of the first query, once the scene is at rest
function schedulePick() {
if(pickTimer) {
clearTimeout(pickTimer)
pickTimer = null
}
if(!(scene.pickIdleDelay > 0) || scene.pickOnPress) {
return
}
pickTimer = setTimeout(function() {
pickTimer = null
if(scene._prevButtons) {
// nothing is queried while a button is held: look again later
schedulePick()
} else {
ensurePick()
}
}, scene.pickIdleDelay)
}

//Render the scene for mouse picking
function renderPick() {
if(checkContextLoss()) {
return
}

// Nothing can be picked (every trace has hoverinfo 'skip'): no pass, and no query reads the buffers
pickEmpty = !anyPickable()
if(pickEmpty) {
return
}

scene.pickStats.draws++

gl.colorMask(true, true, true, true)
gl.depthMask(true)
gl.disable(gl.BLEND)
Expand All @@ -19200,6 +19311,7 @@ function createScene(options) {
}
buf.end()
}
pickDrawn = true
}

var nBounds = [
Expand Down Expand Up @@ -19340,9 +19452,24 @@ function createScene(options) {
}

//If state changed, then redraw pick buffers
pickFrameSeen = true
if(pickDirty) {
pickDirty = false
renderPick()
if(scene.pickMode === 'lazy') {
// Not in this frame: a pointer event that queries the buffer draws it, or the idle timer does
schedulePick()
if(pickNew) {
if(pickWait) {
clearTimeout(pickWait)
}
pickWait = setTimeout(function() {
pickWait = null
}, 1)
}
pickNew = false
} else {
pickDirty = false
renderPick()
}
}

//Recalculate pixel data
Expand Down Expand Up @@ -20169,6 +20296,11 @@ proto.pick = function(selected) {
if(selected.id !== this.pickId) {
return null
}
// A cloud that is never picked gives no hit. Nor does one with more points than the 24 bits of the point index
// in the pick buffer can tell apart (the last value means none): its ids would name other points
if(this.noPick || this.pointCount > 16777215) {
return null
}
var x = selected.value[2] + (selected.value[1]<<8) + (selected.value[0]<<16)
if(x >= this.pointCount || x < 0) {
return null
Expand Down Expand Up @@ -20311,6 +20443,9 @@ proto.update = function(options) {
//Set dirty flag
this.dirty = true

// A cloud that is never picked (hoverinfo 'skip'). The scene draws no pick pass when no object can be picked.
if('pick' in options) this.noPick = options.pick === false

//Create new buffers
var points = options.position

Expand Down Expand Up @@ -20870,6 +21005,18 @@ proto.query = function(x, y, radius) {
Math.sqrt(closest[2]))
}

// Read the buffer back now (a pick pass drawn on demand is queried at once)
proto.sync = function() {
if(!this.gl) {
return
}
if(this._readTimeout) {
clearTimeout(this._readTimeout)
}
this._readCallback()
this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, null)
}

proto.dispose = function() {
if(!this.gl) {
return
Expand Down
15 changes: 7 additions & 8 deletions stackgl_modules/package-lock.json

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

Loading
Loading