Skip to content

Commit 753efb7

Browse files
committed
Integrate createTextureFromUrl to make adding a background image easier
1 parent f92f330 commit 753efb7

5 files changed

Lines changed: 387 additions & 353 deletions

File tree

README.md

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const scatterplot = createScatterplot({
4545
canvas,
4646
width,
4747
height,
48-
pointSize: 5
48+
pointSize: 5,
4949
});
5050

5151
const points = new Array(10000)
@@ -61,15 +61,15 @@ See a complete example at [example/index.js](example/index.js).
6161

6262
### Constructors
6363

64-
#### `const scatterplot = createScatterplot(options)`
64+
<a name="createScatterplot" href="#createScatterplot">#</a> <b>createScatterplot</b>(<i>options = {}</i>)
6565

6666
**Returns:** a new scatterplot instance.
6767

6868
**Options:** is an object that accepts the following properties:
6969

7070
- `regl` a Regl instance to be used for rendering.
7171
- `background` background color of the scatterplot.
72-
- `backgroundImage` background image for the scatterplot. This must be a [regl texture object](https://github.com/regl-project/regl/blob/gh-pages/API.md#textures) and can be created with [`createTextureFromUrl`](#const-texture--createTextureFromUrlregl-url-isCrossOrigin).
72+
- `backgroundImage` background image for the scatterplot. This can either be a URL or a [regl texture object](https://github.com/regl-project/regl/blob/gh-pages/API.md#textures). The latter can be created with [`scatterplot.createTextureFromUrl`](#scatterplot.createTextureFromUrl).
7373
- `canvas` canvas element.
7474
- `colors` colormap.
7575
- `pointSize` size of the points.
@@ -82,25 +82,19 @@ See a complete example at [example/index.js](example/index.js).
8282
- `rotation` rotation of the camera around the target.
8383
- `view` view matrix defining `target`, `distance`, and `rotation`. When given `target`, `distance`, and `rotation` are ignored.
8484

85-
#### `const regl = createRegl(canvas)`
85+
<a name="createRegl" href="#createRegl">#</a> <b>createRegl</b>(<i>canvas</i>)
8686

8787
**Returns:** a new Regl instance with appropriate extensions being enabled.
8888

8989
**Canvas:** the canvas object on which the scatterplot will be rendered on.
9090

91-
#### `const texture = createTextureFromUrl(regl, url, isCrossOrigin)`
91+
<a name="createTextureFromUrl" href="#createTextureFromUrl">#</a> <b>createTextureFromUrl</b>(<i>regl</i>, <i>url</i>)
9292

93-
**Returns:** a new [Regl texture](https://github.com/regl-project/regl/blob/gh-pages/API.md#textures) from a remote image.
94-
95-
**regl:** the Regl instance **associated to your scatterplot instance**. Either use [`createRegl()`](#const-regl--createreglcanvas) or `scatterplot.regl`. Important, if you use `createRegl()` make sure to pass the created regl instance into the scatterplot constructor as well!
96-
97-
**url:** the URL to an image.
98-
99-
**isCrossOrigin:** if `url` is pointing to another origin `isCrossOrigin` must be set to `true`.
93+
_DEPRECATED! Use [`scatterplot.createTextureFromUrl()`](#scatterplot.createTextureFromUrl) instead._
10094

10195
### Methods
10296

103-
#### `scatterplot.draw(points)`
97+
<a name="scatterplot.draw" href="#scatterplot.draw">#</a> scatterplot.<b>draw</b>(<i>points</i>)
10498

10599
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]`.
106100

@@ -116,8 +110,8 @@ const points = [
116110
// The category, which defaults to `0` if `undefined`
117111
0,
118112
// Some value, which defaults to `0` if `undefined`
119-
0.5
120-
]
113+
0.5,
114+
],
121115
];
122116

123117
scatterplot.draw(points);
@@ -133,23 +127,11 @@ scatterplot.draw();
133127
scatterplot.draw([]);
134128
```
135129

136-
#### `scatterplot.canvas`
137-
138-
The canvas element on which the scatterplot is rendered.
139-
140-
#### `scatterplot.regl`
141-
142-
The Regl instance which renderes the scatterplot.
143-
144-
#### `scatterplot.version`
145-
146-
The version number of the scatterplot.
147-
148-
#### `scatterplot.get(property)`
130+
<a name="scatterplot.get" href="#scatterplot.set">#</a> scatterplot.<b>get</b>(<i>property</i>)
149131

150132
**Returns:** one of the properties documented in [`set()`](#scatterplotset)
151133

152-
#### `scatterplot.set(properties)`
134+
<a name="scatterplot.set" href="#scatterplot.set">#</a> scatterplot.<b>set</b>(<i>properties = {}</i>)
153135

154136
**Arguments:** `properties` is an object of key-value pairs. The list of all understood properties is given below.
155137

@@ -227,14 +209,22 @@ scatterplot.set({ backgroundColor: [255, 0, 0] }); // rgb array
227209
scatterplot.set({ backgroundColor: [255, 0, 0, 1.0] }); // rgba array
228210
scatterplot.set({ backgroundColor: [1.0, 0, 0, 1.0] }); // normalized rgba
229211

230-
// Set background image to an image from the same origin
231-
scatterplot.set({ backgroundImage: 'my-image.png' });
232-
// Set background image to an image from a different origin
233-
scatterplot.set({ backgroundImage: { src: 'https://server.com/my-image.png', crossOrigin: true } });
234-
// Set background image to some regl texture
235-
const image = new Image();
236-
image.src = 'my-image.png';
237-
image.onload = () => { scatterplot.set({ backgroundImage: scatterplot.regl.texture(image) });
212+
// Set background image to an image
213+
scatterplot.set({ backgroundImage: 'https://server.com/my-image.png' });
214+
// If you need to know when the image was loaded you have two options. First,
215+
// you can listen to the following event
216+
scatterplot.subscribe(
217+
'background-image-ready',
218+
() => {
219+
console.log('Background image is now loaded and rendered!');
220+
},
221+
1
222+
);
223+
// or you load the image yourself as follows
224+
const backgroundImage = await scatterplot.createTextureFromUrl(
225+
'https://server.com/my-image.png'
226+
);
227+
scatterplot.set({ backgroundImage });
238228

239229
// Color by
240230
scatterplot.set({ colorBy: 'category' });
@@ -261,35 +251,39 @@ scatterplot.set({ pointSizeSelected: 2 });
261251
// Change the lasso color and make it very smooth, i.e., do not wait before
262252
// extending the lasso (i.e., `lassoMinDelay = 0`) and extend the lasso when
263253
// the mouse moves at least 1 pixel
264-
scatterplot.set({ lassoColor: [1, 1, 1, 1], lassoMinDelay: 0, lassoMinDist: 1 });
254+
scatterplot.set({
255+
lassoColor: [1, 1, 1, 1],
256+
lassoMinDelay: 0,
257+
lassoMinDist: 1,
258+
});
265259

266260
// Activate recticle and set recticle color to red
267261
scatterplot.set({ showRecticle: true, recticleColor: [1, 0, 0, 0.66] });
268262
```
269263

270-
#### `scatterplot.select(points)`
264+
<a name="scatterplot.select" href="#scatterplot.select">#</a> scatterplot.<b>select</b>()
271265

272266
Select some points, such that they get visually highlighted. This will trigger
273267
a `select` event.
274268

275-
#### `scatterplot.deselect()`
269+
<a name="scatterplot.deselect" href="#scatterplot.deselect">#</a> scatterplot.<b>deselect</b>()
276270

277271
Deselect all selected points. This will trigger a `deselect` event.
278272

279-
#### `scatterplot.destroy()`
273+
<a name="scatterplot.destroy" href="#scatterplot.destroy">#</a> scatterplot.<b>destroy</b>()
280274

281275
Destroys the scatterplot instance by disposing all event listeners, the pubSub
282276
instance, regl, and the camera.
283277

284-
#### `scatterplot.refresh()`
278+
<a name="scatterplot.refresh" href="#scatterplot.refresh">#</a> scatterplot.<b>refresh</b>()
285279

286280
Refreshes the viewport of the scatterplot's regl instance.
287281

288-
#### `scatterplot.reset()`
282+
<a name="scatterplot.reset" href="#scatterplot.reset">#</a> scatterplot.<b>reset</b>()
289283

290284
Sets the view back to the initially defined view.
291285

292-
#### `scatterplot.subscribe(eventName, eventHandler)`
286+
<a name="scatterplot.subscribe" href="#scatterplot.subscribe">#</a> scatterplot.<b>unsubscribe</b>(<i>eventName</i>, <i>eventHandler</i>)
293287

294288
Subscribe to an event.
295289

@@ -303,7 +297,13 @@ Subscribe to an event.
303297

304298
**eventHandler** needs to be a callback function that can receive the payload.
305299

306-
#### `scatterplot.unsubscribe(eventName, eventHandler)`
300+
<a name="scatterplot.unsubscribe" href="#scatterplot.unsubscribe">#</a> scatterplot.<b>unsubscribe</b>(<i>eventName</i>, <i>eventHandler</i>)
307301

308302
Unsubscribe from an event. See [`scatterplot.subscribe()`](#scatterplot.subscribe) for a list of all
309303
events.
304+
305+
<a name="scatterplot.createTextureFromUrl" href="#scatterplot.createTextureFromUrl">#</a> scatterplot.<b>createTextureFromUrl</b>(<i>url</i>)
306+
307+
**Returns:** a Promise that resolves to a [Regl texture](https://github.com/regl-project/regl/blob/gh-pages/API.md#textures) that can be used, for example, as the [background image](#).
308+
309+
**url:** the URL to an image.

0 commit comments

Comments
 (0)