0

I'm getting cm as an input value and I want to convert the integer to pixel (doesn't need to be absolutely precise). Is there a formula to do this?

2 Answers2

1

Two screens with the same resolution can be of two completely different sizes, it all depends on the pixel density, so you can't convert pixels to centimeters without knowing it.

For example, if you use a projector, the projected image can be two meters wide, but it still has just as many pixels as it has on your 15" laptop screen.

Look here : https://en.wikipedia.org/wiki/Pixel_density

  • I heard about that. Thanks. Just thought maybe there's a way to approximate it. – olaseniorita Apr 02 '19 at 14:43
  • @TheCodingPenguin Could one get the pixel density, say using `window.devicePixelRatio` and calculate an approximate inch -> pixel? – dustytrash Apr 02 '19 at 14:48
  • Someone already wanted to know [here](https://stackoverflow.com/questions/279749/detecting-the-system-dpi-ppi-from-js-css). Basically, you could achieve that with mediaqueries, provided that the browser version is recent enough. `devicePixelRatio` only allows, according to [mozilla](https://developer.mozilla.org/en-US/docs/Web/API/Window/devicePixelRatio), to get "the ratio of the resolution in physical pixels to the resolution in CSS pixels for the current display device". – The Coding Penguin Apr 02 '19 at 14:52
-1

Pixels (px) are relative to the viewing device.

For low-dpi devices, 1px is one device pixel (dot) of the display.
For printers and high resolution screens 1px implies multiple device pixels.

See: https://www.w3schools.com/css/css_units.asp

Usagi Miyamoto
  • 6,196
  • 1
  • 19
  • 33