Programmatically determine DPI via the browser?

后端 未结 4 778
情话喂你
情话喂你 2021-02-12 16:24

I would like to programmaticaly determine the DPI of a user\'s display in order to show a web page at a precise number of units (centimeters/inches). I know it a weird request:

相关标签:
4条回答
  • 2021-02-12 16:32

    I think that you won't get precise results - for example you can resize the picture using the monitor. I'd rather stick with the user-driven method, although you can f.e. detect screen resolution for a first estimate.

    0 讨论(0)
  • 2021-02-12 16:38

    You can't. DPI is a function of the pixel resolution of the screen and the physical dimensions of the display. The latter is not available in any browser interface I am aware of.

    0 讨论(0)
  • 2021-02-12 16:45

    It's not possible from a browser without code running on the target computer. If you could install something on the target computer you might be able to determine enough about the monitor to calculate this.

    0 讨论(0)
  • 2021-02-12 16:46

    If you're doing this in javascript/mootools, CSS units are your friend. You can specify sizes in inches or centimeters, or even points (1/72 of an inch). If you absolutely need DPI for whatever reason even though you can specify sizes in those units, simply size the "resizable" div you are using to a known size and calculate from the translated pixel size.

    Edit:

    Unfortunately, the CSS units of points, cm, and in are not physically correct. They are only relatively correct. This was the first thing I tried until I realized it wasn't working and checked the CSS spec.. to my dismay.Brandon Pelfrey

    That's a good point; browsers tend to fake it by assuming a default DPI (I think 72 or 96) and going with that.

    Well, if you need precision sizing like you're asking for, you're out of luck. You'll never be able to get it without being able to read both the current resolution of the monitor the browser is on and the "viewable screen area" of that monitor. Ain't no way you're gonna get that via Javascript.

    I suggest that you make the default assumption that browsers do and size according to the CSS units. You can then allow your users to "adjust the sizing" using the method you mentioned, but only if it's necessary. Do this with on a separate page with the DPI calculation stored as part of the users session or as a cookie.

    Alternatively, once Microsoft fixes this bug, you could use Silverlight's "device independent units" for accurate scaling.

    0 讨论(0)
提交回复
热议问题