gpt4 book ai didi

javascript - 16 :9 Aspect Ratio App & Background (Accounting for space taken by the Nav)

转载 作者:行者123 更新时间:2023-11-27 23:05:29 33 4
gpt4 key购买 nike

好的,我正在为 IOS/Android 创建一个简单的游戏,但我在跨设备布局的一致性方面遇到了一些麻烦。

我的新假设本质上是,如果我以 16:9 的比例开发布局,它将适用于大多数/所有设备。我已经实现了保持游戏外包装的比例,但游戏区域也是 16:9 比例背景图像,我无法正确缩小以填充“游戏区域”

为此,我们假设页面顶部有 30px,底部有 30px 用于导航,这意味着“游戏区域”的高度为 100% - 60px,宽度为 100%,这会导致问题。尽管背景图像的比例为 16:9,但该区域不再是 16:9 的 Canvas 区域。

我的尝试:

  1. 将图像更改为 60px -> 这非常失败
  2. 将背景图像置于“游戏区域”的中心,这还不错,但我们在两侧损失了很多空间。 Background 100% of inner DIV
  3. 使用 Canvas 将图像强制放入该区域,但事情变得模糊:Background Using Canvas
  4. 将背景图像移动到外部包装器/主体。这实际上看起来相当不错!但是我失去了 60px 的图像仍然被 NAV 覆盖。在这里看到:Background 100% of wrapper that is 16:9

关于如何获得更好的结果还有其他想法吗?

这是当前代码:

#Game_Wrapper {
width: 100vw;
height: calc(100vw * 9 / 16);
margin: auto;
position: absolute;
top:0;bottom:0; /* vertical center */
left:0;right:0; /* horizontal center */
padding: 0;
border: 1px solid red;
background: lightblue url('https://i.imgur.com/tRFltCI.png') 0 0/contain no-repeat;
}
#Game_Area {
margin: 0;
padding: 0;
width: 100%;
height: calc(100% - 60px);
}
#GUI_Top {
margin: 0;
padding: 0;
height: 30px;
background: grey;
}
#GUI_Top p {
margin: 0;
padding: 0;
text-align: center;
}
#GUI_Bottom {
margin: 0;
padding: 0;
height: 30px;
background: grey;
}
#GUI_Bottom p {
margin: 0;
padding: 0;
text-align: center;
}
/* 100 * 16/9 = 177.778 */
@media (min-width: 177.778vh) {
#Game_Wrapper {
height: 100vh;
width: calc(100vh * 16 / 9);
}
}
<meta charset="utf-8" name="viewport" content= "width=device-width, initial-scale=1.0">
<body>
<div id="Game_Wrapper">
<!-- This will hold the entire game and GUI -->
<div id="GUI_Top">
<!-- This is where the top GUI will be -->
<p>This is the top GUI / NAV</p>
</div>
<div id="Game_Area">
<!-- This is where you can interact with the actual game -->
</div>
<div id="GUI_Bottom">
<!-- This is where the bottom GUI will be -->
<p>This is the bottom GUI / NAV</p>
</div>
</div>
</body>

最佳答案

正如您已经注意到的,不可能在同样为 16:9 且包含其他元素的容器中保持 16:9 的图像比例。

我的建议是用图像的扩展填充剩余空间,这样我们可能会认为它是同一张图像。

这是它应该看起来的近似值。我只是用相同但模糊的图像填充空白区域。这个想法是找到好的模式,使它在视觉上更好:

#Game_Wrapper {
width: 100vw;
height: calc(100vw * 9 / 16);
margin: auto;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
padding: 0;
border: 1px solid red;
}

#Game_Area {
margin: 0;
padding: 0;
width: 100%;
height: calc(100% - 60px);
background: url('https://i.imgur.com/tRFltCI.png') center/contain no-repeat;
position:relative;
overflow:hidden;
}
#Game_Area:before {
content:"";
position:absolute;
top:-10px;
left:-10px;
right:-10px;
bottom:-10px;
z-index:-1;
background:
url('https://i.imgur.com/tRFltCI.png') right/contain no-repeat,
url('https://i.imgur.com/tRFltCI.png') left/contain no-repeat;
filter:blur(8px);
}


#GUI_Top {
margin: 0;
padding: 0;
height: 30px;
background: grey;
}

#GUI_Top p {
margin: 0;
padding: 0;
text-align: center;
}

#GUI_Bottom {
margin: 0;
padding: 0;
height: 30px;
background: grey;
}

#GUI_Bottom p {
margin: 0;
padding: 0;
text-align: center;
}


/* 100 * 16/9 = 177.778 */

@media (min-width: 177.778vh) {
#Game_Wrapper {
height: 100vh;
width: calc(100vh * 16 / 9);
}
}
<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1.0">

<body>
<div id="Game_Wrapper">
<!-- This will hold the entire game and GUI -->
<div id="GUI_Top">
<!-- This is where the top GUI will be -->
<p>This is the top GUI / NAV</p>
</div>
<div id="Game_Area">
<!-- This is where you can interact with the actual game -->
</div>
<div id="GUI_Bottom">
<!-- This is where the bottom GUI will be -->
<p>This is the bottom GUI / NAV</p>
</div>
</div>
</body>

关于javascript - 16 :9 Aspect Ratio App & Background (Accounting for space taken by the Nav),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58761969/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com