Skip to content

Commit

Permalink
Merge pull request #7134 from Evorage0/main
Browse files Browse the repository at this point in the history
Added window size check upon p5 instantiation
  • Loading branch information
davepagurek authored Jul 19, 2024
2 parents 16d1bd0 + d8b0fa5 commit e992412
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/core/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ p5.prototype.displayHeight = screen.height;
* @alt
* This example does not render anything.
*/
p5.prototype.windowWidth = getWindowWidth();
p5.prototype.windowWidth = 0;

/**
* A `Number` variable that stores the height of the browser's viewport.
Expand Down Expand Up @@ -703,7 +703,7 @@ p5.prototype.windowWidth = getWindowWidth();
* @alt
* This example does not render anything.
*/
p5.prototype.windowHeight = getWindowHeight();
p5.prototype.windowHeight = 0;

/**
* A function that's called when the browser window is resized.
Expand Down Expand Up @@ -800,6 +800,15 @@ function getWindowHeight() {
);
}

/**
* Called upon each p5 instantiation instead of module import due to the
* possibility of the window being resized when no sketch is active.
*/
p5.prototype._updateWindowSize = function() {
this._setProperty('windowWidth', getWindowWidth());
this._setProperty('windowHeight', getWindowHeight());
};

/**
* A `Number` variable that stores the width of the canvas in pixels.
*
Expand Down
3 changes: 3 additions & 0 deletions src/core/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,9 @@ class p5 {
}
};

// ensure correct reporting of window dimensions
this._updateWindowSize();

// call any registered init functions
this._registeredMethods.init.forEach(function(f) {
if (typeof f !== 'undefined') {
Expand Down

0 comments on commit e992412

Please sign in to comment.