Pixel to MM equation?

北慕城南 提交于 2019-11-27 14:45:55

What i did :

 <div id="my_mm" style="height:100mm;display:none"></div>

Then:

var pxTomm = function(px){   
   return Math.floor(px/($('#my_mm').height()/100)); //JQuery returns sizes in PX
 };

tl;dr: If you don't know the DPI of the device, you won't be able to deduce how big the pixel is in the real-world.


Pixels on their own are not real-world units of measurement.

They can become a real-world measurement if you take into account the DPI value of the device that displays them.

The formula is:

  • mm = ( pixels * 25.4 ) / DPI

So 8 pixels viewed on a 96-DPI screen setting:

  • ( 8 * 25.4 ) / 96 = 2.116mm

All this assuming the device is not scaled/zoomed.

You would need to know the DPI of the device and if the display is scaled or not. That would mean that you would have to have a database of the physical screen dimensions and screen resolutions of each device.

old post but I stumbled upon this today and had to make it work.

the trick is to create an element with dimensions styled in inches and request its width, this will give you the px per inch.

 function inch2px(inches) {
                $("body").append("<div id='inch2px' style='width:1in;height:1in;display:hidden;'></div>");
                var pixels = $("#inch2px").width();
                $("#inch2px").remove();

                return inches * pixels;
            }

            function px2inch(px) {
                $("body").append("<div id='inch2px' style='width:1in;height:1in;display:hidden;'></div>");
                var pixels = $("#inch2px").width();
                $("#inch2px").remove();

                return px / pixels;
            }

now if you need millimetres, just do px2inch(10)*25.4.

You cannot reliably calculate this since there is no way to detect physical screen size.

No need for jQuery.

function xToPx(x) {
    var div = document.createElement('div');
    div.style.display = 'block';
    div.style.height = x;
    document.body.appendChild(div);
    var px = parseFloat(window.getComputedStyle(div, null).height);
    div.parentNode.removeChild(div);
    return px;
}


pixels = xToPx('10cm'); // convert 10cm to pixels
pixels = xToPx('10mm'); // convert 10mm to pixels

Please notice that values in pixels are depending on what resolution the browser of the device tells you. Some browsers (on some phones) lie about this and tell you something different than the actual screen resolution in an attempt to be compatible with older sites. Main important thing is to never port conversion values from one device to another but always use real-time calculations. Even on a desktop the user can change the screen resolution.

To learn more about the units, check out this (short) article on W3:

https://www.w3.org/Style/Examples/007/units.en.html

Based on the @Dawa answer above, this is my solution:

var mmToPx = function(mm) {
    var div = document.createElement('div');
    div.style.display = 'block';
    div.style.height = '100mm';
    document.body.appendChild(div);
    var convert = div.offsetHeight * mm / 100;
    div.parentNode.removeChild(div);
    return convert;
};

Just note that the 100mm height, will give you a better precision factor. The calculation will be instant and the div will not be visible. No need for jQuery

i use this simple function

function pix2mm(val,dpi){
    return (val/0.0393701)/dpi;
}

test outputs it 300,600,900 DPI

var r = pix2mm(100,300);  // converting 100 pixels it 300 DPI 
console.log(r);  // output : 8.4 mm
var r1 = pix2mm(100,600);  // converting 100 pixels it 600 DPI
console.log(r1);  // output : 4.2 mm
var r2 = pix2mm(100,900);  // converting 100 pixels it 900 DPI
console.log(r2);  // output : 2.8 mm

DEMO : https://jsfiddle.net/xpvt214o/29044/

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!