Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I move Phaser.Game to the center of a browser?

enter image description here

I'm new to JS/phaser and just create a Phaser.Game with

var game = new Phaser.Game(600, 490);

The game is created top left but I would like to have it placed in the center of a browser.

I checked the API and nothing looks relevant:

new Game(width, height, renderer, parent, state, transparent, antialias, physicsConfig)

Could someone tell me how do I do so?

like image 643
Rahn Avatar asked Dec 03 '25 23:12

Rahn


2 Answers

Using Laravel 8 and [email protected]

The solution found here worked for me. The other answers provided in this thread did not (but did point me in the correct direction).

canvas {
display : block;   
margin : auto;
}
like image 107
Nyk Avatar answered Dec 06 '25 13:12

Nyk


Thanks to this link provided by @FedericoklezCulloca.

add

game.scale.pageAlignHorizontally = true;
game.scale.pageAlignVertically = true;
game.scale.refresh();

to function update and the Phaser.Game will be in the middle.

like image 37
Rahn Avatar answered Dec 06 '25 15:12

Rahn