i am trying to center my phaser 3 game. I had to comment out my scale code because everytime i uncomment it the page will turn white. If i comment it the game will show on the left side of the screen but uncenterd. I even tried to add a parent above the autocenter code and called it “myGame”. Then on my html page i added a div in the body and named the id “myGame”. Also didn’t work is there a way i can fix this enter code here?
So, I tried to center my game in the browser (well basically you want to center canvas element). And I was able to center my game just by using css (in my case I used SCSS, but it really doesnt matter).
My config file looks like this
My html looks like this:
My DIV with the ID of “game” is the parent for the canvas.
Then the centering part using SCSS:
I’m not sure if you were trying to achieve this, but it worked for me. My game (or canvas) is in the center of the browser. But if you are going to place more elements in HTML (like some header or footer) than you need to optimize whole CSS thing. Also small tip if your game was working and than after some changes in the code the whole page is white than hit F12 to open browser console, usually there will be some errors. Usually if the error is caused by variable/function there will also be the line on which the error is. If you have more questions, I might be able to help you. Hopefully I solved your problem. Good luck
Oh I see, but when I try this canvas is only centered in horizontal way. In my case canvas is centered both horizontal and vertical but I like your solution, it is simple, looks like I need to step up my CSS game
Assuming, that you are running a portrait game :
scale: {
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH,
},
These, settings will work just fine.
Also, you must also, add :
parent: “className”, as a part of the configurations.
The “className” will be the class_name of the HTML element where you want to inject your Phaser 3 game.
Now, it’s just simple CSS, you can apply to the that