Skip to content

Commit 70491bc

Browse files
committed
fix(viewer): own canvas sizing and initial render since session no longer installs resize observer
1 parent b5634e9 commit 70491bc

1 file changed

Lines changed: 28 additions & 2 deletions

File tree

src/viewer.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ import JSZip from "jszip";
88
import { loadAsync } from "./runtime";
99
import { createFuture } from "./core/future";
1010

11+
function debounce(func, delay = 100) {
12+
let timeoutId;
13+
14+
return function (...args) {
15+
clearTimeout(timeoutId);
16+
timeoutId = setTimeout(func, delay, ...args);
17+
};
18+
}
19+
1120
export class ExportViewer {
1221
constructor(containerSelector, remoting) {
1322
this.remoting = remoting;
@@ -66,10 +75,27 @@ export class ExportViewer {
6675
"position: absolute; left: 0; top: 0; width: 100%; height: 100%;",
6776
);
6877
this.container.appendChild(canvas);
69-
7078
const target = this.remoting.bindCanvas(rwId, canvas);
7179
this.remoting.native.bindRenderWindow(rwId, target);
72-
this.remoting.native.startEventLoop(rwId);
80+
this.remoting.boundRenderWindows.add(rwId);
81+
82+
// RemoteSession opts out of VTK's own canvas sizing (resize observer /
83+
// expand-to-container), so the viewer owns it: size the render window to
84+
// the canvas now (this also triggers the first render) and on every resize.
85+
const resize = () => {
86+
const { width, height } = canvas.getBoundingClientRect();
87+
const dpr = window.devicePixelRatio || 1;
88+
this.remoting.setSizeAsync(
89+
rwId,
90+
Math.max(1, Math.round(width * dpr)),
91+
Math.max(1, Math.round(height * dpr)),
92+
);
93+
};
94+
this.resizeObserver = new ResizeObserver(debounce(resize));
95+
this.resizeObserver.observe(canvas);
96+
resize();
97+
98+
this.remoting.startEventLoop(rwId);
7399
}
74100
}
75101

0 commit comments

Comments
 (0)