From af538fec50f8e212bde2cdfbfcf733bad5d7c771 Mon Sep 17 00:00:00 2001 From: Dominik Date: Thu, 3 Dec 2026 23:12:00 -0800 Subject: [PATCH 1/7] Draw the pick pass on demand The scene drew every pickable object into the pick buffers, and read them back, in every frame in which the camera moved, although the buffers are only queried by pointer events with no button held. - pickMode 'lazy' (default): draw the pass when a pointer event is about to query it, or pickIdleDelay ms after the last frame that changed it (never while a button is held), and read it back at once. 'eager' is the previous behaviour. - Draw no pass when no object can be picked (objects flagged noPick). - pickOnPress: query only when the main button goes down. - A hover that changes nothing in the picture (spikes off, highlightScale 1) calls onrender without a frame. Needs gl-select-static with sync(). --- scene.js | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 104 insertions(+), 5 deletions(-) diff --git a/scene.js b/scene.js index 06c3565..e4734b0 100644 --- a/scene.js +++ b/scene.js @@ -192,6 +192,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], @@ -348,6 +356,10 @@ function createScene(options) { } scene._stopped = true + if(pickTimer) { + clearTimeout(pickTimer) + pickTimer = null + } window.removeEventListener('resize', resizeListener) canvas.removeEventListener('webglcontextlost', checkContextLoss) @@ -412,7 +424,12 @@ function createScene(options) { } scene._mouseRotating = false - for(var i=0; i selection.distance) { @@ -440,17 +457,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) @@ -481,12 +508,78 @@ function createScene(options) { canvas.addEventListener('webglcontextlost', checkContextLoss) + var pickTimer = null + var pickFrameSeen = false + var pickEmpty = false + + // True if some object that is drawn into a pick buffer can be picked + function anyPickable() { + for(var i=0; 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 + function ensurePick() { + if(pickTimer) { + clearTimeout(pickTimer) + pickTimer = null + } + if(!pickDirty || !pickFrameSeen || 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 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) @@ -651,9 +744,15 @@ 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() + } else { + pickDirty = false + renderPick() + } } //Recalculate pixel data From 368b8188fda8b6be89af42fadda930b3a915546a Mon Sep 17 00:00:00 2001 From: Dominik Date: Sat, 5 Dec 2026 01:08:24 -0800 Subject: [PATCH 2/7] Draw no pick pass on demand before the frame that draws a change A pointer event between scene.add and the next frame drew the pick pass with the new object, so the scene gave a hit for an object that no frame had drawn, with the camera and bounds of the frame before. The pass in the frame never did that. ensurePick now returns while the scene waits for a frame. --- scene.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/scene.js b/scene.js index e4734b0..35c9783 100644 --- a/scene.js +++ b/scene.js @@ -522,13 +522,15 @@ function createScene(options) { return false } - // Draw the pick buffers and read them back now, if they are out of date + // 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 || scene._stopped || scene.contextLost) { + if(!pickDirty || !pickFrameSeen || dirty || scene._stopped || scene.contextLost) { return } pickDirty = false From d054d4cfc56c35df9fd603344b48d5a5e0877422 Mon Sep 17 00:00:00 2001 From: Dominik Date: Sat, 5 Dec 2026 01:12:12 -0800 Subject: [PATCH 3/7] Query no pick buffer before a pick pass has drawn it A new select buffer takes its memory from the pool and does not fill it. With the pick pass on demand, a pointer event can come before the first pass, for example while a change waits for its frame. The query then read the picture of an earlier scene and could give a hit. The scene now queries the buffers only after a pass has drawn all of them. --- scene.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/scene.js b/scene.js index 35c9783..187652c 100644 --- a/scene.js +++ b/scene.js @@ -301,6 +301,7 @@ function createScene(options) { } //Create new pick buffer var nbuffer = createSelect(gl, viewShape) + pickDrawn = false pickBufferIds[i] = numPick pickBuffers.push(nbuffer) pickBufferCount.push(pickCount) @@ -429,7 +430,7 @@ function createScene(options) { ensurePick() } - for(var i=0; i selection.distance) { @@ -511,6 +512,9 @@ function createScene(options) { 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 // True if some object that is drawn into a pick buffer can be picked function anyPickable() { @@ -606,6 +610,7 @@ function createScene(options) { } buf.end() } + pickDrawn = true } var nBounds = [ From ee198b2b2faba4c02396a6ad54889af622b25f18 Mon Sep 17 00:00:00 2001 From: Dominik Date: Sat, 5 Dec 2026 01:51:05 -0800 Subject: [PATCH 4/7] Give a pick pass on demand 1 ms after a frame, as the read-back of the frame pass did The pick pass of a frame was read back 1 ms later, so a pointer event that came right after a frame found the picture of the frame before, and nothing after the first frame of a scene. The pass on demand answered such an event at once. A pointer that rests on a scene when its canvas appears then got a hover label that it did not get before, and later events at the same place are not reported, so the label stayed. The pass on demand now waits the same 1 ms. --- scene.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/scene.js b/scene.js index 187652c..0daf588 100644 --- a/scene.js +++ b/scene.js @@ -361,6 +361,10 @@ function createScene(options) { clearTimeout(pickTimer) pickTimer = null } + if(pickWait) { + clearTimeout(pickWait) + pickWait = null + } window.removeEventListener('resize', resizeListener) canvas.removeEventListener('webglcontextlost', checkContextLoss) @@ -515,6 +519,9 @@ function createScene(options) { // 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 if some object that is drawn into a pick buffer can be picked function anyPickable() { @@ -534,7 +541,7 @@ function createScene(options) { clearTimeout(pickTimer) pickTimer = null } - if(!pickDirty || !pickFrameSeen || dirty || scene._stopped || scene.contextLost) { + if(!pickDirty || !pickFrameSeen || dirty || pickWait || scene._stopped || scene.contextLost) { return } pickDirty = false @@ -756,6 +763,12 @@ function createScene(options) { if(scene.pickMode === 'lazy') { // Not in this frame: a pointer event that queries the buffer draws it, or the idle timer does schedulePick() + if(pickWait) { + clearTimeout(pickWait) + } + pickWait = setTimeout(function() { + pickWait = null + }, 1) } else { pickDirty = false renderPick() From e68f3db7da0e319755d8df6aec1c0b6a0e83385b Mon Sep 17 00:00:00 2001 From: Dominik Date: Sat, 5 Dec 2026 01:52:25 -0800 Subject: [PATCH 5/7] Start the 1 ms wait of the pick pass once, not again in each frame A scene that draws frame after frame started the timer again before it ran out when the page was busy, so the wait could last as long as the frames. One timer runs at a time now. --- scene.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/scene.js b/scene.js index 0daf588..482b3ef 100644 --- a/scene.js +++ b/scene.js @@ -763,12 +763,12 @@ function createScene(options) { if(scene.pickMode === 'lazy') { // Not in this frame: a pointer event that queries the buffer draws it, or the idle timer does schedulePick() - if(pickWait) { - clearTimeout(pickWait) + // (one timer at a time: a scene that draws frame after frame must not hold the wait for good) + if(!pickWait) { + pickWait = setTimeout(function() { + pickWait = null + }, 1) } - pickWait = setTimeout(function() { - pickWait = null - }, 1) } else { pickDirty = false renderPick() From 0e93399b407ca2e73c0bad5456e20cb11358e95b Mon Sep 17 00:00:00 2001 From: Dominik Date: Sat, 5 Dec 2026 02:00:53 -0800 Subject: [PATCH 6/7] Start the 1 ms wait of the pick pass only after a change of the objects The wait started after every frame that left the pick buffers out of date. A scene that draws frame after frame, as it does while the camera settles after a plot, then refused a pass on demand for 1 ms or more in each frame, and a pointer event in that time found nothing. The frame pass never did that: it gave the picture of the frame before. Only the first frame after scene.add or scene.remove starts the wait now. --- scene.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/scene.js b/scene.js index 482b3ef..eaf0051 100644 --- a/scene.js +++ b/scene.js @@ -333,6 +333,7 @@ function createScene(options) { pickBufferIds.push(-1) dirty = true pickDirty = true + pickNew = true reallocPickIds() } @@ -348,6 +349,7 @@ function createScene(options) { pickBufferIds.pop() dirty = true pickDirty = true + pickNew = true reallocPickIds() } @@ -522,6 +524,9 @@ function createScene(options) { // 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() { @@ -763,12 +768,12 @@ function createScene(options) { if(scene.pickMode === 'lazy') { // Not in this frame: a pointer event that queries the buffer draws it, or the idle timer does schedulePick() - // (one timer at a time: a scene that draws frame after frame must not hold the wait for good) - if(!pickWait) { + if(pickNew && !pickWait) { pickWait = setTimeout(function() { pickWait = null }, 1) } + pickNew = false } else { pickDirty = false renderPick() From 03ca2f651f73835f17e9643480301054c2f4c26e Mon Sep 17 00:00:00 2001 From: Dominik Date: Sat, 5 Dec 2026 02:01:09 -0800 Subject: [PATCH 7/7] Count the 1 ms wait of the pick pass from the last frame after a change of the objects plotly draws an empty first frame and then the frame with the traces in one task. The wait ran from the first of the two, so it could end before the read-back of the second would have. It runs from the last such frame now; only a change of the objects starts it, so frames of a moving camera cannot hold it. --- scene.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/scene.js b/scene.js index eaf0051..6e62359 100644 --- a/scene.js +++ b/scene.js @@ -768,7 +768,10 @@ function createScene(options) { 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 && !pickWait) { + if(pickNew) { + if(pickWait) { + clearTimeout(pickWait) + } pickWait = setTimeout(function() { pickWait = null }, 1)