@@ -793,20 +793,58 @@ p5.prototype._onresize = function(e) {
793
793
// and visual viewport). This ordering prefers mobile-friendly measurements while retaining
794
794
// sensible fallbacks for desktop browsers.
795
795
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
+
796
818
return (
797
- ( window . visualViewport && window . visualViewport . width ) ||
798
- ( document . documentElement && document . documentElement . clientWidth ) ||
799
819
window . innerWidth ||
820
+ ( document . documentElement && document . documentElement . clientWidth ) ||
800
821
( document . body && document . body . clientWidth ) ||
801
822
0
802
823
) ;
803
824
}
804
825
805
826
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
+
806
845
return (
807
- ( window . visualViewport && window . visualViewport . height ) ||
808
- ( document . documentElement && document . documentElement . clientHeight ) ||
809
846
window . innerHeight ||
847
+ ( document . documentElement && document . documentElement . clientHeight ) ||
810
848
( document . body && document . body . clientHeight ) ||
811
849
0
812
850
) ;
0 commit comments