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 @@
- 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)]
7 changes: 6 additions & 1 deletion src/lib/prepare_regl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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({
Expand Down
5 changes: 5 additions & 0 deletions src/traces/scattergl/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
55 changes: 51 additions & 4 deletions test/jasmine/tests/gl2d_plot_interact_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
});
Expand All @@ -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);
Expand Down