html - 画布上的相对定位按钮会切断大部分画布
问题描述
我正在尝试使用 JS、HTML 和 CSS 制作游戏,其中我使用 HTML 打开包/库存菜单。但是,当尝试将我的按钮放置在游戏屏幕(画布)的右下方时,画布的很大一部分会被切断,使其无法播放。
这是我看到的,下面是我的代码。
function setup() {
game = new Game;
player = new Player;
controller = new Controller;
canvas.width = 800;
canvas.height = 600;
}
@font-face { font-family: "04b_03"; src: url("assets/fonts/04B_03__.TTF"); }
:root {
--background: 15, 15, 15;
--outline: 255, 160, 30;
}
html, body {
/* Prevent scrollbars */
overflow: hidden;
/* Background */
background-color: rgb(var(--background));
/* Text */
font-family: "04b_03", sans-serif;
/* font-family: "Staatliches", cursive; */
font-size: 200%;
color: white;
}
.container {
position: relative;
width: auto;
height: auto;
}
canvas {
/* Center */
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
/* Border */
border: 10px solid white;
border-radius: 25px;
box-shadow: 0 0 50px rgba(255, 255, 255, 0.4);
/* Rendering */
image-rendering: pixelated;
/* Positioning */
z-index: 1;
}
.bag {
position: absolute;
right: 5%;
bottom: 5%;
z-index: 2;
}
<body>
FIRE
<div class = "container">
<canvas id = "canvas"> Your browser does not support the HTML canvas :( </canvas>
<button class = "bag"> Bag </button>
</div>
</body>
我怎样才能解决这个问题?每当我使用relative
定位时,容器都会缩小,但我不相信还有其他方法可以做到这一点。如果最终最终用户决定缩放他们的屏幕,使用absolute
定位将在按钮位置产生错误,因此我试图寻找一种解决方案,其中按钮将始终保持在画布上的同一位置。
解决方案
没关系,我得到它的工作。我在容器 CSS 上使用了固定定位。这里是:
.container {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
推荐阅读
- javascript - 如何修复视口中的滚动中心?
- python - 如何在 Tkinter 和 Matplotlib 中将日期时间对象格式化为可读标签?
- html - CSS属性中的@是什么?它是在网站代码中定义的?(示例-颜色:@fill;)
- amazon-s3 - localstack:aws cp 无法将文件上传到 S3 存储桶
- oracle-apex - 在 Oracle APEX 中聚焦时创建一个像“10,000.0000”这样的数字字段?
- elixir - 编译 en Elixir 程序时执行的函数
- python - 是否可以在 ipython 中始终运行事件循环,类似于在 jupyter notebook 中的操作方式?
- python - 为什么我用 SMTP Python 发送的消息是空的?
- java - 增强Java中数组中元素出现次数最多的方式
- javascript - 如何将基于类的组件中的函数转换为 React 中的函数式组件?