Skip to content
Closed
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/8218_fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix shared WebGL instances so regl traces receive necessary extensions [[#8218](https://github.com/plotly/plotly.js/pull/8218)], with thanks to @sanketpatel32 for the contribution!
27 changes: 24 additions & 3 deletions src/lib/prepare_regl.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ var showNoWebGlMsg = require('./show_no_webgl_msg');
// regl and all its bytes.
var createRegl = require('@plotly/regl');

// Request the extension union used by regl trace modules because any module can create the shared instance first.
const knownReglExtensions = [
'ANGLE_instanced_arrays',
'OES_element_index_uint'
];

function hasExtensions(regl, extensions) {
for(const extension of extensions) {
if(!regl.hasExtension(extension)) return false;
}
return true;
}

/**
* Idempotent version of createRegl. Create regl instances
* in the correct canvases with the correct attributes and
Expand All @@ -18,13 +31,18 @@ var createRegl = require('@plotly/regl');
*
* @return {boolean} true if all createRegl calls succeeded, false otherwise
*/
module.exports = function prepareRegl(gd, extensions, reglPrecompiled) {
function prepareRegl(gd, extensions, reglPrecompiled) {
var fullLayout = gd._fullLayout;
const requiredExtensions = extensions || [];
const optionalExtensions = knownReglExtensions.filter(extension =>
requiredExtensions.every(requiredExtension => requiredExtension.toLowerCase() !== extension.toLowerCase())
);
var success = true;

fullLayout._glcanvas.each(function(d) {
if(d.regl) {
d.regl.preloadCachedCode(reglPrecompiled);
if(!hasExtensions(d.regl, requiredExtensions)) success = false;
return;
}
// only parcoords needs pick layer
Expand All @@ -38,7 +56,8 @@ module.exports = function prepareRegl(gd, extensions, reglPrecompiled) {
preserveDrawingBuffer: true
},
pixelRatio: gd._context.plotGlPixelRatio || global.devicePixelRatio,
extensions: extensions || [],
extensions: requiredExtensions,
optionalExtensions: optionalExtensions,
cachedCode: reglPrecompiled || {}
});
} catch(e) {
Expand All @@ -63,4 +82,6 @@ module.exports = function prepareRegl(gd, extensions, reglPrecompiled) {
showNoWebGlMsg({container: fullLayout._glcontainer.node()});
}
return success;
};
}

module.exports = prepareRegl;
72 changes: 72 additions & 0 deletions test/jasmine/tests/scattergl_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,78 @@ describe('end-to-end scattergl tests', function() {
.then(done, done.fail);
});

function expectScatterRegionDrawn() {
const canvas = gd.querySelector('.gl-canvas-context');
const x = Math.floor(canvas.width * 0.6);
const width = canvas.width - x;
const pixels = readPixel(canvas, x, 0, width, canvas.height);
let sum = 0;

for(let i = 0; i < pixels.length; i++) {
sum += pixels[i];
}

expect(sum).toBeGreaterThan(0);
}

function parcoordsTrace() {
return {
type: 'parcoords',
domain: {x: [0, 0.4]},
dimensions: [
{values: [0, 1]},
{values: [1, 0]}
]
};
}

function scatterglLineTrace(extra) {
return Object.assign({
type: 'scattergl',
mode: 'lines',
x: [0, 1],
y: [0, 1],
line: {color: 'red', width: 4}
}, extra);
}

const parcoordsScatterLayout = {
width: 400,
height: 300,
margin: {l: 0, r: 0, t: 0, b: 0},
xaxis: {domain: [0.6, 1], range: [0, 1]},
yaxis: {range: [0, 1]}
};

it('@gl should switch from parcoords to scattergl lines', function(done) {
Plotly.react(gd, [parcoordsTrace()], parcoordsScatterLayout)
.then(() => Plotly.react(gd, [scatterglLineTrace()], parcoordsScatterLayout))
.then(() => {
expect(gd._fullLayout._plots.xy._scene.line2d).toBeDefined();
expectScatterRegionDrawn();
})
.then(done, done.fail);
});

it('@gl should draw scattergl lines, fill and error bars after parcoords', function(done) {
Plotly.newPlot(gd, [
parcoordsTrace(),
scatterglLineTrace({
fill: 'tozeroy',
error_y: {value: 0.1}
})
], parcoordsScatterLayout)
.then(() => {
const scene = gd._fullLayout._plots.xy._scene;

expect(scene.line2d).toBeDefined();
expect(scene.fill2d).toBeDefined();
expect(scene.error2d).toBeDefined();
expectScatterRegionDrawn();
})
.then(done, done.fail);
});

it('@gl should be able to toggle trace with different modes', function(done) {
Plotly.newPlot(gd, [{
// a trace with all regl2d objects
Expand Down