Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert pixels to cm in JavaScript

I'm writing CSS for project and need to convert pixel to centimeter with JavaScript. how do I calculate this conversion?

like image 975
Laurie R. Horton Avatar asked May 23 '26 16:05

Laurie R. Horton


2 Answers

You could use window.devicePixelRatio to check for device caps. It is used in most browsers.

like image 52
Bee Avatar answered May 25 '26 05:05

Bee


There are 2.54 centimeters per inch; if it is sufficient to assume 96 pixels per inch, the formula is rather simple:

centimeters = pixels * 2.54 / 96

There is a way to get the configured pixels per inch of your display for Microsoft Windows called GetDeviceCaps. Microsoft has a guide called "Developing DPI-Aware Applications", look for the section "Creating DPI-Aware Fonts".

sample code

like image 29
Json Avatar answered May 25 '26 05:05

Json



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!