diff --git a/draftlogs/XXXX_change.md b/draftlogs/XXXX_change.md new file mode 100644 index 00000000000..675efb90412 --- /dev/null +++ b/draftlogs/XXXX_change.md @@ -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)] diff --git a/draftlogs/XXXX_fix.md b/draftlogs/XXXX_fix.md new file mode 100644 index 00000000000..24e45e3c675 --- /dev/null +++ b/draftlogs/XXXX_fix.md @@ -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)] diff --git a/src/plots/gl3d/scene.js b/src/plots/gl3d/scene.js index e320fe9e780..1f01726f181 100644 --- a/src/plots/gl3d/scene.js +++ b/src/plots/gl3d/scene.js @@ -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, @@ -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); @@ -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) { diff --git a/src/traces/scatter3d/convert.js b/src/traces/scatter3d/convert.js index 019d362e5fe..9e14fdd36f4 100644 --- a/src/traces/scatter3d/convert.js +++ b/src/traces/scatter3d/convert.js @@ -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; @@ -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, @@ -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; diff --git a/stackgl_modules/index.js b/stackgl_modules/index.js index b812d41f949..d3475467f60 100644 --- a/stackgl_modules/index.js +++ b/stackgl_modules/index.js @@ -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], @@ -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) @@ -19013,6 +19022,7 @@ function createScene(options) { pickBufferIds.push(-1) dirty = true pickDirty = true + pickNew = true reallocPickIds() } @@ -19028,6 +19038,7 @@ function createScene(options) { pickBufferIds.pop() dirty = true pickDirty = true + pickNew = true reallocPickIds() } @@ -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) @@ -19101,7 +19120,12 @@ function createScene(options) { } scene._mouseRotating = false - for(var i=0; i selection.distance) { @@ -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) @@ -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= 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 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) @@ -19200,6 +19311,7 @@ function createScene(options) { } buf.end() } + pickDrawn = true } var nBounds = [ @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/stackgl_modules/package-lock.json b/stackgl_modules/package-lock.json index 0961819ddab..2927e14cd37 100644 --- a/stackgl_modules/package-lock.json +++ b/stackgl_modules/package-lock.json @@ -16,8 +16,8 @@ "gl-error3d": "^1.0.17", "gl-line3d": "^1.2.2", "gl-mesh3d": "^2.3.2", - "gl-plot3d": "^2.4.7", - "gl-scatter3d": "^1.4.1", + "gl-plot3d": "github:settylab/gl-plot3d#03ca2f651f73835f17e9643480301054c2f4c26e", + "gl-scatter3d": "github:settylab/gl-scatter3d#370d619a7ec1351f87989c9963a37cd4ccb06e5b", "gl-shader": "4.3.1", "gl-spikes3d": "^1.0.11", "gl-streamtube3d": "^1.4.2", @@ -1312,8 +1312,7 @@ }, "node_modules/gl-plot3d": { "version": "2.4.7", - "resolved": "https://registry.npmjs.org/gl-plot3d/-/gl-plot3d-2.4.7.tgz", - "integrity": "sha512-mLDVWrl4Dj0O0druWyHUK5l7cBQrRIJRn2oROEgrRuOgbbrLAzsREKefwMO0bA0YqkiZMFMnV5VvPA9j57X5Xg==", + "resolved": "git+ssh://git@github.com/settylab/gl-plot3d.git#03ca2f651f73835f17e9643480301054c2f4c26e", "license": "MIT", "dependencies": { "3d-view": "^2.0.0", @@ -1347,8 +1346,8 @@ }, "node_modules/gl-scatter3d": { "version": "1.4.1", - "resolved": "https://registry.npmjs.org/gl-scatter3d/-/gl-scatter3d-1.4.1.tgz", - "integrity": "sha512-v2nQWSPr9/smu2YB4erhHuUkk+8w2q59CsTPvcK72vFYWCSgU6eeTnu0UlmN634bblbTTa5ap/xrbxJGiKLyrw==", + "resolved": "git+ssh://git@github.com/settylab/gl-scatter3d.git#370d619a7ec1351f87989c9963a37cd4ccb06e5b", + "integrity": "sha512-kyGOqNcQPAiKtDbKqVYRkPFkGsCiMTL7NR3cWqC1f/CsVqWFnCBC3TyHhlMdm1c/m6Ls/naxpysYdv6Jcw/hhw==", "license": "MIT", "dependencies": { "gl-buffer": "^2.1.2", @@ -1364,8 +1363,8 @@ }, "node_modules/gl-select-static": { "version": "2.0.7", - "resolved": "https://registry.npmjs.org/gl-select-static/-/gl-select-static-2.0.7.tgz", - "integrity": "sha512-OvpYprd+ngl3liEatBTdXhSyNBjwvjMSvV2rN0KHpTU+BTi4viEETXNZXFgGXY37qARs0L28ybk3UQEW6C5Nnw==", + "resolved": "git+ssh://git@github.com/settylab/gl-select-static.git#4375784bfd498a0de58a766f4518840df183a99d", + "integrity": "sha512-+qsKdpPCUp8TpeqgqE32Eqd22U5pp9nbTikXePGWQYlapQ9oB9EU6RkxIzw3J0hugpOZ+KE4djSCIN+A7BC3Gg==", "license": "MIT", "dependencies": { "bit-twiddle": "^1.0.2", diff --git a/stackgl_modules/package.json b/stackgl_modules/package.json index a145e05bf6b..ac97e922a08 100644 --- a/stackgl_modules/package.json +++ b/stackgl_modules/package.json @@ -19,8 +19,8 @@ "gl-error3d": "^1.0.17", "gl-line3d": "^1.2.2", "gl-mesh3d": "^2.3.2", - "gl-plot3d": "^2.4.7", - "gl-scatter3d": "^1.4.1", + "gl-plot3d": "github:settylab/gl-plot3d#03ca2f651f73835f17e9643480301054c2f4c26e", + "gl-scatter3d": "github:settylab/gl-scatter3d#370d619a7ec1351f87989c9963a37cd4ccb06e5b", "gl-shader": "4.3.1", "gl-spikes3d": "^1.0.11", "gl-streamtube3d": "^1.4.2", @@ -52,6 +52,7 @@ "webpack-cli": "7.2.3" }, "overrides": { + "gl-select-static": "github:settylab/gl-select-static#4375784bfd498a0de58a766f4518840df183a99d", "is-mobile": "^5.0.0", "to-px": "1.0.1" } diff --git a/test/jasmine/tests/gl3d_hover_click_test.js b/test/jasmine/tests/gl3d_hover_click_test.js index 1499eebbf5d..e177d77a028 100644 --- a/test/jasmine/tests/gl3d_hover_click_test.js +++ b/test/jasmine/tests/gl3d_hover_click_test.js @@ -1503,3 +1503,383 @@ describe('hover on traces with (x|y|z|u|v|w)hoverformat and valuehoverformat', f }); }); }); + +function f32(n, seed) { + var a = new Float32Array(n); + var s = seed; + for(var i = 0; i < n; i++) { + s = (s * 1664525 + 1013904223) >>> 0; + a[i] = s / 4294967296; + } + return a; +} + +function cloud(n, extra, seed) { + seed = seed || 0; + var t = {type: 'scatter3d', mode: 'markers', x: f32(n, 1 + seed), y: f32(n, 2 + seed), z: f32(n, 3 + seed), + marker: {size: 10, color: 'blue', line: {width: 0}}}; + for(var k in extra) t[k] = extra[k]; + return t; +} + +var PICK_LAYOUT = {width: 600, height: 400, margin: {l: 0, r: 0, t: 0, b: 0}, showlegend: false, + scene: {camera: {eye: {x: 1.4, y: 1.2, z: 0.9}, projection: {type: 'orthographic'}}}}; +var EYE2 = {x: 0.9, y: 1.6, z: 0.8}; + +describe('Test gl3d pick pass:', function() { + var gd; + + beforeEach(function() { gd = createGraphDiv(); }); + afterEach(function() { + Plotly.purge(gd); + destroyGraphDiv(); + }); + + function glplot() { return gd._fullLayout.scene._scene.glplot; } + function draws() { return glplot().pickStats.draws; } + function layout() { return JSON.parse(JSON.stringify(PICK_LAYOUT)); } + // the points of the hover events of pointer moves over the cloud + function hoverTrail() { + var out = []; + gd.on('plotly_hover', function(e) { + // every frame with a hovered point emits the event again: keep the changes + var p = e.points[0].curveNumber + ':' + e.points[0].pointNumber; + if(out[out.length - 1] !== p) out.push(p); + }); + return out; + } + // Wait for the frame of the scene that follows a pointer event. With the pick pass in each frame, or with + // the axis spikes on, the hover events come from that frame, and a frame can take longer than a fixed delay. + const frame = () => + new Promise((resolve) => { + requestAnimationFrame(() => requestAnimationFrame(() => setTimeout(resolve, 0))); + }); + function moves(list) { + var p = Promise.resolve(); + list.forEach(function(xy) { + p = p + .then(() => { + mouseEvent('mousemove', xy[0], xy[1]); + }) + .then(delay(60)) + .then(frame); + }); + return p; + } + var PATH = [[300, 200], [280, 190], [320, 215], [305, 230]]; + + it('@gl draws no pick pass in the frames of a camera change; the first pointer query draws one', function(done) { + var n0; + Plotly.newPlot(gd, [cloud(3000)], layout()) + .then(delay(100)) + .then(function() { + glplot().pickIdleDelay = 0; + mouseEvent('mousemove', 300, 200); + n0 = draws(); + return Plotly.relayout(gd, 'scene.camera.eye', EYE2); + }) + .then(delay(150)) + .then(function() { + expect(draws()).toBe(n0, 'no pass in the frames after the camera change'); + mouseEvent('mousemove', 310, 205); + expect(draws()).toBe(n0 + 1, 'the pointer query draws it'); + mouseEvent('mousemove', 320, 210); + mouseEvent('mousemove', 290, 190); + expect(draws()).toBe(n0 + 1, 'later queries read the same buffer'); + }) + .then(done, done.fail); + }); + + it('@gl draws the pick pass once the scene has been at rest for pickIdleDelay', function(done) { + var n0; + Plotly.newPlot(gd, [cloud(3000)], layout()) + .then(delay(900)) + .then(function() { + expect(glplot().pickIdleDelay).toBe(120); + n0 = draws(); + expect(n0).toBeGreaterThan(0); + return Plotly.relayout(gd, 'scene.camera.eye', EYE2); + }) + .then(delay(900)) + .then(function() { + expect(draws()).toBe(n0 + 1); + mouseEvent('mousemove', 310, 205); + expect(draws()).toBe(n0 + 1); + }) + .then(done, done.fail); + }); + + it('@gl draws no pick pass during a drag', function(done) { + var n0; + Plotly.newPlot(gd, [cloud(3000)], layout()) + .then(delay(300)) + .then(function() { + mouseEvent('mousemove', 300, 200); + n0 = draws(); + mouseEvent('mousedown', 300, 200, {buttons: 1}); + }) + .then(delay(30)) + .then(function() { mouseEvent('mousemove', 320, 205, {buttons: 1}); }) + .then(delay(200)) + .then(function() { mouseEvent('mousemove', 340, 210, {buttons: 1}); }) + .then(delay(200)) + .then(function() { + expect(draws()).toBe(n0, 'none while the button is held, also past the idle delay'); + mouseEvent('mouseup', 340, 210); + expect(draws()).toBe(n0 + 1, 'the release queries the buffer'); + }) + .then(done, done.fail); + }); + + it('@gl gives the hover points of the eager pick pass after a camera change', function(done) { + var lazy, eager; + Plotly.newPlot(gd, [cloud(3000), cloud(3000, {marker: {size: 10, color: 'red', line: {width: 0}}}, 7)], layout()) + .then(delay(700)) + .then(function() { + glplot().pickIdleDelay = 0; + lazy = hoverTrail(); + return Plotly.relayout(gd, 'scene.camera.eye', EYE2); + }) + .then(delay(700)) + .then(function() { return moves(PATH); }) + .then(function() { + return Plotly.newPlot(gd, [cloud(3000), cloud(3000, {marker: {size: 10, color: 'red', line: {width: 0}}}, 7)], layout()); + }) + .then(delay(700)) + .then(function() { + glplot().pickMode = 'eager'; + eager = hoverTrail(); + return Plotly.relayout(gd, 'scene.camera.eye', EYE2); + }) + .then(delay(700)) + .then(function() { return moves(PATH); }) + .then(function() { + expect(lazy.length).toBeGreaterThan(1); + expect(lazy).toEqual(eager); + }) + .then(done, done.fail); + }); + + it('@gl draws no pick pass when every trace has hoverinfo skip', function(done) { + var trail; + Plotly.newPlot(gd, [cloud(3000, {hoverinfo: 'skip'}), cloud(3000, {hoverinfo: 'skip'}, 7)], layout()) + .then(delay(900)) + .then(function() { + trail = hoverTrail(); + expect(draws()).toBe(0); + return Plotly.relayout(gd, 'scene.camera.eye', EYE2); + }) + .then(delay(700)) + .then(function() { return moves(PATH); }) + .then(function() { + expect(draws()).toBe(0); + expect(trail.length).toBe(0); + // a pickable trace brings the pass back + return Plotly.restyle(gd, {hoverinfo: 'all'}, [1]); + }) + .then(delay(300)) + .then(function() { return moves(PATH); }) + .then(function() { + expect(draws()).toBeGreaterThan(0); + expect(trail.length).toBeGreaterThan(0); + trail.forEach(function(p) { expect(p.charAt(0)).toBe('1'); }); + }) + .then(done, done.fail); + }); + + it('@gl a skip trace in a mixed scene is still drawn into the pick buffer and hides the points behind it', function(done) { + // a wall of skip points in front of a pickable wall, seen along x + function wall(xv, extra) { + var t = cloud(4000, extra); + for(var i = 0; i < 4000; i++) t.x[i] = xv; + t.marker.size = 14; + return t; + } + var L = layout(); + L.scene.camera.eye = {x: 2, y: 0, z: 0}; + var trail; + Plotly.newPlot(gd, [wall(0), wall(1, {hoverinfo: 'skip'})], L) + .then(delay(900)) + .then(function() { + trail = hoverTrail(); + return moves(PATH); + }) + .then(function() { + expect(draws()).toBe(1); + expect(trail.length).toBe(0); + }) + .then(done, done.fail); + }); + + it('@gl hovermode false picks only when the button goes down: no hover events, plotly_click works', function(done) { + var ev = []; + var L = layout(); + L.scene.hovermode = false; + Plotly.newPlot(gd, [cloud(3000)], L) + .then(delay(900)) + .then(function() { + ['plotly_hover', 'plotly_unhover', 'plotly_click'].forEach(function(n) { + gd.on(n, function(e) { ev.push(n.slice(7) + (e && e.points ? ':' + e.points[0].pointNumber : '')); }); + }); + expect(glplot().pickOnPress).toBe(true); + expect(draws()).toBe(0, 'no pass ahead of a query'); + return moves(PATH); + }) + .then(function() { + expect(draws()).toBe(0, 'pointer moves query nothing'); + expect(ev).toEqual([]); + mouseEvent('mousedown', 300, 200, {buttons: 1}); + }) + .then(delay(100)) + .then(function() { + expect(draws()).toBe(1); + expect(ev.length).toBeGreaterThan(0); + ev.forEach(function(e) { expect(e.slice(0, 6)).toBe('click:'); }); + mouseEvent('mouseup', 300, 200); + }) + .then(delay(100)) + .then(function() { + ev.length = 0; + return Plotly.relayout(gd, 'scene.hovermode', 'closest'); + }) + .then(function() { + expect(glplot().pickOnPress).toBe(false); + return moves(PATH); + }) + .then(function() { + expect(ev.filter(function(e) { return e.slice(0, 6) === 'hover:'; }).length).toBeGreaterThan(0); + }) + .then(done, done.fail); + }); + + it('@gl gives no hover for a pointer event right after the first frame of a scene', function(done) { + // A pointer that rests on a scene sends an event when the canvas appears, right after the first frame. + // The pick pass in the frame was read back 1 ms later, so that event found nothing, and the pointer got no label + // until it moved. The pick pass on demand must answer the same way. + const labels = () => document.querySelectorAll('g.hovertext').length; + Plotly.newPlot(gd, Lib.extendDeep({}, mesh3dcoloringMock)) + .then(() => { + mouseEvent('mousemove', 600, 200); + }) + .then(frame) + .then(() => { + expect(labels()).toBe(0, 'no label for the event right after the first frame'); + }) + .then(delay(300)) + .then(() => { + expect(labels()).toBe(0, 'and none later without a pointer event'); + mouseEvent('mousemove', 601, 201); + }) + .then(delay(20)) + .then(frame) + .then(() => { + expect(labels()).toBe(1, 'the mesh is under that pointer: a move at rest finds it'); + }) + .then(done, done.fail); + }); + + it('@gl a hover move draws no frame when the axis spikes are off, and gives the same events', function(done) { + function run(spikes) { + var L = layout(); + ['xaxis', 'yaxis', 'zaxis'].forEach(function(a) { L.scene[a] = {showspikes: spikes}; }); + var ev = []; + var frames = 0; + return Plotly.newPlot(gd, [cloud(3000)], L) + .then(delay(900)) + .then(function() { + var o = glplot().objects[0]; + var draw = o.draw; + o.draw = function() { + frames++; + return draw.apply(this, arguments); + }; + gd.on('plotly_hover', function(e) { ev.push('hover:' + e.points[0].pointNumber); }); + gd.on('plotly_unhover', function() { ev.push('unhover'); }); + gd.on('plotly_click', function(e) { ev.push('click:' + e.points[0].pointNumber); }); + return moves(PATH); + }) + .then(function() { mouseEvent('mousedown', 305, 230, {buttons: 1}); }) + .then(delay(60)) + .then(frame) + .then(function() { mouseEvent('mouseup', 305, 230); }) + .then(delay(60)) + .then(frame) + .then(function() { return moves([[5, 5]]); }) + .then(function() { + expect(document.querySelectorAll('g.hovertext').length).toBe(0); + return {ev: ev, frames: frames}; + }); + } + var off; + run(false) + .then(function(r) { + off = r; + expect(r.frames).toBe(0, 'no frame for the hover moves, the click and the unhover'); + expect(r.ev.length).toBeGreaterThan(PATH.length); + expect(r.ev[r.ev.length - 1]).toBe('unhover'); + return run(true); + }) + .then(function(on) { + expect(on.frames).toBeGreaterThan(PATH.length - 1, 'with spikes every hover move draws a frame'); + expect(off.ev).toEqual(on.ev); + }) + .then(done, done.fail); + }); + + it('@gl a trace with more points than a pick id can tell apart is drawn but never picked, and says so', function(done) { + var convert = require('../../../src/traces/scatter3d/convert'); + var max = convert.maxPickPoints; + expect(max).toBe(16777215); + convert.maxPickPoints = 2000; + spyOn(Lib, 'warn'); + var trail; + Plotly.newPlot(gd, [cloud(3000)], layout()) + .then(delay(900)) + .then(function() { + var o = glplot().objects[0]; + expect(Lib.warn).toHaveBeenCalled(); + expect(Lib.warn.calls.argsFor(0)[0]).toMatch(/3000 points/); + expect(o.pointCount).toBe(3000); + expect(o.noPick).toBe(true); + trail = hoverTrail(); + return moves(PATH); + }) + .then(function() { + expect(trail.length).toBe(0); + expect(draws()).toBe(0); + Lib.warn.calls.reset(); + return Plotly.newPlot(gd, [cloud(2000)], layout()); + }) + .then(delay(900)) + .then(function() { + expect(Lib.warn).not.toHaveBeenCalled(); + trail = hoverTrail(); + return moves(PATH); + }) + .then(function() { + expect(trail.length).toBeGreaterThan(0); + // gl-scatter3d itself gives no hit for a cloud above 2^24 - 1 points + var o = glplot().objects[0]; + var n = o.pointCount; + expect(o.pick({id: o.pickId, value: [0, 0, 7]})).not.toBe(null); + o.pointCount = 16777216; + expect(o.pick({id: o.pickId, value: [0, 0, 7]})).toBe(null); + o.pointCount = n; + }) + .then(function() { convert.maxPickPoints = max; }, function(e) { + convert.maxPickPoints = max; + throw e; + }) + .then(done, done.fail); + }); + + it('@gl a purge with a pending idle timer does not throw', function(done) { + Plotly.newPlot(gd, [cloud(3000)], layout()) + .then(delay(50)) + .then(function() { return Plotly.relayout(gd, 'scene.camera.eye', EYE2); }) + .then(delay(30)) + .then(function() { Plotly.purge(gd); }) + .then(delay(300)) + .then(done, done.fail); + }); +});