如何获得屏幕的物理尺寸(即以英寸为单位)? [英] How to get screen's physical size (i.e. in inches)?

查看:443
本文介绍了如何获得屏幕的物理尺寸(即以英寸为单位)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图弄清楚如何通过Javascript获取设备屏幕的物理尺寸。到目前为止,我的结论是,它目前是不可能的,但我希望有人可以证明我错了:)。

I'm trying to figure out how to get the physical dimensions of a device's screen via Javascript. So far, my conclusion is that it's currently impossible, but I hope someone can prove me wrong :).

到目前为止,我试图通过查找设备来获取此信息DPI,但似乎没有办法在这里得到正确的值,因为我测试的所有设备(一些HDPI和XHDPI Android设备,iPhone4S,iPad 2和iPad 3)报告96DPI。

So far I have tried to get this information by finding the device's DPI, but it seems there is no way to get the correct value here, as all devices I have tested (some HDPI & XHDPI Android devices, an iPhone4S, an iPad 2 and an iPad 3) report 96DPI.

获得我尝试的DPI的第一种方法是你可以在互联网上随处找到的方法:创建一个CSS宽度为 div 1in ,获取 clientWidth offsetWidth 并且有你的DPI 。不起作用,所有设备报告96。

The first method of obtaining the DPI I tried is one you can find everywhere on the internet: create a div with a CSS width of 1in, get its clientWidth or offsetWidth and there's your DPI. Doesn't work, all devices report 96.

第二种方法是使用分辨率媒体查询,类似于:

The second method was using the resolution media query, something along the lines of:

for (var i=90; i < 400; i++) {
    if (matchMedia('(resolution: ' + i + 'dpi)').matches) {
       return i;
    }
}

我认为这是一个聪明的解决方案,但遗憾的是也会返回96.

I thought that was a smart solution, but unfortunately that returns 96 as well.

还有什么可以尝试吗?

推荐答案

96DPI是一个与现实无关的网络标准。它测量的英寸最好被认为是逻辑英寸,它对应于字体度量和CSS测量(可以包括点和英寸)。排版中的点定义为1/72英寸,但屏幕停止时始终为72 DPI。因此,现在所有的CSS点真正意味着96点字体高72像素。 (这是逻辑像素,因为这个问题现在被hi-DPI屏幕进一步混淆了。)

96 "DPI" is a web standard that has little to do with reality. The inches it measures are best considered "logical" inches, which correspond to font metrics and CSS measurements (which can include points and inches). A "point" in typography is defined to be 1/72 inch, but screens stopped being consistently 72 DPI ages ago. Thus, all a CSS point really means now is that a 96 point font is 72 pixels tall. (And that's logical pixels, since the issue is now further conflated by hi-DPI screens.)

无论如何,大多数本机操作系统都不知道他们的真实情况物理屏幕大小,因此他们甚至没有关于他们可以通过浏览器向Web应用程序公开的信息。你问的是不可能的。

Anyhow, most native operating systems don't know a thing about their true physical screen size, so they don't even have information about such that they could expose to web apps via a browser. What you're asking isn't possible.

这篇关于如何获得屏幕的物理尺寸(即以英寸为单位)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆