admin管理员组文章数量:1353150
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?
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?
Share Improve this question asked Jul 12, 2018 at 15:23 RahnRahn 5,4254 gold badges36 silver badges72 bronze badges 3-
2
Probably something along the lines of
<canvas style="margin:auto;"></canvas>
. It's a CSS issue, not really a Phaser issue. – zero298 Commented Jul 12, 2018 at 15:25 - have you checked this. Does it help? – Federico klez Culloca Commented Jul 12, 2018 at 15:25
- @FedericoklezCulloca Ahh! It helps! – Rahn Commented Jul 12, 2018 at 15:30
4 Answers
Reset to default 5Using 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;
}
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.
In this example the game will be centered but the logo is at 0,0
var game = new Phaser.Game(640, 480, Phaser.AUTO, 'phaser-example', { preload: preload, create: create });
function preload() {
game.load.crossOrigin = "Anonymous";
game.load.image('logo', 'https://cdn.rawgit./photonstorm/phaser-examples/master/examples/assets/sprites/phaser2.png');
}
function create() {
game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
game.scale.pageAlignHorizontally = true;
game.scale.pageAlignVertically = true;
var logo = game.add.sprite(0, 0, 'logo');
}
* {
margin: 0;
padding: 0;
}
<meta name="viewport" content="initial-scale=1 user-scalable=no" />
<script src="https://cdnjs.cloudflare./ajax/libs/phaser-ce/2.11.0/phaser.js"></script>
Use game config options to work with size and scale
const gameConfig: Phaser.Types.Core.GameConfig = {
parent: 'game',
scale: {
mode: Phaser.Scale.RESIZE, // you can find another types in Phaser.Scale.ScaleModeType: RESIZE | FIT | ENVELOP ...
autoCenter: Phaser.Scale.CENTER_BOTH,
},
...yourRest
}
Also I remend to add css to your :
#id {
width: 100vw; // you can skip it
height: 100vh; // you can skip it
display: flex;
align-items: center;
justify-content: center;
canvas {
padding: 0 !important;
margin: 0 !important;
}
}
本文标签: javascriptHow do I move PhaserGame to the center of a browserStack Overflow
版权声明:本文标题:javascript - How do I move Phaser.Game to the center of a browser? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743902410a2558911.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论