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 :).
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.
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;
}
}
I thought that was a smart solution, but unfortunately that returns 96 as well.
Is there anything left that I can try?
Check the laptop's specifications for a section labeled "Screen," "Display," or similar. The screen size is listed in that section, in inches. If you're still unsure, a quick online search of the laptop's model number provides you the exact size.
Assuming the screen's length is the arc length of a circle, we can find the base depth and width by using the following formulas which we use in the screen size calculator: base depth = radius * (1 - cos(length / (2 * radius))) ; base width = 2 * radius * sin(length / (2 * radius)) .
Display display = getWindowManager(). getDefaultDisplay(); Point size = new Point(); display. getSize(size); int width = size. x; int height = size.
Square the screen's height and the screen's width, add the 2 numbers together, then find the square root of the sum, which is the diagonal measurement.
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.)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With