Skip to content

Commit 469e4cd

Browse files
committed
add regl_utils lib module
... exposing prepare and clear methods
1 parent c4c1514 commit 469e4cd

File tree

5 files changed

+60
-50
lines changed

5 files changed

+60
-50
lines changed

src/lib/regl_utils.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Copyright 2012-2018, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
'use strict';
10+
11+
var createRegl = require('regl');
12+
13+
/**
14+
* Idempotent version of createRegl. Create regl instances
15+
* in the correct canvases with the correct attributes and
16+
* options
17+
*
18+
* @param {DOM node or object} gd : graph div object
19+
* @param {array} extensions : list of extension to pass to createRegl
20+
*/
21+
exports.prepare = function prepare(gd, extensions) {
22+
gd._fullLayout._glcanvas.each(function(d) {
23+
if(d.regl) return;
24+
25+
d.regl = createRegl({
26+
canvas: this,
27+
attributes: {
28+
antialias: !d.pick,
29+
preserveDrawingBuffer: true
30+
},
31+
pixelRatio: gd._context.plotGlPixelRatio || global.devicePixelRatio,
32+
extensions: extensions || []
33+
});
34+
});
35+
};
36+
37+
/**
38+
* Clear gl frame (if any). This is a common pattern as
39+
* we usually set `preserveDrawingBuffer: true` during
40+
* gl context creation (e.g. via `reglUtils.prepare`).
41+
*
42+
* @param {DOM node or object} gd : graph div object
43+
*/
44+
exports.clear = function clear(gd) {
45+
var fullLayout = gd._fullLayout;
46+
47+
if(fullLayout._glcanvas && fullLayout._glcanvas.size()) {
48+
fullLayout._glcanvas.each(function(d) {
49+
if(d.regl) d.regl.clear({color: true});
50+
});
51+
}
52+
};

src/plots/cartesian/dragbox.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var supportsPassive = require('has-passive-events');
1616
var Registry = require('../../registry');
1717
var Lib = require('../../lib');
1818
var svgTextUtils = require('../../lib/svg_text_utils');
19+
var reglUtils = require('../../lib/regl_utils');
1920
var Color = require('../../components/color');
2021
var Drawing = require('../../components/drawing');
2122
var Fx = require('../../components/fx');
@@ -713,14 +714,7 @@ function makeDragBox(gd, plotinfo, x, y, w, h, ns, ew) {
713714
return ax._length * (1 - scaleFactor) * FROM_TL[ax.constraintoward || 'middle'];
714715
}
715716

716-
// clear gl frame, if any, since we preserve drawing buffer
717-
// FIXME: code duplication with cartesian.plot
718-
// TODO DRY this up!!
719-
if(fullLayout._glcanvas && fullLayout._glcanvas.size()) {
720-
fullLayout._glcanvas.each(function(d) {
721-
if(d.regl) d.regl.clear({color: true});
722-
});
723-
}
717+
reglUtils.clear(gd);
724718

725719
for(i = 0; i < subplots.length; i++) {
726720
var subplot = plotinfos[subplots[i]],

src/traces/parcoords/plot.js

+2-13
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,15 @@
99
'use strict';
1010

1111
var parcoords = require('./parcoords');
12-
var createRegl = require('regl');
12+
var reglUtils = require('../../lib/regl_utils');
1313

1414
module.exports = function plot(gd, cdparcoords) {
1515
var fullLayout = gd._fullLayout;
1616
var svg = fullLayout._toppaper;
1717
var root = fullLayout._paperdiv;
1818
var container = fullLayout._glcontainer;
1919

20-
// make sure proper regl instances are created
21-
fullLayout._glcanvas.each(function(d) {
22-
if(d.regl) return;
23-
d.regl = createRegl({
24-
canvas: this,
25-
attributes: {
26-
antialias: !d.pick,
27-
preserveDrawingBuffer: true
28-
},
29-
pixelRatio: gd._context.plotGlPixelRatio || global.devicePixelRatio
30-
});
31-
});
20+
reglUtils.prepare(gd);
3221

3322
var gdDimensions = {};
3423
var gdDimensionsOriginalOrder = {};

src/traces/scattergl/index.js

+2-15
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
'use strict';
1010

11-
var createRegl = require('regl');
1211
var createScatter = require('regl-scatter2d');
1312
var createLine = require('regl-line2d');
1413
var createError = require('regl-error2d');
@@ -17,6 +16,7 @@ var arrayRange = require('array-range');
1716

1817
var Registry = require('../../registry');
1918
var Lib = require('../../lib');
19+
var reglUtils = require('../../lib/regl_utils');
2020
var AxisIDs = require('../../plots/cartesian/axis_ids');
2121

2222
var subTypes = require('../scatter/subtypes');
@@ -336,20 +336,7 @@ function plot(gd, subplot, cdata) {
336336
var width = fullLayout.width;
337337
var height = fullLayout.height;
338338

339-
// make sure proper regl instances are created
340-
fullLayout._glcanvas.each(function(d) {
341-
if(d.regl || d.pick) return;
342-
d.regl = createRegl({
343-
canvas: this,
344-
attributes: {
345-
antialias: !d.pick,
346-
preserveDrawingBuffer: true
347-
},
348-
extensions: ['ANGLE_instanced_arrays', 'OES_element_index_uint'],
349-
pixelRatio: gd._context.plotGlPixelRatio || global.devicePixelRatio
350-
});
351-
});
352-
339+
reglUtils.prepare(gd, ['ANGLE_instanced_arrays', 'OES_element_index_uint']);
353340
var regl = fullLayout._glcanvas.data()[0].regl;
354341

355342
// that is needed for fills

src/traces/splom/base_plot.js

+2-14
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
'use strict';
1010

11-
var createRegl = require('regl');
1211
var createLine = require('regl-line2d');
1312

1413
var Registry = require('../../registry');
1514
var Lib = require('../../lib');
15+
var reglUtils = require('../../lib/regl_utils');
1616
var getModuleCalcData = require('../../plots/get_data').getModuleCalcData;
1717
var Cartesian = require('../../plots/cartesian');
1818
var AxisIDs = require('../../plots/cartesian/axis_ids');
@@ -31,19 +31,7 @@ function plot(gd) {
3131
});
3232
}
3333

34-
// make sure proper regl instances are created
35-
fullLayout._glcanvas.each(function(d) {
36-
if(d.regl || d.pick) return;
37-
d.regl = createRegl({
38-
canvas: this,
39-
attributes: {
40-
antialias: !d.pick,
41-
preserveDrawingBuffer: true
42-
},
43-
extensions: ['ANGLE_instanced_arrays', 'OES_element_index_uint'],
44-
pixelRatio: gd._context.plotGlPixelRatio || global.devicePixelRatio
45-
});
46-
});
34+
reglUtils.prepare(gd, ['ANGLE_instanced_arrays', 'OES_element_index_uint']);
4735

4836
if(fullLayout._hasOnlyLargeSploms) {
4937
drawGrid(gd);

0 commit comments

Comments
 (0)