Skip to content

Instantly share code, notes, and snippets.

@teamrun
Last active April 2, 2018 08:32
Show Gist options
  • Select an option

  • Save teamrun/94ddd2e4ff33722d969bd9e83b3dbafe to your computer and use it in GitHub Desktop.

Select an option

Save teamrun/94ddd2e4ff33722d969bd9e83b3dbafe to your computer and use it in GitHub Desktop.
计算屏幕宽高厘米值 get width and height of screen in cm
function getScreenWidthHeight(inch, wRatio = 16, hRatio = 9) {
let diagonalCm = inch * 2.54;
let diagonalRatio = wRatio * wRatio + hRatio * hRatio;
let oneItem = Math.sqrt(diagonalCm * diagonalCm / diagonalRatio);
return {
width: Number((wRatio * oneItem).toFixed(2)),
height: Number((hRatio * oneItem).toFixed(2)),
};
}
// 55寸电视宽高 16:9
getScreenWidthHeight(55); //w: 121.76, h: 68.49
// 150寸幕布宽高 4:3
getScreenWidthHeight(150, 4, 3); //w: 304.8, h: 228.6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment