Skip to content

Commit 4b5ee8b

Browse files
committed
env: prefer visualViewport.segments and include scrollbars when available
Prefer experimental visualViewport.segments[0] and visualViewport before window.inner* so measurements include scrollbars and handle orientation changes when available. Guard experimental access and fall back to documentElement.client* for compatibility.
1 parent fb2f003 commit 4b5ee8b

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

src/core/environment.js

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -793,20 +793,58 @@ p5.prototype._onresize = function(e) {
793793
// and visual viewport). This ordering prefers mobile-friendly measurements while retaining
794794
// sensible fallbacks for desktop browsers.
795795
function getWindowWidth() {
796+
// Prefer including scrollbars when possible per contributor guidance.
797+
// Order: experimental visualViewport.segments (may include scrollbars/handle rotation),
798+
// visualViewport.width, window.innerWidth (includes scrollbars),
799+
// documentElement.clientWidth (excludes scrollbars), document.body.clientWidth.
800+
try {
801+
if (window.visualViewport) {
802+
const segs = window.visualViewport.segments;
803+
if (Array.isArray(segs) && segs.length > 0) {
804+
const w = segs[0] && typeof segs[0].width === 'number' ? segs[0].width : null;
805+
if (w && w > 0) {
806+
return w;
807+
}
808+
}
809+
810+
if (typeof window.visualViewport.width === 'number' && window.visualViewport.width > 0) {
811+
return window.visualViewport.width;
812+
}
813+
}
814+
} catch (e) {
815+
// experimental access may throw; ignore and continue with fallbacks
816+
}
817+
796818
return (
797-
(window.visualViewport && window.visualViewport.width) ||
798-
(document.documentElement && document.documentElement.clientWidth) ||
799819
window.innerWidth ||
820+
(document.documentElement && document.documentElement.clientWidth) ||
800821
(document.body && document.body.clientWidth) ||
801822
0
802823
);
803824
}
804825

805826
function getWindowHeight() {
827+
try {
828+
if (window.visualViewport) {
829+
const segs = window.visualViewport.segments;
830+
if (Array.isArray(segs) && segs.length > 0) {
831+
const h = segs[0] && typeof segs[0].height === 'number' ? segs[0].height : null;
832+
if (h && h > 0) {
833+
return h;
834+
}
835+
}
836+
837+
if (typeof window.visualViewport.height === 'number' && window.visualViewport.height > 0) {
838+
return window.visualViewport.height;
839+
}
840+
}
841+
} catch (e) {
842+
// experimental access may throw; ignore and continue with fallbacks
843+
}
844+
806845
return (
807-
(window.visualViewport && window.visualViewport.height) ||
808-
(document.documentElement && document.documentElement.clientHeight) ||
809846
window.innerHeight ||
847+
(document.documentElement && document.documentElement.clientHeight) ||
810848
(document.body && document.body.clientHeight) ||
811849
0
812850
);

0 commit comments

Comments
 (0)