docs.rodeo

MDN Web Docs mirror

Scaling

{{GamesSidebar}} 

{{PreviousNext("Games/Workflows/2D_Breakout_game_Phaser/Initialize_the_framework", "Games/Workflows/2D_Breakout_game_Phaser/Load_the_assets_and_print_them_on_screen")}} 

This is the 2nd step out of 16 of the Gamedev Phaser tutorial. You can find the source code as it should look after completing this lesson at Gamedev-Phaser-Content-Kit/demos/lesson02.html.

Scaling refers to how the game canvas will scale on different screen sizes. We can make the game scale to fit on any screen size automatically during the preload stage, so we don’t have to worry about it later.

The Phaser scale object

There’s a special scale object available in Phaser with a few handy methods and properties available. Update your existing preload() function as follows:

function preload() {
  game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
  game.scale.pageAlignHorizontally = true;
  game.scale.pageAlignVertically = true;
}

scaleMode has a few different options available for how the Canvas can be scaled:

The other two lines of code in the preload() function are responsible for aligning the canvas element horizontally and vertically, so it is always centered on screen regardless of size.

Adding a custom canvas background color

We can also add a custom background color to our canvas, so it won’t stay black. The stage object has a backgroundColor property for this purpose, which we can set using CSS color definition syntax. Add the following line below the other three you added earlier:

game.stage.backgroundColor = "#eee";

Compare your code

You can check the finished code for this lesson in the live demo below, and play with it to understand better how it works:

{{JSFiddleEmbed("https://jsfiddle.net/end3r/6a64vecL/","","400")}} 

Next steps

Now we’ve set up the scaling for our game, let’s continue to the third lesson and work out how to load the assets and print them on screen.

{{PreviousNext("Games/Workflows/2D_Breakout_game_Phaser/Initialize_the_framework", "Games/Workflows/2D_Breakout_game_Phaser/Load_the_assets_and_print_them_on_screen")}} 

In this article

View on MDN