根据 Matanster 的回答,他观察了 SVG 属性,例如screenPixelToMillimeterX
,我做了一个测试,看看它们是否有效:
(编辑:这些属性从规范中删除(或从未删除),浏览器可能不再拥有(或从未拥有)它们,使我们远离了解实际物理显示大小)。
// on Macbook Pro Retina (2880x1800, 15.4"), is the calculated diagonal size
// approximately 15.4? Let's see...
var svgEl = document.createElementNS("http://www.w3.org/2000/svg", "svg");
var screenWidthMillimeters = svgEl.screenPixelToMillimeterX * 2880;
var screenHeightMillimeters = svgEl.screenPixelToMillimeterY * 1800;
var screenDiagonalMillimeters = Math.sqrt(Math.pow(screenWidthMillimeters, 2) + Math.pow(screenHeightMillimeters, 2)); // pythagorean theorem
var screenDiagonalInches = (screenDiagonalMillimeters / 10 / 2.54); // (mm / 10mm/cm) / 2.54cm/in = in
console.log("The calculated diagonal of the screen is "+screenDiagonalInches+" inches. \nIs that close to the actual 15.4\"?");
这是输出:
The calculated diagonal of the screen is 35.37742738560738 inches.
Is that close to the actual value of 15.4?
不。
似乎还没有办法在网络浏览器中获得真实的物理值。