How to create a Phaser.Game instance with the right GameConfig options for renderer, scaling, pixel art, FPS, and canvas placement.
Key source paths: ,
,
src/core/typedefs/GameConfig.js
,
,
Related skills: ../scenes/SKILL.md, ../loading-assets/SKILL.md, ../scale-and-responsive/SKILL.md
The simplest possible Phaser 4 game -- a single scene with the default 1024×768 canvas:
triggers the entire boot sequence: config parsing (
), renderer creation, DOM insertion, and the game loop (
). The game waits for
before booting.
The
constructor reads a flat
object and resolves defaults. Some properties can be specified at top level OR nested inside sub-objects (e.g.,
can be top-level or under
). The
sub-object takes priority when both are present.
Render properties can likewise be top-level shortcuts (e.g.,
) or nested under
.
When
is
, Config automatically sets
,
, and
.
js
const config = {
type: Phaser.AUTO,
width: 320,
height: 240,
pixelArt: true,
scale: {
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH,
zoom: Phaser.Scale.ZOOM_2X
},
scene: MyScene
};
js
const config = {
type: Phaser.AUTO,
scale: {
mode: Phaser.Scale.FIT,
parent: 'game-container',
autoCenter: Phaser.Scale.CENTER_BOTH,
width: 1280,
height: 720,
min: { width: 640, height: 360 },
max: { width: 1920, height: 1080 }
},
backgroundColor: '#2d2d2d',
scene: MyScene
};
js
const config = {
type: Phaser.AUTO,
width: 800,
height: 600,
fps: {
target: 60,
limit: 30,
forceSetTimeOut: false,
smoothStep: true
},
scene: MyScene
};
js
const config = {
type: Phaser.AUTO,
width: 800,
height: 600,
scene: [BootScene, PreloadScene, MenuScene, GameScene],
physics: {
default: 'arcade',
arcade: { gravity: { y: 300 } }
}
};
Only the first scene starts automatically. Others start only if they have
in their scene config. See ../scenes/SKILL.md.
js
const config = {
type: Phaser.AUTO,
width: 800,
height: 600,
callbacks: {
preBoot: function (game) {
// Runs before Phaser boots. Game systems not yet available.
},
postBoot: function (game) {
// Runs after boot. All systems ready, game loop starting.
}
},
scene: MyScene
};
js
const config = {
type: Phaser.AUTO,
width: 800,
height: 600,
input: {
keyboard: true,
mouse: true,
touch: false,
gamepad: false,
activePointers: 1,
windowEvents: true
},
disableContextMenu: true,
scene: MyScene
};
These can be set at the top level OR nested under
. The
sub-object takes priority.
Listen on
(or
from a Scene) for visibility and focus changes:
js
game.events.on('blur', () => { /* tab lost focus */ });
game.events.on('focus', () => { /* tab regained focus */ });
game.events.on('hidden', () => { /* Page Visibility API: page hidden */ });
game.events.on('visible', () => { /* Page Visibility API: page visible */ });
game.events.on('pause', () => { /* game.pause() was called */ });
game.events.on('resume', () => { /* game.resume() was called */ });
destroy(removeCanvas, noReturn)
is asynchronous -- it flags the game for destruction on the next frame. Listen for
Phaser.Core.Events.DESTROY
to react to completion.
The
instance exposes key managers as properties, accessible from any Scene via
:
-
scale vs top-level config. ,
,
, and
can be set at the top level or inside
. The
sub-object values take priority. Avoid setting both to prevent confusion.
-
Phaser.WEBGL has no fallback. Unlike
, using
directly will not fall back to Canvas if WebGL is unavailable. The game will fail silently.
-
parent: null vs parent: undefined. (or omitted) appends the canvas to
.
means Phaser will not add the canvas to the DOM at all -- you must do it yourself.
-
transparent overrides backgroundColor. When
, the background color is forced to
regardless of what you set.
-
fps.target does not enforce frame rate. It is advisory only. Use
to actually cap the frame rate. The browser's display refresh rate is always the upper bound.
-
fps.limit only slows down, never speeds up. Setting
on a 60Hz display still results in 60 FPS.
-
DOM Container requires a parent. Setting
dom.createContainer: true
without providing a
element will not work.
-
smoothPixelArt is WebGL-only. It has no effect with the Canvas renderer.
-
window.FORCE_WEBGL and window.FORCE_CANVAS. Config.js checks for these globals at the end of parsing and will override the
setting. This is intended for development/testing.
-
Game.destroy() is asynchronous. It flags the game for destruction on the next frame. Listen for the
event to react to completion. Pass
only if you will never create another Phaser instance on the same page.