Skip to content

Commit fea4606

Browse files
committed
partial commit on resizing
1 parent 530be52 commit fea4606

File tree

4 files changed

+143
-38
lines changed

4 files changed

+143
-38
lines changed

src/interaction.ts

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ export class Zoom {
4343
HTMLElement,
4444
any
4545
>;
46-
public width: number;
47-
public height: number;
46+
private _width: number;
47+
private _height: number;
4848
public renderers: Map<string, Renderer>;
4949
public deeptable?: Deeptable;
5050
public _timer?: d3.Timer;
@@ -62,8 +62,8 @@ export class Zoom {
6262
this.prefs = prefs;
6363

6464
this.svg_element_selection = select(selector);
65-
this.width = +this.svg_element_selection.attr('width');
66-
this.height = +this.svg_element_selection.attr('height');
65+
this._width = plot.width;
66+
this._height = plot.height;
6767
this.renderers = new Map();
6868
this.scatterplot = plot;
6969
// A zoom keeps track of all the renderers
@@ -72,14 +72,40 @@ export class Zoom {
7272
this.renderers = new Map();
7373
}
7474

75-
attach_tiles(tiles: Deeptable) {
76-
this.deeptable = tiles;
75+
get width() {
76+
return this._width;
77+
}
78+
79+
get height() {
80+
return this._height;
81+
}
82+
83+
public resize(width: number, height: number) {
84+
const { x_, y_ } = this.scales();
85+
const old_center = [
86+
x_.invert(this._width / 2),
87+
y_.invert(this._height / 2),
88+
];
89+
// Update the extent
90+
this.zoomer.extent([
91+
[0, 0],
92+
[width, height],
93+
]);
94+
this._width = width;
95+
this._height = height;
96+
this.scales(true); // Force rebuild of scales.
97+
this.zoom_to(this.transform.k, old_center[0], old_center[1]);
98+
}
99+
100+
attach_tiles(deeptable: Deeptable) {
101+
this.deeptable = deeptable;
77102
return this;
78103
}
79104

80105
attach_renderer(key: string, renderer: Renderer) {
81106
this.renderers.set(key, renderer);
82107
renderer.bind_zoom(this);
108+
// Should just be `this.initialize_zoom(), right?
83109
renderer.zoom.initialize_zoom();
84110
return this;
85111
}
@@ -175,10 +201,10 @@ export class Zoom {
175201
});
176202

177203
canvas.call(zoomer);
178-
179204
this.add_mouseover();
180205

181206
this.zoomer = zoomer;
207+
this.resize(width, height);
182208
}
183209

184210
set_highlit_points(data: StructRowProxy[]) {
@@ -292,22 +318,7 @@ export class Zoom {
292318
return this._timer;
293319
}
294320

295-
data(deeptable: undefined): Deeptable;
296-
data(deeptable: Deeptable): Zoom;
297-
298-
data(deeptable: Deeptable | undefined) {
299-
if (deeptable === undefined) {
300-
return this.deeptable;
301-
}
302-
this.deeptable = deeptable;
303-
return this as Zoom;
304-
}
305-
306-
/**
307-
*
308-
* @returns
309-
*/
310-
scales(): ScaleSet {
321+
scales(force = false): ScaleSet {
311322
// General x and y scales that map from data space
312323
// to pixel coordinates, and also
313324
// rescaled ones that describe the current zoom.
@@ -316,17 +327,16 @@ export class Zoom {
316327

317328
// equal_units: should a point of x be the same as a point of y?
318329

319-
if (this._scales) {
330+
if (force !== true && this._scales) {
320331
this._scales.x_ = this.transform.rescaleX(this._scales.x);
321332
this._scales.y_ = this.transform.rescaleY(this._scales.y);
322333
return this._scales;
323334
}
324335

325336
const { width, height } = this;
326-
if (this.deeptable === undefined) {
327-
throw new Error('Error--scales created before tileSet present.');
328-
}
329-
const { extent } = this.deeptable;
337+
338+
const extent = this.deeptable.extent;
339+
330340
if (extent === undefined) {
331341
throw new Error('Error--scales created before extent present.');
332342
}

src/regl_rendering.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ export class ReglRenderer extends Renderer {
470470
height: this.height,
471471
depth: false,
472472
});
473+
473474
this.fbos.ping = regl.framebuffer({
474475
width: this.width,
475476
height: this.height,
@@ -507,6 +508,20 @@ export class ReglRenderer extends Renderer {
507508
});
508509
}
509510

511+
resize(width: number, height: number): void {
512+
super.resize(width, height);
513+
this.resize_textures();
514+
}
515+
516+
resize_textures() {
517+
this.fbos.colorpicker?.resize(this.width, this.height);
518+
this.fbos.contour?.resize(this.width, this.height);
519+
this.fbos.ping?.resize(this.width, this.height);
520+
this.fbos.pong?.resize(this.width, this.height);
521+
this.fbos.lines?.resize(this.width, this.height);
522+
this.fbos.points?.resize(this.width, this.height);
523+
}
524+
510525
get_image_texture(url: string) {
511526
const { regl } = this;
512527
this.textures = this.textures || {};

src/rendering.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ export class Renderer {
126126
public holder: d3.Selection<Element, unknown, BaseType, unknown>;
127127
public canvas: HTMLCanvasElement;
128128
public deeptable: Deeptable;
129-
public width: number;
130-
public height: number;
129+
private _width: number;
130+
private _height: number;
131131
public _use_scale_to_download_tiles = true;
132132
public aes?: AestheticSet;
133133
public _zoom?: Zoom;
@@ -138,11 +138,30 @@ export class Renderer {
138138
this.canvas = select(
139139
this.holder!.node()!.firstElementChild,
140140
).node() as HTMLCanvasElement;
141-
this.width = +select(this.canvas).attr('width');
142-
this.height = +select(this.canvas).attr('height');
141+
this._width = this.scatterplot.width;
142+
this._height = this.scatterplot.height;
143143
this._use_scale_to_download_tiles = true;
144144
}
145145

146+
get width() {
147+
return this._width;
148+
}
149+
set width(value: number) {
150+
this._width = value;
151+
this.resize(this._width, this._height);
152+
}
153+
get height() {
154+
return this._height;
155+
}
156+
set height(value: number) {
157+
this._height = value;
158+
this.resize(this._width, this._height);
159+
}
160+
resize(width: number, height: number) {
161+
this._width = width;
162+
this._height = height;
163+
}
164+
146165
get discard_share() {
147166
// If jitter is temporal, e.g., or filters are in place,
148167
// it may make sense to estimate the number of hidden points.

src/scatterplot.ts

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ type Hook = () => void;
4949
*/
5050
export class Scatterplot {
5151
public _renderer?: ReglRenderer;
52-
public width: number;
53-
public height: number;
52+
protected _width: number;
53+
protected _height: number;
5454
public _root?: Deeptable;
5555
public elements?: Selection<SVGElement, unknown, Element, unknown>[];
5656
public secondary_renderers: Record<string, Renderer> = {};
@@ -104,19 +104,38 @@ export class Scatterplot {
104104
}
105105
const {
106106
selector,
107-
width,
108-
height,
107+
width: rawWidth,
108+
height: rawHeight,
109109
source_url,
110110
deeptable,
111111
arrow_buffer,
112112
arrow_table,
113113
} = options;
114+
let [width, height] = [rawWidth, rawHeight];
115+
if (rawWidth === undefined) {
116+
width = window.innerWidth;
117+
height = window.innerHeight;
118+
119+
// Permanently bind the scatterplot to the window size.
120+
window.onresize = () => {
121+
this.resize(window.innerWidth, window.innerHeight);
122+
};
123+
}
124+
125+
if (selector !== undefined) {
126+
this.bind(selector, width, height);
127+
}
128+
if (width !== undefined) {
129+
this._width = width;
130+
}
131+
if (height !== undefined) {
132+
this._height = height;
133+
}
134+
114135
this.bound = false;
115136
if (selector !== undefined) {
116137
this.bind(selector, width, height);
117138
}
118-
this.width = width || window.innerWidth;
119-
this.height = height || window.innerHeight;
120139
// mark_ready is called when the scatterplot can start drawing..
121140
this.ready = new Promise((resolve) => {
122141
this.mark_ready = resolve;
@@ -136,6 +155,48 @@ export class Scatterplot {
136155
this.prefs = { ...default_API_call } as DS.CompletePrefs;
137156
}
138157

158+
/**
159+
* @param selector A selector for the root element of the deepscatter; must already exist.
160+
* @param width Width of the plot, in pixels.
161+
* @param height Height of the plot, in pixels.
162+
*/
163+
164+
public resize(width: number, height: number) {
165+
// Set the dimensions of the plot.
166+
this._width = width;
167+
this._height = height;
168+
if (this.elements === undefined) {
169+
throw 'Must bind plot to DOM element before setting dimensions';
170+
}
171+
for (const el of this.elements) {
172+
el.attr('width', this.width).attr('height', this.height);
173+
el.selectAll('.deepscatter-render-canvas')
174+
.attr('width', this.width)
175+
.attr('height', this.height);
176+
}
177+
if (this._renderer !== undefined) {
178+
this._zoom?.resize(this.width, this.height);
179+
this._renderer.resize(this.width, this.height);
180+
}
181+
}
182+
183+
get width() {
184+
return this._width;
185+
}
186+
187+
set width(w: number) {
188+
this._width = w;
189+
this.resize(w, this.height);
190+
}
191+
192+
get height() {
193+
return this._height;
194+
}
195+
set height(h: number) {
196+
this._height = h;
197+
this.resize(this.width, h);
198+
}
199+
139200
/**
140201
* Attaches the scatterplot to a div element (either as a css selector or as a DOM element).
141202
* This is a permanent relationship.

0 commit comments

Comments
 (0)