Skip to content

Commit 7e0f504

Browse files
committed
Wait for the browser to apply 100% width/height
1 parent 34094ed commit 7e0f504

2 files changed

Lines changed: 16 additions & 11 deletions

File tree

example/index.html

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,12 +343,6 @@
343343
left: 0;
344344
}
345345

346-
#canvas {
347-
position: absolute;
348-
width: 100%;
349-
height: 100%;
350-
}
351-
352346
#modal {
353347
position: absolute;
354348
display: none;

src/index.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -970,8 +970,10 @@ const createScatterplot = (initialProperties = {}) => {
970970
const setHeight = (newHeight) => {
971971
if (newHeight === 'auto') {
972972
height = newHeight;
973-
setCurrentHeight(canvas.getBoundingClientRect().height);
974973
canvas.style.height = '100%';
974+
window.requestAnimationFrame(() => {
975+
if (canvas) setCurrentHeight(canvas.getBoundingClientRect().height);
976+
});
975977
return;
976978
}
977979

@@ -1024,8 +1026,10 @@ const createScatterplot = (initialProperties = {}) => {
10241026
const setWidth = (newWidth) => {
10251027
if (newWidth === 'auto') {
10261028
width = newWidth;
1027-
setCurrentWidth(canvas.getBoundingClientRect().width);
10281029
canvas.style.width = '100%';
1030+
window.requestAnimationFrame(() => {
1031+
if (canvas) setCurrentWidth(canvas.getBoundingClientRect().width);
1032+
});
10291033
return;
10301034
}
10311035

@@ -2429,9 +2433,16 @@ const createScatterplot = (initialProperties = {}) => {
24292433
setDeselectOnEscape(properties.deselectOnEscape);
24302434
}
24312435

2432-
updateViewAspectRatio();
2433-
refresh();
2434-
drawRaf();
2436+
// setWidth and setHeight can be async when width or height are set to
2437+
// 'auto'. And since draw() would have anyway been async we can just make
2438+
// all calls async.
2439+
window.requestAnimationFrame(() => {
2440+
if (!canvas) return; // Instance was destroyed in between
2441+
updateViewAspectRatio();
2442+
refresh();
2443+
draw();
2444+
drawHandler();
2445+
});
24352446
};
24362447

24372448
const hover = (point, showRecticleOnce = false) => {

0 commit comments

Comments
 (0)