Scale and Responsive Design
How to use the ScaleManager for scaling, centering, fullscreen, orientation handling, and responsive resize in Phaser 4.
Key source paths: src/scale/ScaleManager.js
,
,
,
src/core/typedefs/ScaleConfig.js
Related skills: ../game-setup-and-config/SKILL.md
Quick Start
Scale-to-fit with centering -- the most common setup for responsive games:
js
const config = {
type: Phaser.AUTO,
scale: {
parent: 'game-container',
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH,
width: 800,
height: 600
},
scene: MyScene
};
const game = new Phaser.Game(config);
The
config object is parsed into a
instance which the
reads during boot. The ScaleManager sets the canvas element size and applies CSS scaling to fit it within its parent.
Core Concepts
ScaleManager (src/scale/ScaleManager.js)
The ScaleManager is created during the Game boot sequence and is accessible at
or
from within a Scene. It extends
.
Three internal Size components drive all calculations:
| Component | Property | Purpose |
|---|
| | The unmodified game dimensions from config. Used for world bounds, cameras. Read via / . |
| | The auto-rounded gameSize. Sets the actual and attributes. |
| | The CSS-scaled canvas size after applying scale mode, parent bounds, and zoom. Sets / . |
Scaling works by keeping the canvas element dimensions fixed (baseSize) and stretching it via CSS properties (displaySize). This is equivalent to CSS
but without browser prefix issues.
The
property (
) holds the ratio
and is used internally for input coordinate transformation.
Scale Modes (src/scale/const/SCALE_MODE_CONST.js)
All modes are on
and are set via
in config:
| Constant | Value | Behavior |
|---|
| 0 | No automatic scaling. Canvas uses config width/height. You manage sizing yourself. If you resize the canvas externally, call to update internals. |
| 1 | Height adjusts automatically to maintain aspect ratio based on width. |
| 2 | Width adjusts automatically to maintain aspect ratio based on height. |
| 3 | Scales to fit inside parent while preserving aspect ratio. May leave empty space (letterbox/pillarbox). Most commonly used mode. |
| 4 | Scales to cover the entire parent while preserving aspect ratio. May extend beyond parent bounds (content gets cropped). |
| 5 | Canvas element itself is resized to fill parent. No CSS scaling -- 1:1 pixel mapping. The , , and all change to match parent. Beware of GPU fill-rate on large displays. |
| 6 | Hybrid of RESIZE and FIT. The visible area resizes to fill the parent, and the canvas scales to fit inside that area. Added in v3.80. |
Shorthand constants are also available directly on
:
,
, etc.
Center Modes (src/scale/const/CENTER_CONST.js)
Set via
in config. Centering is achieved by setting CSS
and
on the canvas:
| Constant | Value | Behavior |
|---|
| 0 | No auto-centering (default). |
| 1 | Center horizontally and vertically within parent. |
| 2 | Center horizontally only. |
| 3 | Center vertically only. |
The parent element must have calculable bounds. If the parent has no defined width/height, centering will not work correctly.
Zoom (src/scale/const/ZOOM_CONST.js)
Set via
in config. Multiplies the display size:
| Constant | Value | Behavior |
|---|
| 1 | No zoom (default). |
| 2 | 2x zoom -- good for pixel art at low base resolution. |
| 4 | 4x zoom. |
| -1 | Automatically calculates the largest integer zoom that fits in the parent. |
You can also pass any numeric value. The zoom affects CSS display size but not the canvas resolution.
Orientation (src/scale/const/ORIENTATION_CONST.js)
Read via
. Values are strings:
Phaser.Scale.Orientation.LANDSCAPE
=
Phaser.Scale.Orientation.LANDSCAPE_SECONDARY
=
Phaser.Scale.Orientation.PORTRAIT
=
Phaser.Scale.Orientation.PORTRAIT_SECONDARY
=
Convenience booleans:
,
(device orientation),
game.scale.isGamePortrait
,
game.scale.isGameLandscape
(game dimensions).
Common Patterns
FIT Mode with Centering (Most Common)
js
scale: {
parent: 'game-container',
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH,
width: 1280,
height: 720
}
The game maintains its 16:9 aspect ratio and centers within the parent. Empty space appears as letterbox/pillarbox bars. Style the parent's background color to control bar appearance.
Responsive Resize (Dynamic Canvas Size)
js
scale: {
parent: 'game-container',
mode: Phaser.Scale.RESIZE,
width: '100%',
height: '100%'
}
js
// In your scene -- respond to size changes:
create() {
this.scale.on('resize', this.handleResize, this);
}
handleResize(gameSize, baseSize, displaySize) {
this.cameras.resize(gameSize.width, gameSize.height);
// Reposition UI elements based on new dimensions
}
Width/height accept percentage strings (e.g.,
) which resolve against the parent size. If the parent has no size, they fall back to
/
.
Fullscreen Toggle
js
// Must be called from a pointerup gesture (not pointerdown)
this.input.on('pointerup', () => {
if (this.scale.isFullscreen) {
this.scale.stopFullscreen();
} else {
this.scale.startFullscreen();
}
});
// Or use the convenience method:
this.input.on('pointerup', () => {
this.scale.toggleFullscreen();
});
startFullscreen(fullscreenOptions)
-- requests browser fullscreen. Default options: .
- -- exits fullscreen mode.
toggleFullscreen(fullscreenOptions)
-- toggles between the two.
- -- read-only boolean for current state.
If no
is configured, Phaser creates a temporary
, moves the canvas into it, and sends that div fullscreen. The div is removed when leaving fullscreen.
For iframes, the
attribute is required.
Mobile Orientation Handling
js
create() {
this.scale.on('orientationchange', (orientation) => {
if (orientation === Phaser.Scale.LANDSCAPE) {
// Show game UI
} else {
// Show "rotate device" message
}
});
// Lock orientation (mobile browsers only, limited support):
this.scale.lockOrientation('landscape');
}
Fixed Size (No Scaling)
js
scale: {
mode: Phaser.Scale.NONE,
width: 800,
height: 600
}
In NONE mode, if you change the canvas size externally you must call
game.scale.resize(newWidth, newHeight)
to update all internal components including input coordinates.
Pixel Art with Max Zoom
js
scale: {
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH,
width: 320,
height: 240,
zoom: Phaser.Scale.MAX_ZOOM
},
pixelArt: true
calculates the largest integer multiplier that fits in the parent. Combined with
(which disables anti-aliasing), this gives crisp pixel rendering.
Snap Values for Grid Alignment
js
scale: {
mode: Phaser.Scale.FIT,
width: 800,
height: 600,
snap: { width: 16, height: 16 }
}
When the browser resizes, dimensions snap down to the nearest grid multiple (using floor). Best used with FIT mode. Can also be set at runtime:
game.scale.setSnap(16, 16)
or reset with
.
Min/Max Size Constraints
js
scale: {
mode: Phaser.Scale.FIT,
width: 800,
height: 600,
min: { width: 400, height: 300 },
max: { width: 1600, height: 1200 }
}
The display size is clamped to these bounds during scaling calculations.
Configuration Reference
ScaleConfig (src/core/typedefs/ScaleConfig.js)
All properties go inside the
object in your game config:
| Property | Type | Default | Description |
|---|
| | | Base game width. Strings like resolve against parent size. |
| | | Base game height. Strings like resolve against parent size. |
| | | Canvas zoom factor. Use for auto. |
| | | Parent DOM element or its ID. = . = no parent (you manage canvas placement). |
| | | Allow ScaleManager to set parent/body CSS height to 100%. |
| | | Scale mode constant (see table above). |
| | - | Minimum canvas dimensions. |
| | - | Maximum canvas dimensions. |
| | - | Snap values for resize rounding. |
| | | Floor display/style sizes for low-powered device performance. |
| | | Auto-centering mode (see table above). |
| | | Milliseconds between parent size checks (fallback polling). |
| | | Element to send fullscreen. If not set, Phaser creates a wrapper div. |
Events (src/scale/events/)
All events are on
and are emitted by the ScaleManager instance (
):
| Event | String Value | Callback Signature | When |
|---|
| | (gameSize, baseSize, displaySize, previousWidth, previousHeight)
| Any resize, refresh, or scale change. The three Size parameters have , , . |
| | | Device orientation changes. is a string constant from . |
| | | Browser successfully enters fullscreen. |
| | | Browser leaves fullscreen (via code or user ESC). |
| | | Fullscreen request was denied by the browser. |
| | | Browser does not support the Fullscreen API. |
API Quick Reference
Key Properties (read-only unless noted)
| Property | Type | Description |
|---|
| | Game width (from ). |
| | Game height (from ). |
| | Currently in fullscreen mode. |
| | Device is in portrait orientation. |
| | Device is in landscape orientation. |
| | Game dimensions are taller than wide. |
| | Game dimensions are wider than tall. |
| | Current scale mode constant. |
| | Current orientation string. |
| | Current zoom factor. |
| | Whether sizes are auto-floored. |
| | Current center mode constant. |
| | Computed parent dimensions. |
| | Unmodified game dimensions. |
| | Canvas element dimensions. |
| | CSS-scaled display dimensions. |
| | Ratio of baseSize to canvasBounds (used for input mapping). |
| | The game canvas element. |
| | DOM bounding rect of the canvas. |
| | The parent DOM element. |
| | True if parent is . |
| | Polling interval in ms (writable). |
Key Methods
| Method | Returns | Description |
|---|
| | For NONE mode: sets game, base, and display sizes directly. Updates canvas element and CSS. |
setGameSize(width, height)
| | For scaled modes (FIT, etc.): changes the base game size and re-applies scaling. |
setParentSize(width, height)
| | Manually set parent dimensions (useful when ). |
| | Change zoom factor at runtime. |
| | Set zoom to maximum integer that fits parent. |
setSnap(snapWidth, snapHeight)
| | Set or reset snap grid values. |
| | Calculate (but don't apply) the max zoom. |
getViewPort(camera?, out?)
| | Get the visible area rectangle, optionally for a specific camera. |
| | Force recalculation of scale, bounds, orientation. Emits RESIZE. |
startFullscreen(options?)
| | Enter fullscreen. Must be called from . |
| | Exit fullscreen. |
toggleFullscreen(options?)
| | Toggle fullscreen state. |
lockOrientation(orientation)
| | Attempt to lock screen orientation (mobile only). |
| | Convert DOM pageX to game coordinate. |
| | Convert DOM pageY to game coordinate. |
| | Recalculate parent size. Returns true if changed. |
| | Recalculate and apply centering margins. |
| | Update from the canvas bounding client rect. |
Gotchas
-
Parent element must have dimensions. The ScaleManager relies on the parent's computed CSS size. An unstyled
has zero height, which breaks centering and scaling. Either give it explicit CSS dimensions or let
(the default) set body/parent to 100% height.
-
No padding on the parent. The ScaleManager does not account for parent padding. Apply padding to a wrapper element instead, or use margins on the parent's parent.
-
Do not style the canvas directly. The ScaleManager controls
,
,
, and
. External CSS on the canvas will conflict.
-
Fullscreen requires , not . On touch devices,
fullscreen requests are blocked unless the document already received touch input. Always use
for reliable behavior.
-
Fullscreen in iframes needs . Without this attribute on the iframe element, fullscreen requests will silently fail.
-
RESIZE mode and GPU fill-rate. RESIZE creates a 1:1 pixel canvas matching the parent. On high-resolution displays this can be very large. Monitor performance on low-end devices.
-
vs . Use
only in NONE mode (it sets everything directly). Use
when using FIT/ENVELOP/etc. (it preserves the scale mode calculations).
-
iOS height quirks. When the parent is the window on iOS, the ScaleManager uses
to work around Safari's dynamic toolbar affecting
.
-
Percentage strings require a sized parent. Setting
or
resolves against
. If the parent has no size, it falls back to
/
.
-
is a fallback poll. Modern browsers dispatch
events, but the ScaleManager also polls every
ms (default 500) to catch edge cases on older browsers.
Source File Map
| File | Purpose |
|---|
src/scale/ScaleManager.js
| Main class -- all scaling, centering, fullscreen, orientation logic. Access via . |
src/scale/const/SCALE_MODE_CONST.js
| Scale mode constants: NONE, WIDTH_CONTROLS_HEIGHT, HEIGHT_CONTROLS_WIDTH, FIT, ENVELOP, RESIZE, EXPAND. |
src/scale/const/CENTER_CONST.js
| Center mode constants: NO_CENTER, CENTER_BOTH, CENTER_HORIZONTALLY, CENTER_VERTICALLY. |
src/scale/const/ZOOM_CONST.js
| Zoom constants: NO_ZOOM, ZOOM_2X, ZOOM_4X, MAX_ZOOM. |
src/scale/const/ORIENTATION_CONST.js
| Orientation string constants: LANDSCAPE, LANDSCAPE_SECONDARY, PORTRAIT, PORTRAIT_SECONDARY. |
src/scale/events/RESIZE_EVENT.js
| Resize event definition. Callback receives gameSize, baseSize, displaySize, previousWidth, previousHeight. |
src/scale/events/ENTER_FULLSCREEN_EVENT.js
| Emitted on successful fullscreen entry. |
src/scale/events/LEAVE_FULLSCREEN_EVENT.js
| Emitted when leaving fullscreen. |
src/scale/events/FULLSCREEN_FAILED_EVENT.js
| Emitted when fullscreen request is denied. |
src/scale/events/FULLSCREEN_UNSUPPORTED_EVENT.js
| Emitted when browser lacks Fullscreen API support. |
src/scale/events/ORIENTATION_CHANGE_EVENT.js
| Emitted on device orientation change. Callback receives orientation string. |
src/core/typedefs/ScaleConfig.js
| JSDoc typedef for the config object. |
| The Size component class used by gameSize, baseSize, displaySize. |
src/dom/GetInnerHeight.js
| iOS-specific workaround for getting accurate viewport height. |
src/dom/GetScreenOrientation.js
| Determines current screen orientation from dimensions. |