A highly scalable scatterplot rendered with WebGL using Regl
Demo: https://flekschas.github.io/regl-scatterplot/
Interactions:
- Pan: Click and drag your mouse.
- Zoom: Scroll vertically.
- Rotate: While pressing ALT, click and drag your mouse.
- Select a single dot: Click on a dot with your mouse.
- Select multiple dots: While pressing SHIFT, click and drag your mouse. All items within the lasso will be selected.
- Deselect: Click outside a dot.
npm -i regl-scatterplot
import createScatterplot from 'regl-scatterplot';
const canvas = document.querySelector('#canvas');
const { width, height } = canvas.getBoundingClientRect();
const scatterplot = createScatterplot({
canvas,
width,
height,
pointSize: 5
});
const points = new Array(10000)
.fill()
.map(() => [-1 + Math.random() * 2, -1 + Math.random() * 2, color]);
scatterplot.draw(points);See a complete example at example/index.js.
Returns: a new scatterplot instance.
Options: is an object that accepts the following properties:
regla Regl instance to be used for rendering.backgroundbackground color of the scatterplot.backgroundImagebackground image for the scatterplot. This must be a regl texture object.canvascanvas element.colorscolormap.pointSizesize of the points.pointSizeSelectedadditional size of the points when being selected. I.e., if set to0selected and deselect points have the same size.pointOutlineWidthwidth of the point outline when a point is being selected.widthwidth of the canvas element.heightheight of the canvas element.targetlocation that the camera should look at.distancedistance of the camera to the target.rotationrotation of the camera around the target.viewview matrix definingtarget,distance, androtation. When giventarget,distance, androtationare ignored.
Returns: a new Regl instance with appropriate extensions being enabled.
Canvas: the canvas object on which the scatterplot will be rendered on.
Returns: a new Regl texture from a remote image.
regl: the Regl instance associated to your scatterplot instance. Either use createRegl() or scatterplot.regl;
url: the URL to an image.
isCrossOrigin: if url is pointing to another origin isCrossOrigin must be set to true.
Sets and draws points, which must be an array of points where a point is interpreted as a quadruple of form [x, y, category, value].
Examples:
const points = [
[
// The X position
2,
// The Y position
1,
// The category, which defaults to `0` if `undefined`
0,
// Some value, which defaults to `0` if `undefined`
0.5
]
];
scatterplot.draw(points);
// You can now do something else like changing the point size etc.
// Lets redraw the scatterplot. Since `draw` is caching the points you don't
// have to specify the points here again if they didn't change.
scatterplot.draw();
// Lets actively unset the points. Since `draw()` assumes that you want to
// redraw existing points you have to actively pass in an empty array
scatterplot.draw([]);The canvas element on which the scatterplot is rendered.
The Regl instance which renderes the scatterplot.
The version number of the scatterplot.
Returns: one of the properties documented in set()
Arguments: properties is an object of key-value pairs. The list of all understood properties is given below.
Properties:
| Name | Type | Default | Constraints | Settable | Nullifiable |
|---|---|---|---|---|---|
| canvas | object | document.createElement('canvas') |
true |
false |
|
| regl | object | createRegl(canvas) |
true |
false |
|
| version | string | false |
false |
||
| width | number | 300 |
> 0 | true |
false |
| height | number | 200 |
> 0 | true |
false |
| aspectRatio | number | 1.0 |
> 0 | true |
false |
| background | string or array | rgba(0,0,0,1) | hex, rgb, rgba | true |
false |
| backgroundImage | function | null |
Regl texture | true |
true |
| colorBy | string | null |
category or value |
true |
true |
| colors | array | see below | list of hex, rgb, rgba | true |
false |
| opacity | number | 1 |
> 0 | true |
false |
| pointOutlineWidth | number | 2 |
>= 0 | true |
false |
| pointSize | number | 6 |
> 0 | true |
false |
| pointSizeSelected | number | 2 |
>= 0 | true |
false |
| lassoColor | array | rgba(0, 0.667, 1, 1) | hex, rgb, rgba | true |
false |
| lassoMinDelay | number | 15 | >= 0 | true |
false |
| lassoMinDist | number | 4 | >= 0 | true |
false |
| showRecticle | boolean | false |
true or false |
true |
false |
| recticleColor | array | rgba(1,1,1,.5) | hex, rgb, rgba | true |
false |
Notes:
-
An attribute is considered nullifiable if it can be unset. Attributes that are not nullifiable will be ignored if you try to set them to a falsy value. For example, if you call
scatterplot.attr({ width: 0 });the width will not be changed as0is interpreted as a falsy value. -
The background of the scatterplot is transparent, i.e., you have to control the background with CSS!
backgroundis used when drawing the outline of selected points to simulate the padded border only. -
The background image must be a Regl texture function. To easily set a remote image as the background please use
createTextureFromUrl. -
The scatterplot stores 4 colors per color representing 4 states, representing:
- normal: used by default [default:
[0.66, 0.66, 0.66, 1]] - active: used when selecting a point [default:
[0, 0.55, 1, 1]] - hover: used when mousing over a point not implemented yet [default:
[1, 1, 1, 1]] - background: used as the background color [default:
[0, 0, 0, 1]]
When defining colors you can either pass in a flat array of normal colors and regl-scatterplot will fill in the rest or you provide a list of quadruples where each quadruple defines the colors for all 4 states.
- normal: used by default [default:
-
Points can currently by colored by category and value.
-
The size of selected points is given by
pointSize + pointSizeSelected
Examples:
// Set width and height
scatterplot.set({ width: 300, height: 200 });
// get width
const width = scatterplot.get('width');
// Set the aspect ratio of the scatterplot. This aspect ratio is referring to
// your data source and **not** the aspect ratio of the canvas element! By
// default it is assumed that your data us following a 1:1 ratio and this ratio
// is preserved even if your canvas element has some other aspect ratio. But if
// you wanted you could provide data that's going from [0,2] in x and [0,1] in y
// in which case you'd have to set the aspect ratio as follows to `2`.
scatterplot.set({ aspectRatio: 2.0 });
// Set background color red
scatterplot.set({ background: '#00ff00' });
scatterplot.set({ background: [255, 0, 0] });
scatterplot.set({ background: [255, 0, 0, 1.0] });
scatterplot.set({ background: [1, 0, 0, 1.0] }); // normalized rgba
// Set background image to an image with the same origin
scatterplot.set({ backgroundImage: 'my-image.png' });
// Set background image to an image with a different origin
scatterplot.set({ backgroundImage: { src: 'https://server.com/my-image.png', crossOrigin: true } });
// Set background image to some regl texture
const image = new Image();
image.src = 'my-image.png';
image.onload = () => { scatterplot.set({ backgroundImage: regl.texture(image) });
// Color by
scatterplot.set({ colorBy: 'category' });
// Set color map
scatterplot.set({ colors: ['#ff0000', '#00ff00', '#0000ff'] });
// Set base opacity
scatterplot.set({ opacity: 0.5 });
// Set the width of the outline of selected points
scatterplot.set({ pointOutlineWidth: 2 });
// Set the base point size
scatterplot.set({ pointSize: 10 });
// Set the additional point size of selected points
scatterplot.set({ pointSizeSelected: 2 });
// Change the lasso color and make it very smooth, i.e., do not wait before
// extending the lasso (i.e., `lassoMinDelay = 0`) and extend the lasso when
// the mouse moves at least 1 pixel
scatterplot.set({ lassoColor: [1, 1, 1, 1], lassoMinDelay: 0, lassoMinDist: 1 });
// Activate recticle and set recticle color to red
scatterplot.set({ showRecticle: true, recticleColor: [1, 0, 0, 0.66] });Select some points, such that they get visually highlighted. This will trigger
a select event.
Deselect all selected points. This will trigger a deselect event.
Destroys the scatterplot instance by disposing all event listeners, the pubSub instance, regl, and the camera.
Refreshes the viewport of the scatterplot's regl instance.
Sets the view back to the initially defined view.
Subscribe to an event.
eventName needs to be one of the following events:
pointover[payload:point]: broadcasted when the mouse cursor is over a pointpointout[payload:point]: broadcasted when the mouse cursor moves out of a pointselect[payload:{ points }]: broadcasted when points are selecteddeselect[payload:undefined]: broadcasted when points are deselectedview[payload: camera view matrix]: broadcasted when the view changes
eventHandler needs to be a callback function that can receive the payload.
Unsubscribe from an event. See scatterplot.subscribe() for a list of all
events.
