Skip to content

Commit 384b577

Browse files
committed
Recycle global canvases properly
1 parent f7fc8cb commit 384b577

File tree

5 files changed

+43
-6
lines changed

5 files changed

+43
-6
lines changed

src/plot_api/helpers.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ exports.cleanLayout = function(layout) {
154154

155155
// clean old Camera coords
156156
var cameraposition = scene.cameraposition;
157+
157158
if(Array.isArray(cameraposition) && cameraposition[0].length === 4) {
158159
var rotation = cameraposition[0],
159160
center = cameraposition[1],

src/plot_api/plot_api.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ Plotly.plot = function(gd, data, layout, config) {
208208
key: 'pickLayer',
209209
context: false,
210210
pick: true
211-
}]);
211+
}], function(d) { return d.key; });
212212

213213
fullLayout._glcanvas.enter().append('canvas')
214214
.attr('class', function(d) {
@@ -225,8 +225,6 @@ Plotly.plot = function(gd, data, layout, config) {
225225
})
226226
.attr('width', fullLayout.width)
227227
.attr('height', fullLayout.height);
228-
229-
fullLayout._glcanvas.exit().remove();
230228
}
231229

232230
return Lib.syncOrAsync([

src/plots/plots.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,16 @@ plots.cleanPlot = function(newFullData, newFullLayout, oldFullData, oldFullLayou
609609

610610
var hasPaper = !!oldFullLayout._paper;
611611
var hasInfoLayer = !!oldFullLayout._infolayer;
612+
var hadGl = oldFullLayout._has && oldFullLayout._has('gl');
613+
var hasGl = newFullLayout._has && newFullLayout._has('gl');
614+
615+
if(hadGl && !hasGl) {
616+
if(oldFullLayout._glcontainer !== undefined) {
617+
oldFullLayout._glcontainer.selectAll('.gl-canvas').data([]).exit().remove();
618+
oldFullLayout._glcontainer.remove();
619+
oldFullLayout._glcanvas = null;
620+
}
621+
}
612622

613623
oldLoop:
614624
for(i = 0; i < oldFullData.length; i++) {
@@ -1322,7 +1332,11 @@ plots.purge = function(gd) {
13221332
// a new plot, and may have been set outside of our scope.
13231333

13241334
var fullLayout = gd._fullLayout || {};
1325-
if(fullLayout._glcontainer !== undefined) fullLayout._glcontainer.remove();
1335+
if(fullLayout._glcontainer !== undefined) {
1336+
fullLayout._glcontainer.selectAll('.gl-canvas').data([]).exit().remove();
1337+
fullLayout._glcontainer.remove();
1338+
fullLayout._glcanvas = null;
1339+
}
13261340
if(fullLayout._geocontainer !== undefined) fullLayout._geocontainer.remove();
13271341

13281342
// remove modebar

src/traces/parcoords/parcoords.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ module.exports = function(root, svg, parcoordsLineLayers, styledData, layout, ca
294294
.each(function(d) {
295295
// FIXME: figure out how to handle multiple instances
296296
d.viewModel = vm[0];
297-
d.model = vm[0].model;
297+
d.model = d.viewModel ? d.viewModel.model : null;
298298
});
299299

300300
var tweakables = {renderers: [], dimensions: []};

test/jasmine/tests/gl_plot_interact_test.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1302,11 +1302,35 @@ describe('Test gl plot side effects', function() {
13021302

13031303
return Plotly.deleteTraces(gd, [0]);
13041304
}).then(function() {
1305-
countCanvases(3);
1305+
countCanvases(0);
13061306

13071307
return Plotly.purge(gd);
13081308
}).then(done);
13091309
});
1310+
1311+
it('should be able to switch trace type', function(done) {
1312+
Plotly.newPlot(gd, [{
1313+
type: 'parcoords',
1314+
x: [1, 2, 3],
1315+
y: [2, 1, 2],
1316+
dimensions: [
1317+
{
1318+
constraintrange: [200, 700],
1319+
label: 'Block height',
1320+
values: [321, 534, 542, 674, 31, 674, 124, 246, 456, 743]
1321+
}
1322+
]
1323+
}])
1324+
.then(function() {
1325+
expect(d3.selectAll('canvas').size()).toEqual(3);
1326+
1327+
return Plotly.restyle(gd, 'type', 'scatter');
1328+
})
1329+
.then(function() {
1330+
expect(d3.selectAll('canvas').size()).toEqual(0);
1331+
})
1332+
.then(done);
1333+
});
13101334
});
13111335

13121336
describe('Test gl2d interactions', function() {

0 commit comments

Comments
 (0)