获得容器标准 16:9 实际宽高

const getStandardRatio = (width, height, widthRatio = 16, heightRatio = 9) => {
  // 实际宽高比
  const computedRatio = width / height;
  // 标准宽高比
  const standardRatio = widthRatio / heightRatio;
  // 以高为准
  if (computedRatio >= standardRatio) {
    const computedWidth = (height * widthRatio) / heightRatio;
    return {
      width: computedWidth,
      height,
    };
  }
  // 以宽为准
  const computedHeight = (width * heightRatio) / widthRatio;
  return {
    width,
    height: computedHeight,
  };
};

  

posted @ 2023-12-24 00:29  671_MrSix  阅读(3)  评论(0编辑  收藏  举报