Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added window size check upon p5 instantiation
This fixes a bug where `windowWidth` and `windowHeight` would represent the correct values when the window was resized and no p5 sketch was active
  • Loading branch information
bensgilbert committed Jul 17, 2024
commit 9b0f03cd3d845748e38faf7c0400cedd1466cbea
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
2 changes: 2 additions & 0 deletions src/core/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@ class p5 {
this._events.devicemotion = null;
}

this.registerMethod('init', this._updateWindowSize);

// Function to invoke registered hooks before or after events such as preload, setup, and pre/post draw.
p5.prototype.callRegisteredHooksFor = function (hookName) {
const target = this || p5.prototype;
Expand Down