Skip to content

Commit 57fac09

Browse files
committed
Improve exporting and drawing (#42)
1 parent a3e1e3c commit 57fac09

3 files changed

Lines changed: 51 additions & 16 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,14 @@ Refreshes the viewport of the scatterplot's regl instance.
306306

307307
Sets the view back to the initially defined view.
308308

309+
<a name="scatterplot.export" href="#scatterplot.export">#</a> scatterplot.<b>export</b>(<i>options</i>)
310+
311+
**Arguments:**
312+
313+
- `options` is an object for customizing how to export. See [regl.read()](https://github.com/regl-project/regl/blob/master/API.md#reading-pixels) for details.
314+
315+
**Returns:** an object with three properties: `pixels`, `width`, and `height`. The `pixels` is a `Uint8ClampedArray`.
316+
309317
<a name="scatterplot.subscribe" href="#scatterplot.subscribe">#</a> scatterplot.<b>subscribe</b>(<i>eventName</i>, <i>eventHandler</i>)
310318

311319
Subscribe to an event.

src/index.js

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ const createScatterplot = (initialProperties = {}) => {
247247
const fbo = regl.framebuffer({
248248
width: fboRes[0],
249249
height: fboRes[1],
250+
colorFormat: 'rgba',
250251
colorType: 'float',
251252
});
252253
let backgroundColorBrightness = rgbBrightness(backgroundColor);
@@ -1187,7 +1188,7 @@ const createScatterplot = (initialProperties = {}) => {
11871188
11881189
void main () {
11891190
vec4 color = texture2D(src, gl_FragCoord.xy / srcRes);
1190-
gl_FragColor = vec4(approxLinearToSRGB(color.rgb, gamma), 1);
1191+
gl_FragColor = vec4(approxLinearToSRGB(color.rgb, gamma), color.a);
11911192
}`,
11921193
attributes: {
11931194
xy: [-4, -4, 4, -4, 0, 4],
@@ -1199,6 +1200,15 @@ const createScatterplot = (initialProperties = {}) => {
11991200
},
12001201
count: 3,
12011202
depth: { enable: false },
1203+
blend: {
1204+
enable: true,
1205+
func: {
1206+
srcRGB: 'one',
1207+
srcAlpha: 'one',
1208+
dstRGB: 'one minus src alpha',
1209+
dstAlpha: 'one minus src alpha',
1210+
},
1211+
},
12021212
});
12031213

12041214
const drawPoints = (
@@ -1696,27 +1706,34 @@ const createScatterplot = (initialProperties = {}) => {
16961706
// Update camera
16971707
isViewChanged = camera.tick();
16981708

1709+
regl.clear({
1710+
// background color (transparent)
1711+
color: [0, 0, 0, 0],
1712+
depth: 1,
1713+
});
1714+
1715+
// eslint-disable-next-line no-underscore-dangle
1716+
if (backgroundImage && backgroundImage._reglType) {
1717+
drawBackgroundImage();
1718+
}
1719+
1720+
if (lassoPointsCurr.length > 2) drawPolygon2d();
1721+
1722+
// The draw order of the following calls is important!
1723+
if (!isTransitioning)
1724+
pointConnections.draw({
1725+
projection: getProjection(),
1726+
model: getModel(),
1727+
view: getView(),
1728+
});
1729+
16991730
fbo.use(() => {
17001731
regl.clear({
17011732
// background color (transparent)
17021733
color: [0, 0, 0, 0],
17031734
depth: 1,
17041735
});
17051736

1706-
// eslint-disable-next-line no-underscore-dangle
1707-
if (backgroundImage && backgroundImage._reglType) {
1708-
drawBackgroundImage();
1709-
}
1710-
1711-
if (lassoPointsCurr.length > 2) drawPolygon2d();
1712-
1713-
// The draw order of the following calls is important!
1714-
if (!isTransitioning)
1715-
pointConnections.draw({
1716-
projection: getProjection(),
1717-
model: getModel(),
1718-
view: getView(),
1719-
});
17201737
drawPointBodies();
17211738
if (!mouseDown && (showRecticle || showRecticleOnce)) drawRecticle();
17221739
if (hoveredPoint >= 0) drawHoveredPoint();
@@ -2573,6 +2590,12 @@ const createScatterplot = (initialProperties = {}) => {
25732590
}
25742591
};
25752592

2593+
const exportFn = (options = {}) => ({
2594+
pixels: Uint8ClampedArray.from(regl.read(options)),
2595+
width: currentWidth * window.devicePixelRatio,
2596+
height: currentHeight * window.devicePixelRatio,
2597+
});
2598+
25762599
const init = () => {
25772600
updateViewAspectRatio();
25782601
initCamera();
@@ -2676,6 +2699,7 @@ const createScatterplot = (initialProperties = {}) => {
26762699
reset: withDraw(reset),
26772700
select,
26782701
set,
2702+
export: exportFn,
26792703
subscribe: pubSub.subscribe,
26802704
unsubscribe: pubSub.unsubscribe,
26812705
};

src/utils.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ export const checkReglExtensions = (regl) => {
3434
* @return {function} New Regl instance
3535
*/
3636
export const createRegl = (canvas) => {
37-
const gl = canvas.getContext('webgl');
37+
const gl = canvas.getContext('webgl', {
38+
antialias: true,
39+
preserveDrawingBuffer: true,
40+
});
3841
const extensions = [];
3942

4043
// Needed to run the tests properly as the headless-gl doesn't support all

0 commit comments

Comments
 (0)