开发者

How can I set the size of an object in cm, using JavaScript

I need to set the size 开发者_开发百科of an object in cm/inches (or another value that is always the same on any monitor). I know the code to set the size in px, but not in a value that is always the same.


In theory, you just use the cm unit:

{
    width: 15cm;
    height: 10cm;
}

In practice, you can't do this automatically since this depends on:

  • The computer being correctly calibrated for the monitor so it knows what the DPI of the display is for the resolution it is outputting
  • The browser to calculate pixel sizes based on the system DPI and not its own assumptions

It is very rare for both of these to be true.


A workaround would be to display an image with a known pixel size and ask the user to measure it on their display. You could then calculate all your lengths based on that radio and set all widths, heights, etc with JavaScript. This would be more effort then most users would be willing to go to for most projects.


Same answer as David Dorward.

Another solution is to generate an image of what you want to display, using AJAX if you want, then display it. But again, if users are zoom sets on 120% (like my grand parents), your image will be stretched.

Why do you want to specify a size using centimeters/inches ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜