我已经为我的问题准备了a simple test web page at Github(使用main.css文件):

animated screenshot

在垂直flexbox布局中,我try 使用ResizeObserver来查看具有flex-grow:1的父dis元素,然后在其子canvas元素上保持1:1的长宽比.

我试图保持canvas长宽比的原因是in my real app a PixiJS canvas is embedded in a ReactJS app,如果我只是zoom canvas元素以填充dis父级,那么PixiJS内容就会被拉伸:

screenshot

不幸的是,parent迪夫元素将hint迪夫元素从屏幕底部推出-

我认为,我的代码中缺少一些小东西,请推荐一个修复方法:

const parentElement = document.getElementById("parent");
const childElement = document.getElementById("child");

const resizeObserver = new ResizeObserver((entries) => {
  for (let entry of entries) {
    const { width, height } = entry.contentRect;
    const minDimension = Math.min(width, height);
    console.log(
      `parent: ${width} x ${height} -> child: ${minDimension} x ${minDimension}`
    );
    childElement.style.width = `${minDimension}px`;
    childElement.style.height = `${minDimension}px`;
  }
});

resizeObserver.observe(parentElement);
html,
body {
  margin: 0;
  padding: 0;
  overflow: hidden;
}

.flexRoot {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: stretch;
  width: 100%;
  height: 100vh;
}

/* the parent holds: status, canvas, hint */
.parent {
  border: 4px solid red;
  flex-grow: 1;
}

canvas {
  width: 1020px;
  height: 1020px;
  background-color: yellow;
  border: 4px green dotted;
  box-sizing: border-box;
}

.hint,
.status {
  background: lightblue;
  font-style: italic;
  text-align: center;
  flex-grow: 0;
}
<div class="flexRoot">
  <div class="status">Game #1 Score1:Score2</div>
  <div class="parent" id="parent">
    <canvas id="child"></canvas>
  </div>
  <div class="hint">A game hint to do this and that...</div>
</div>

推荐答案

为了确保.parent的大小由.flexRoot而不是#child决定,不应该允许溢出:

.parent {
  overflow: hidden;
}

Optional:画布使用flex对中.

const parentElement = document.getElementById("parent");
const childElement = document.getElementById("child");

const resizeObserver = new ResizeObserver((entries) => {
  for (let entry of entries) {
    const {
      width,
      height
    } = entry.contentRect;
    const minDimension = Math.min(width, height);
    console.log(
      `parent: ${width} x ${height} -> child: ${minDimension} x ${minDimension}`
    );
    childElement.style.width = `${minDimension}px`;
    childElement.style.height = `${minDimension}px`;
  }
});

resizeObserver.observe(parentElement);
html,
body {
  margin: 0;
  padding: 0;
  overflow: hidden;
}

.flexRoot {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: stretch;
  width: 100%;
  height: 100vh;
}


/* the parent holds: status, canvas, hint */

.parent {
  border: 4px solid red;
  flex-grow: 1;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

canvas {
  width: 1020px;
  height: 1020px;
  background-color: yellow;
  border: 4px green dotted;
  box-sizing: border-box;
}

.hint,
.status {
  background: lightblue;
  font-style: italic;
  text-align: center;
  flex-grow: 0;
}
<div class="flexRoot">
  <div class="status">Game #1 Score1:Score2</div>
  <div class="parent" id="parent">
    <canvas id="child"></canvas>
  </div>
  <div class="hint">A game hint to do this and that...</div>
</div>

Javascript相关问答推荐

setFinder关闭模式并重定向到url

详细说明如何以图表方式改变仪表的范围和 colored颜色

foreach循环中的Typescript字符串索引

如何分配类型脚本中具有不同/额外参数的函数类型

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

通过嵌套模型对象进行Mongoose搜索

MongoDB中的引用

React 17与React 18中的不同setState行为

使用javascript将Plotly Expandable Sparkline转换为HighCharter Plot在bslib卡中

在网页上添加谷歌亵渎词

康威的生活游戏规则在我的Javascript版本中不起作用.''我做错了什么?

数字时钟在JavaScript中不动态更新

在浏览器中触发插入事件时检索编码值的能力

为什么可选参数的顺序会导致问题?

使用插件构建包含chart.js提供程序的Angular 库?

<;img>;标记无法呈现图像

处理app.param()中的多个参数

如何根据输入数量正确显示alert ?

如何更改Html元素S的 colored颜色 ,然后将其褪色为原始 colored颜色

不协调嵌入图片