简体   繁体   English

Phaser3 仅在全屏时具有不同的缩放行为

[英]Phaser3 different scaling behaviour only in fullscreen

I'm using phaser3 and I'd like to achieve the following behaviour:我正在使用 phaser3,我想实现以下行为:

  • When the game is not in fullscreen, the canvas is NOT resized and is centered horizontally and plays along nicely with the other html elements on the page.当游戏不是全屏时,canvas 不会调整大小并水平居中并与页面上的其他 html 元素很好地配合。
  • When the game is fullscreen, the canvas should be scaled to best fit the current screen resolution.当游戏全屏时,canvas 应缩放以最适合当前屏幕分辨率。 It should also be centered and any black areas should be at the edges.它也应该居中,任何黑色区域都应该在边缘。

I can achieve both of these - but not at the same time.我可以实现这两个——但不能同时实现。

I can center the canvas using either html/css tricks or with Phaser.Scale.CENTER_HORIZONTALLY (or Phaser.Scale.CENTER_BOTH).我可以使用 html/css 技巧或 Phaser.Scale.CENTER_HORIZONTALLY(或 Phaser.Scale.CENTER_BOTH)将 canvas 居中。 But if I just do this, then it's not scaled in fullscreen.但如果我只是这样做,那么它就不会全屏缩放。

If I use Phaser.Scale.FIT, then it works properly in fullscreen.如果我使用 Phaser.Scale.FIT,那么它可以在全屏模式下正常工作。 But now it also tries to scale when not in fullscreen and it messes up the rest of the html page a little bit.但是现在它也尝试在不全屏时进行缩放,并且稍微弄乱了 html 页面的 rest。

So, how do I achieve Phaser.Scale.CENTER_BOTH and Phaser.Scale.FIT behaviour only in fullsceen, and Phaser.Scale.CENTER_HORIZONTALLY when not in fullscreen?那么,如何仅在全屏模式下实现 Phaser.Scale.CENTER_BOTH 和 Phaser.Scale.FIT 行为,而不是在全屏模式下实现 Phaser.Scale.CENTER_HORIZONTALLY 行为?

Is there some combination of phaser3 scaling and/or html divs+css I can use here?我可以在这里使用 Phaser3 缩放和/或 html divs+css 的某种组合吗?

This is the code I currently use (not using any css style options atm):这是我目前使用的代码(不使用任何 css 样式选项 atm):

<div id="gamediv" class="gamedivclass">
  <script src="game.js"></script>
</div>

And in the js file I set up some kind of scale options.在 js 文件中,我设置了某种比例选项。 Eg like this:比如像这样:

var config = {
  // ...
  scale: {
    mode: Phaser.Scale.FIT,
    autocenter: Phaser.Scale.CENTER_BOTH,
    // ...
  }

// ...

var game = new Phaser.Game(config);

Phaser has two events that can come in handy in this situation: Phaser 有两个事件可以在这种情况下派上用场:

Phaser.Scale.Events.ENTER_FULLSCREEN;
Phaser.Scale.Events.LEAVE_FULLSCREEN;

You can listen to those events and "do stuff" , please adapt your code for your real needs, of course.您可以收听这些事件并“做事” ,当然,请根据您的实际需求调整您的代码。

const game = new Phaser.Game(config);

game.scale.on(Phaser.Scale.Events.LEAVE_FULLSCREEN, () => {
  game.scale.scaleMode = Phaser.Scale.NONE;
  game.scale.autoCenter = Phaser.Scale.CENTER_HORIZONTALLY;
});

game.scale.on(Phaser.Scale.Events.ENTER_FULLSCREEN, () => {
  game.scale.scaleMode = Phaser.Scale.FIT;
  game.scale.autoCenter = Phaser.Scale.CENTER_BOTH;
});

Some references:一些参考资料:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM