How to calculate dpi in javascript?

By following what others do, I’ve made a tool which displays ruler in real size (realruler.com)
It’s basically coded in javascript with w3css and this is my first js project. based on what I could read online, it is impossible to calculate dpi of the screen because the calculation is based on the actual display diagonal.
Now, I am looking for second opinion. Is there any, more elegant or more accurate way to display 1 cm on screen, any other way to calculate that value, or is there any known service which could return diagonal or dpi of a device.
thanks in advance, btw, I would be grateful if you could test my tool and see if there are potential problems I over-sighted.
Thanks a lot

1 Like

Hi alex76 welcome to the forum

Sorry, but I’m confused.

You’re mixing screen terminology with print terminology.

If I understand, you’re trying to impose “hard” dimensions into a “fluid” display?

1 Like

That has the potential to mess things up rather badly when something on the screen gets printed.

Far more useful would be a ruler that shows the actual print size of the elements which is very unlikely to be the same as the display size. That can most easily be worked in the css using pt, in or cm as the qualifiers on the size (although pt on computers assumes 72 to the in where the print industry use 72.1).

Most programs that display a ruler display one that corresponds to the size that will be used when printed and don’t even attempt the impossible task of matching actual screen size.

2 Likes

you’re right I’ve mixed up dpi and ppi. I write dpi, but I actually mean about ppi. sorry about the confusion. I didn’t mean to create printing tool.

1 Like

I suspect this is an impossible task, try changing the screen resolution or using screens with different pixel densities and running some tests on all of the properties available on window/document - I don’t believe enough information about physical size is ever exposed to JavaScript.

2 Likes

I agree . tried everything. there’s seems no way of knowing physical diagonal. Having user enter that value I think I’ve managed to get it work on PC and android, but I don’t have physical iphone to check what’s going on apple devices. thank you for your help.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.