Skip to content

Commit 1fb7aae

Browse files
committed
add logic for static canvas and static gl context into scene2d
1 parent e7a94ed commit 1fb7aae

File tree

1 file changed

+86
-62
lines changed
  • shelly/plotlyjs/static/plotlyjs/src/gl2d

1 file changed

+86
-62
lines changed

shelly/plotlyjs/static/plotlyjs/src/gl2d/scene2d.js

Lines changed: 86 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -8,78 +8,25 @@ var createLineWithMarkers = require('./scattergl/convert/');
88
var createOptions = require('./lib/gl2daxes');
99
var createCamera = require('./lib/camera');
1010
var htmlToUnicode = require('../gl3d/lib/html2unicode');
11+
var showNoWebGlMsg = require('../gl3d/lib/show_no_webgl_msg');
12+
13+
var AXES = ['xaxis', 'yaxis'];
14+
var STATIC_CANVAS, STATIC_CONTEXT;
1115

1216

1317
Plotly.Plots.registerSubplot('gl2d', ['xaxis', 'yaxis'], ['x', 'y'],
1418
Plotly.Axes.traceAttributes);
1519

1620
function Scene2D(options, fullLayout) {
17-
var container = this.container = options.container;
18-
21+
this.container = options.container;
1922
this.pixelRatio = options.plotGlPixelRatio || window.devicePixelRatio;
2023
this.id = options.id;
2124
this.staticPlot = !!options.staticPlot;
25+
2226
this.fullLayout = fullLayout;
2327
this.updateAxes(fullLayout);
2428

25-
var width = fullLayout.width;
26-
var height = fullLayout.height;
27-
28-
// get pixel ratio
29-
// var pixelRatio = this.pixelRatio = options.pixelRatio || window.devicePixelRatio;
30-
var pixelRatio = this.pixelRatio = 2;
31-
32-
// create canvas and append to container
33-
var canvas = this.canvas = document.createElement('canvas');
34-
canvas.width = Math.ceil(pixelRatio * width) |0;
35-
canvas.height = Math.ceil(pixelRatio * height) |0;
36-
canvas.style.width = '100%';
37-
canvas.style.height = '100%';
38-
canvas.style.position = 'absolute';
39-
canvas.style.top = '0px';
40-
canvas.style.left = '0px';
41-
canvas.style['pointer-events'] = 'none';
42-
43-
// create SVG container for hover text
44-
var svgContainer = this.svgContainer = document.createElementNS(
45-
'http://www.w3.org/2000/svg',
46-
'svg');
47-
svgContainer.style.position = 'absolute';
48-
svgContainer.style.top = svgContainer.style.left = '0px';
49-
svgContainer.style.width = svgContainer.style.height = '100%';
50-
svgContainer.style['z-index'] = 20;
51-
svgContainer.style['pointer-events'] = 'none';
52-
53-
// create div to catch the mouse event
54-
var mouseContainer = this.mouseContainer = document.createElement('div');
55-
mouseContainer.style.position = 'absolute';
56-
57-
// get webgl context
58-
var glopts = options.glopts || { premultipliedAlpha: true };
59-
var gl;
60-
61-
glopts.preserveDrawingBuffer = true;
62-
63-
try {
64-
gl = canvas.getContext('webgl', glopts);
65-
} catch(e) {}
66-
67-
if(!gl) {
68-
try {
69-
gl = canvas.getContext('experimental-webgl', glopts);
70-
} catch(e) {}
71-
}
72-
73-
if(!gl) {
74-
throw new Error('webgl not supported!');
75-
}
76-
77-
this.gl = gl;
78-
79-
// append canvas to container
80-
container.appendChild(canvas);
81-
container.appendChild(svgContainer);
82-
container.appendChild(mouseContainer);
29+
this.makeFramework();
8330

8431
// update options
8532
this.glplotOptions = createOptions(this);
@@ -119,12 +66,88 @@ module.exports = Scene2D;
11966

12067
var proto = Scene2D.prototype;
12168

122-
var AXES = ['xaxis', 'yaxis'];
69+
proto.makeFramework = function() {
70+
// create canvas and gl context
71+
if(this.staticPlot) {
72+
if(!STATIC_CONTEXT) {
73+
STATIC_CANVAS = document.createElement('canvas');
74+
75+
try {
76+
STATIC_CONTEXT = STATIC_CANVAS.getContext('webgl', {
77+
preserveDrawingBuffer: true,
78+
premultipliedAlpha: true
79+
});
80+
} catch(e) {
81+
throw new Error([
82+
'Error creating static canvas/context for image server'
83+
].join(' '));
84+
}
85+
}
86+
87+
this.canvas = STATIC_CANVAS;
88+
this.gl = STATIC_CONTEXT;
89+
}
90+
else {
91+
var liveCanvas = document.createElement('canvas'),
92+
glOpts = { premultipliedAlpha: true };
93+
var gl;
94+
95+
try {
96+
gl = liveCanvas.getContext('webgl', glOpts);
97+
} catch(e) {}
98+
99+
if(!gl) {
100+
try {
101+
gl = liveCanvas.getContext('experimental-webgl', glOpts);
102+
} catch(e) {}
103+
}
104+
105+
if(!gl) showNoWebGlMsg(this);
106+
107+
this.canvas = liveCanvas;
108+
this.gl = gl;
109+
}
110+
111+
// position the canvas
112+
var canvas = this.canvas,
113+
pixelRatio = this.pixelRatio,
114+
fullLayout = this.fullLayout;
115+
116+
canvas.width = Math.ceil(pixelRatio * fullLayout.width) |0;
117+
canvas.height = Math.ceil(pixelRatio * fullLayout.height) |0;
118+
canvas.style.width = '100%';
119+
canvas.style.height = '100%';
120+
canvas.style.position = 'absolute';
121+
canvas.style.top = '0px';
122+
canvas.style.left = '0px';
123+
canvas.style['pointer-events'] = 'none';
124+
125+
// create SVG container for hover text
126+
var svgContainer = this.svgContainer = document.createElementNS(
127+
'http://www.w3.org/2000/svg',
128+
'svg');
129+
svgContainer.style.position = 'absolute';
130+
svgContainer.style.top = svgContainer.style.left = '0px';
131+
svgContainer.style.width = svgContainer.style.height = '100%';
132+
svgContainer.style['z-index'] = 20;
133+
svgContainer.style['pointer-events'] = 'none';
134+
135+
// create div to catch the mouse event
136+
var mouseContainer = this.mouseContainer = document.createElement('div');
137+
mouseContainer.style.position = 'absolute';
138+
139+
// append canvas, hover svg and mouse div to container
140+
var container = this.container;
141+
container.appendChild(canvas);
142+
container.appendChild(svgContainer);
143+
container.appendChild(mouseContainer);
144+
};
123145

124146
proto.toImage = function(format) {
125147
if(!format) format = 'png';
126148

127149
this.stopped = true;
150+
if(this.staticPlot) this.container.appendChild(STATIC_CANVAS);
128151

129152
// force redraw
130153
this.glplot.setDirty(true);
@@ -173,6 +196,8 @@ proto.toImage = function(format) {
173196
dataURL = canvas.toDataURL('image/png');
174197
}
175198

199+
if(this.staticPlot) this.container.removeChild(STATIC_CANVAS);
200+
176201
return dataURL;
177202
};
178203

@@ -361,7 +386,6 @@ trace_id_loop:
361386
options.dataBox = [xrange[0], yrange[0], xrange[1], yrange[1]];
362387

363388
options.merge(fullLayout);
364-
365389
glplot.update(options);
366390
};
367391

0 commit comments

Comments
 (0)