我有一个完整大小的页面,包含一个顶部工具栏,下面是一个有两列的容器:

  • 右栏
  • 包含画布(在此列中水平居中和垂直居中)的左栏,画布有自己的坐标系(通常为4000 x 3000),应保留此纵横比.这幅画布上有一个"+"按钮.(在我的实际代码中,多个层和多个按钮有几个画布,但这里并不重要)

enter image description here

在这里它nearly可以工作,但我不能使画布占据左列中的全部可用空间.

对于我try 的所有方法(max-width: 100%,max-height: 100%),要么在我们放大/缩小浏览器时失败(70%,80%...150%),要么失败,因为所使用的方法阻止按钮停留在画布的右上角.

TL;DR: how to make this canvas take all available space in the left column, keeping its aspect ratio, keeping the layered buttons, without overflow?

注:我正在寻找一个CSS解决方案,JS不是为定位,但只有图纸.

ctx = document.querySelector("canvas").getContext("2d");
ctx.fillStyle = "#FF0000"; ctx.fillRect(0, 0, 1000, 1000);
ctx.fillStyle = "#00FF00"; ctx.fillRect(1000, 1000, 1000, 1000);
ctx.fillStyle = "#0000FF"; ctx.fillRect(2000, 2000, 1000, 1000);
ctx.fillStyle = "#000000"; ctx.fillRect(3000, 0, 1000, 1000);
* {
  padding: 0;
  margin: 0;
}

.page {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.toolbar {
  background-color: yellow;
  flex: 0 0 5em;
}

.container {
  background-color: orange;
  flex: 1 0 0;
  display: flex;
}

.left {
  background-color: magenta;
  flex: 1 0 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.right {
  background-color: salmon;
  flex: 0 0 10em;
}

.canvas-wrapper {
  background-color: grey;
  display: flex;
  position: relative;
}

canvas {
  height: 100px;
  background-color: white;
}

.button {
  background-color: yellow;
  position: absolute;
  top: 0;
  right: 0;
  width: 1em;
  height: 1em;
}
<div class="page">
  <div class="toolbar">
    TOOLBAR OF HEIGHT 5 EM
  </div>
  <div class="container">
    <div class="left">
      <div class="canvas-wrapper">
        <canvas width="4000" height="3000"></canvas>
        <div class="button">+</div>
      </div>
    </div>
    <div class="right">
      RIGHT OF WIDTH 10 EM
    </div>
  </div>
</div>

PS:您必须在"完整页面"中打开代码片段,才能看到浏览器的放大/缩小功能(CTRL+、CTRL-).

推荐答案

您需要更改一些对齐方式,widthheightflex-grow,并添加一个HTML包装器(即<div class="canvas-wrapper-inner">).

请参见下面的代码片段.

ctx = document.querySelector("canvas").getContext("2d");
ctx.fillStyle = "#FF0000";
ctx.fillRect(0, 0, 1000, 1000);
ctx.fillStyle = "#00FF00";
ctx.fillRect(1000, 1000, 1000, 1000);
ctx.fillStyle = "#0000FF";
ctx.fillRect(2000, 2000, 1000, 1000);
ctx.fillStyle = "#000000";
ctx.fillRect(3000, 0, 1000, 1000);
* {
  padding: 0;
  margin: 0;
}

.page {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.toolbar {
  background-color: yellow;
  flex: 0 0 5em;
}

.container {
  background-color: orange;
  flex: 1 0 0;
  display: flex;
}

.left {
  background-color: magenta;
  flex: 1 0 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.right {
  background-color: salmon;
  flex: 0 0 10em;
}

.canvas-wrapper {
  background-color: grey;
  display: flex;
  position: relative;
  flex-grow: 1; /* Added */
  align-self: stretch; /* Added */
  align-items: center; /* Added */
  justify-content: center; /* Added */
}

canvas {
  width: 100%; /* Added */
  flex-grow: 1; /* Added */
  object-fit: contain; /* Added */
  max-height: calc(100vh - 5em); /* Added */
}

.button {
  background-color: yellow;
  position: absolute;
  top: 0;
  right: 0;
  width: 1em;
  height: 1em;
}

/* Added */
.canvas-wrapper-inner {
  position: relative;
}
<div class="page">
  <div class="toolbar">
    TOOLBAR OF HEIGHT 5 EM
  </div>
  <div class="container">
    <div class="left">
      <div class="canvas-wrapper">
        <div class="canvas-wrapper-inner">
          <canvas width="4000" height="3000"></canvas>
          <div class="button">+</div>
        </div>
      </div>
    </div>
    <div class="right">
      RIGHT OF WIDTH 10 EM
    </div>
  </div>
</div>

Javascript相关问答推荐

是否有方法在OpenWeatherMap API中获取过go 的降水数据?

Chart.js V4切换图表中的每个条,同时每个条下有不同的标签.怎么做?

如何用显示网格平滑地将元素从一个地方移动到另一个地方?

如何使覆盖div与可水平滚动的父div相关?

如何为我的astro页面中的相同组件自动创建不同的内容?

为什么!逗号和空格不会作为输出返回,如果它在参数上?

如何添加绘图条形图图例单击角形事件

如何在模块层面提供服务?

Regex结果包含额外的match/group,只带一个返回

您能在卸载程序(QtInsteller框架)上添加WizardPage吗?

将核心模块导入另一个组件模块时存在多个主题

覆盖TypeScrip中的Lit-Element子类的属性类型

禁用.js文件扩展名并从目录导入隐式根index.js时,找不到NodeJS导入模块

Reaction Redux&Quot;在派单错误中检测到状态Mutations

回溯替代方式

从逗号和破折号分隔的给定字符串中查找所有有效的星期几

Next.js中的服务器端组件列表筛选

如何使用Astro优化大图像?

使用CEPRESS截取时,cy.Wait()在等待5000ms的第一个路由请求时超时

我的NavLink活动类在REACT-ROUTER-V6中出现问题