From bae041e7abd9af0a41e9da8364cc6e9493f10ec0 Mon Sep 17 00:00:00 2001 From: Dominik Date: Fri, 4 Dec 2026 12:10:59 -0800 Subject: [PATCH] Create the WebGL context of the focus layer only for selections Each graph with a regl trace created two WebGL contexts. In scattergl and splom graphs, the focus layer shows only the selected points. Most graphs never select, but the browser counts the second context against its limit of about 16 for each page. prepareRegl now creates the context of the focus layer on request, or if the graph has parcoords. scattergl sends the request when a selection drag mode starts, or when the first selection draw comes. The selected points appear on the focus layer as before. --- draftlogs/XXXX_change.md | 1 + src/lib/prepare_regl.js | 7 ++- src/traces/scattergl/plot.js | 5 ++ test/jasmine/tests/gl2d_plot_interact_test.js | 55 +++++++++++++++++-- 4 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 draftlogs/XXXX_change.md diff --git a/draftlogs/XXXX_change.md b/draftlogs/XXXX_change.md new file mode 100644 index 00000000000..8e469f2cf8f --- /dev/null +++ b/draftlogs/XXXX_change.md @@ -0,0 +1 @@ +- Create the WebGL context of the `scattergl` and `splom` focus layer only when a selection starts (a selection drag mode or selected points) [[#XXXX](https://github.com/plotly/plotly.js/pull/XXXX)] diff --git a/src/lib/prepare_regl.js b/src/lib/prepare_regl.js index d81c7f83a3a..c66c9add1bc 100644 --- a/src/lib/prepare_regl.js +++ b/src/lib/prepare_regl.js @@ -15,10 +15,13 @@ var createRegl = require('@plotly/regl'); * * @param {DOM node or object} gd : graph div object * @param {array} extensions : list of extension to pass to createRegl + * @param {object} reglPrecompiled : precompiled regl code for strict bundles + * @param {boolean} needsFocusLayer : if true, also create the regl instance of the focus layer. + * A graph with parcoords always gets that instance. * * @return {boolean} true if all createRegl calls succeeded, false otherwise */ -module.exports = function prepareRegl(gd, extensions, reglPrecompiled) { +module.exports = function prepareRegl(gd, extensions, reglPrecompiled, needsFocusLayer) { var fullLayout = gd._fullLayout; var success = true; @@ -29,6 +32,8 @@ module.exports = function prepareRegl(gd, extensions, reglPrecompiled) { } // only parcoords needs pick layer if(d.pick && !fullLayout._has('parcoords')) return; + // Browsers limit the WebGL contexts of a page, so the focus layer gets a context only on request + if (d.key === 'focusLayer' && !needsFocusLayer && !fullLayout._has('parcoords')) return; try { d.regl = createRegl({ diff --git a/src/traces/scattergl/plot.js b/src/traces/scattergl/plot.js index d271f8dda8a..7c0e259284f 100644 --- a/src/traces/scattergl/plot.js +++ b/src/traces/scattergl/plot.js @@ -319,6 +319,11 @@ var exports = module.exports = function plot(gd, subplot, cdata) { if(isSelectMode) { // create scatter instance by cloning scatter2d if(!scene.select2d) { + // The focus layer shows the selected points and gets its WebGL context here + if (!prepareRegl(gd, ['ANGLE_instanced_arrays', 'OES_element_index_uint'], reglPrecompiled, true)) { + scene.init(); + return; + } scene.select2d = createScatter(fullLayout._glcanvas.data()[1].regl); } diff --git a/test/jasmine/tests/gl2d_plot_interact_test.js b/test/jasmine/tests/gl2d_plot_interact_test.js index 1ee3b711a77..b825c78acf5 100644 --- a/test/jasmine/tests/gl2d_plot_interact_test.js +++ b/test/jasmine/tests/gl2d_plot_interact_test.js @@ -228,6 +228,18 @@ describe('Test gl plot side effects', function() { expect((eventData || {}).event).toBeDefined(); expect((eventData || {}).layer).toBe('contextLayer'); }) + .then(function() { + return new Promise(function(resolve, reject) { + gd.once('plotly_webglcontextlost', reject); + setTimeout(resolve, 10); + _trigger('focus'); + }); + }) + .then(function(eventData) { + // The focus canvas gets its WebGL context and its listener with the first selection + expect(eventData).toBeUndefined(); + return Plotly.relayout(gd, 'dragmode', 'select'); + }) .then(function() { return new Promise(function(resolve, reject) { gd.once('plotly_webglcontextlost', resolve); @@ -330,15 +342,50 @@ describe('Test gl plot side effects', function() { .then(done, done.fail); }); - it('@gl should create two WebGL contexts per graph', function(done) { + it('@gl should create one WebGL context per graph, and a second one when a selection is drawn', function(done) { var fig = Lib.extendDeep({}, require('../../image/mocks/gl2d_stacked_subplots.json')); - Plotly.newPlot(gd, fig).then(function() { + function countContexts() { var cnt = 0; d3Select(gd).selectAll('canvas').each(function(d) { if(d.regl) cnt++; }); - expect(cnt).toBe(2); + return cnt; + } + + Plotly.newPlot(gd, fig).then(function() { + expect(countContexts()).toBe(1); + expect(gd._fullLayout._glcanvas.data()[0].regl).toBeDefined(); + expect(gd._fullLayout._glcanvas.data()[1].regl).toBeUndefined(); + return Plotly.relayout(gd, 'dragmode', 'pan'); + }) + .then(function() { + expect(countContexts()).toBe(1); + return Plotly.relayout(gd, 'dragmode', 'lasso'); + }) + .then(function() { + // The focus layer shows the selected points + expect(countContexts()).toBe(2); + expect(gd._fullLayout._glcanvas.data()[1].regl).toBeDefined(); + return Plotly.relayout(gd, 'dragmode', 'zoom'); + }) + .then(function() { + expect(countContexts()).toBe(2); + }) + .then(done, done.fail); + }); + + it('@gl should create the focus layer context for traces with selectedpoints', function(done) { + Plotly.newPlot(gd, [{ + type: 'scattergl', + mode: 'markers', + y: [1, 2, 1], + selectedpoints: [1] + }]) + .then(function() { + expect(gd._fullLayout._glcanvas.data()[0].regl).toBeDefined(); + expect(gd._fullLayout._glcanvas.data()[1].regl).toBeDefined(); + expect(gd._fullLayout._glcanvas.data()[2].regl).toBeUndefined(); }) .then(done, done.fail); }); @@ -350,7 +397,7 @@ describe('Test gl plot side effects', function() { }, { type: 'scattergl', y: [2, 1, 2] - }]) + }], {dragmode: 'select'}) .then(function() { expect(gd._fullLayout._glcanvas).toBeDefined(); expect(gd._fullLayout._glcanvas.size()).toBe(3);