Loading...
Loading...
Use this skill when creating a new Phaser 4 game instance or configuring GameConfig options. Covers renderer selection, canvas setup, scaling, pixel art, FPS settings, boot sequence, and all config sub-objects. Triggers on: new Phaser.Game, GameConfig, game setup, renderer, pixel art, FPS.
npx skill4agent add phaserjs/phaser game-setup-and-configHow to create a Phaser.Game instance with the right GameConfig options for renderer, scaling, pixel art, FPS, and canvas placement.
src/core/Game.jssrc/core/Config.jssrc/core/typedefs/GameConfig.jssrc/const.jssrc/scale/const/class MyScene extends Phaser.Scene {
preload() {
this.load.image('logo', 'assets/logo.png');
}
create() {
this.add.image(400, 300, 'logo');
}
}
const config = {
type: Phaser.AUTO,
scene: MyScene
};
const game = new Phaser.Game(config);new Phaser.Game(config)ConfigTimeStepDOMContentLoadednew Phaser.Game(config)Phaser.Core.ConfigAnimationManagerTextureManagerCacheManagerInputManagerSceneManagerScaleManagerSoundManagerTimeStepPluginManagerDOMContentLoadedboot()boot()CreateRendererAddToDOMBOOTTextureManagerREADYREADYstart()start()TimeStepVisibilityHandlerconfig.postBootConfigGameConfigwidthscale.widthscalepixelArt: truerender| Constant | Value | Behavior |
|---|---|---|
| | WebGL if supported, else falls back to Canvas |
| | Force Canvas renderer |
| | Force WebGL -- no fallback if unsupported |
| | No renderer -- DOM still required. For unit testing only |
config.typePhaser.AUTOpixelArttrueantialias: falseantialiasGL: falseroundPixels: trueconst 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
};const config = {
type: Phaser.WEBGL,
width: 320,
height: 240,
smoothPixelArt: true,
scene: MyScene
};smoothPixelArttrueantialias: trueantialiasGL: truepixelArt: falseconst config = {
type: Phaser.AUTO,
scale: {
mode: Phaser.Scale.RESIZE,
parent: 'game-container',
width: '100%',
height: '100%'
},
scene: MyScene
};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
};const config = {
type: Phaser.AUTO,
width: 800,
height: 600,
fps: {
target: 60,
limit: 30,
forceSetTimeOut: false,
smoothStep: true
},
scene: MyScene
};const config = {
type: Phaser.AUTO,
width: 800,
height: 600,
transparent: true,
parent: 'game-container',
scene: MyScene
};transparenttruebackgroundColor0x0000000const canvas = document.getElementById('my-canvas');
const config = {
type: Phaser.WEBGL,
canvas: canvas,
width: 800,
height: 600,
scene: MyScene
};const config = {
type: Phaser.AUTO,
width: 800,
height: 600,
scene: [BootScene, PreloadScene, MenuScene, GameScene],
physics: {
default: 'arcade',
arcade: { gravity: { y: 300 } }
}
};{ active: true }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
};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
};| Property | Type | Default | Description |
|---|---|---|---|
| | | Renderer: |
| | | Game width in pixels (or |
| | | Game height in pixels (or |
| | | Canvas zoom multiplier. Overridden by |
| | | DOM element or |
| | | Provide your own canvas element |
| | | Provide your own rendering context |
| | | CSS styles applied to the canvas element |
| | | Skip feature detection for non-browser environments. |
| | | Scene class (extends Phaser.Scene) or array of scene classes |
| | | Seed for |
| | | Game title shown in console banner |
| | | Game URL shown in console banner |
| | | Game version shown in console banner |
| | | Auto-focus window on boot and mousedown |
| | | |
| | | Disable right-click context menu |
| | | Canvas background color. Accepts hex number, CSS string, or color object |
| | (shown) | |
| Property | Type | Default | Description |
|---|---|---|---|
| | | Base game width |
| | | Base game height |
| | | Zoom multiplier. Use constants: |
| | | DOM parent for the canvas |
| | | Scale mode (see table below) |
| | | Allow adjusting parent CSS height to 100% |
| | | Round display/style sizes for performance |
| | | Canvas centering mode (see table below) |
| | | Milliseconds between browser size checks |
| | | Element for fullscreen mode |
| | | Minimum canvas dimensions |
| | | Maximum canvas dimensions (0 = no limit) |
| | | Snap canvas size to multiples |
| Constant | Value | Behavior |
|---|---|---|
| | No scaling. Canvas stays at config size |
| | Height adjusts based on width |
| | Width adjusts based on height |
| | Fit inside parent keeping aspect ratio (letterboxing) |
| | Cover parent keeping aspect ratio (may overflow) |
| | Canvas resizes to fill parent, ignoring aspect ratio |
| | Like RESIZE for visible area + FIT for canvas scale |
| Constant | Value |
|---|---|
| |
| |
| |
| |
| Constant | Value | Behavior |
|---|---|---|
| | No zoom |
| | 2x zoom |
| | 4x zoom |
| | Max integer zoom that fits in parent |
renderrender| Property | Type | Default | Description |
|---|---|---|---|
| | | Linear interpolation for scaled/rotated textures |
| | | Antialias on WebGL context creation |
| | | Sets |
| | | WebGL only. Blocky pixels with smooth edges |
| | | Snap texture-based objects to integer positions |
| | | Transparent canvas background |
| | | Clear canvas each frame |
| | | Preserve WebGL buffers between frames |
| | | Pre-multiplied alpha in WebGL drawing buffer |
| | | Desynchronized rendering context |
| | | Abort WebGL if browser reports poor performance |
| | | |
| | | Max quads per WebGL batch |
| | | Max GPU textures. |
| | | Max lights visible per camera |
| | | Restrict to 1 texture per batch on iOS/Android |
| | | Self-shadowing on lit textured objects |
| | | Point-combining threshold for Graphics WebGL paths |
| | | Skip drawing objects whose shader is still compiling |
| | | Mipmap filter: |
| | | Custom render nodes for WebGL renderer |
| Property | Type | Default | Description |
|---|---|---|---|
| | | Minimum acceptable FPS |
| | | Target FPS (informational, does not enforce) |
| | | Enforce max FPS. |
| | | Use |
| | | Frames to average for delta smoothing |
| | | Frames before trusting delta again after a panic |
| | | Apply delta smoothing |
| Sub-Config Key | Type | Source |
|---|---|---|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
game.eventsthis.game.eventsgame.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 */ });Phaser.Core.Events.BLURFOCUSHIDDENVISIBLEPAUSERESUME// Pause the entire game loop (rendering and updates stop)
game.pause();
// Resume the game loop
game.resume();
// Check if the game is currently paused
if (game.isPaused) {
// game loop is not running
}// Remove the canvas from the DOM and clean up
game.destroy(true);
// Full cleanup -- pass noReturn=true if you will NEVER create another Phaser instance
// This allows the framework to release additional internal references
game.destroy(true, true);destroy(removeCanvas, noReturn)Phaser.Core.Events.DESTROYGamethis.game| Property | Type | Description |
|---|---|---|
| | Parsed GameConfig (read-only after boot) |
| | Start, stop, switch, and manage all scenes |
| | Shared data store across all scenes |
| | Global animation definitions |
| | Stores loaded non-texture assets (JSON, XML, etc.) |
| | Stores all loaded textures and sprite sheets |
| | Global sound playback manager |
// Example: access the registry from any scene
this.game.registry.set('highScore', 9999);
// Example: access the scene manager
this.game.scene.start('MenuScene');widthheightzoomparentscalescaleAUTOPhaser.WEBGLundefineddocument.bodynulltransparent: truergba(0,0,0,0)fps.limitlimit: 120dom.createContainer: trueparenttypeDESTROYnoReturn: true1024800768600Phaser.Scale.EXPAND6loader.maxRetries20render.renderNodes| Concept | File |
|---|---|
| Game class / boot sequence | |
| Config parsing / all defaults | |
| GameConfig typedef | |
| Renderer constants (AUTO, WEBGL, CANVAS, HEADLESS) | |
| FPSConfig typedef | |
| RenderConfig typedef | |
| ScaleConfig typedef | |
| Scale mode constants | |
| Center constants | |
| Zoom constants | |
| TimeStep (game loop) | |
| Renderer creation | |
| ScaleManager | |
| CallbacksConfig typedef | |
| DOMContainerConfig typedef | |
| InputConfig typedef | |
| LoaderConfig typedef | |
| All core typedefs | |