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

发布时间 2023-12-24 00:33:05作者: 671_MrSix
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,
  };
};