From 3b8d019b972cdfec8b3ddb35183deb31a7c3c263 Mon Sep 17 00:00:00 2001 From: Rex Date: Sun, 31 Dec 2023 17:11:38 +0800 Subject: [PATCH] Add EXPAND scale mode --- src/scale/ScaleManager.js | 49 ++++++++++++++++++++++++++++- src/scale/const/SCALE_MODE_CONST.js | 13 +++++++- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/src/scale/ScaleManager.js b/src/scale/ScaleManager.js index c30db4f73..17e4d80ca 100644 --- a/src/scale/ScaleManager.js +++ b/src/scale/ScaleManager.js @@ -431,7 +431,7 @@ var ScaleManager = new Class({ this.fullscreen = game.device.fullscreen; - if (this.scaleMode !== CONST.SCALE_MODE.RESIZE) + if ((this.scaleMode !== CONST.SCALE_MODE.RESIZE) && (this.scaleMode !== CONST.SCALE_MODE.EXPAND)) { this.displaySize.setAspectMode(this.scaleMode); } @@ -1047,6 +1047,53 @@ var ScaleManager = new Class({ this.canvas.width = styleWidth; this.canvas.height = styleHeight; } + else if (this.scaleMode === CONST.SCALE_MODE.EXPAND) + { + // Resize to match parent, like RESIZE mode + + // This will constrain using min/max + this.displaySize.setSize(this.parentSize.width, this.parentSize.height); + + styleWidth = this.displaySize.width; + styleHeight = this.displaySize.height; + + if (autoRound) + { + styleWidth = Math.floor(styleWidth); + styleHeight = Math.floor(styleHeight); + } + + style.width = styleWidth + 'px'; + style.height = styleHeight + 'px'; + + + // Expand canvas size to fit game size's width or height + + var scaleX = this.parentSize.width / this.gameSize.width; + + var scaleY = this.parentSize.height / this.gameSize.height; + + if (scaleX < scaleY) + { + this.baseSize.setSize(this.gameSize.width, this.parentSize.height / scaleX); + } + else + { + this.baseSize.setSize(this.displaySize.width / scaleY, this.gameSize.height); + } + + styleWidth = this.baseSize.width; + styleHeight = this.baseSize.height; + + if (autoRound) + { + styleWidth = Math.floor(styleWidth); + styleHeight = Math.floor(styleHeight); + } + + this.canvas.width = styleWidth; + this.canvas.height = styleHeight; + } else { // All other scale modes diff --git a/src/scale/const/SCALE_MODE_CONST.js b/src/scale/const/SCALE_MODE_CONST.js index bee58fb8c..6362301a0 100644 --- a/src/scale/const/SCALE_MODE_CONST.js +++ b/src/scale/const/SCALE_MODE_CONST.js @@ -87,6 +87,17 @@ module.exports = { * @const * @since 3.16.0 */ - RESIZE: 5 + RESIZE: 5, + + /** + * The Canvas's visible area is resized to fit all available _parent_ space like RESIZE mode, + * and scale canvas size to fit inside the visible area like FIT mode. + * + * @name Phaser.Scale.ScaleModes.EXPAND + * @type {number} + * @const + * @since 3.70.1 + */ + EXPAND : 6 };