如何通过 JavaScript 访问屏幕显示的 DPI 设置?
有没有办法在 Javascript 函数中访问屏幕显示的 DPI 设置?
我试图在页面上放置一个 HTML 面板,当用户的 DPI 设置为大 (120) 时,它会丢失该位置。 我需要知道 DPI 是多少,以便我可以相应地调整位置。
Is there a way to access the screen display's DPI settings in a Javascript function?
I am trying to position a HTML panel on the page and when the user's DPI is set to large (120), it throws the position off. I need to be able to know what the DPI is so I can adjust the position accordingly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看起来你可以在 IE 中使用“屏幕”DOM 对象,它具有 deviceXDPI、deviceYDPI、logicXDPI、logicYDPI 属性。
https://www.w3schools.com/jsref/obj_screen.asp
这是一个解决方案来自 http://www.webdeveloper.com/forum/showthread.php?t =175278
(我还没试过,看起来完全是黑客:)
只需创建 1 英寸宽的东西并以像素为单位进行测量即可!
Looks like you can use the 'screen' DOM object in IE, its got properties for deviceXDPI, deviceYDPI, logicalXDPI, logicalYDPI.
https://www.w3schools.com/jsref/obj_screen.asp
Here's a solution from http://www.webdeveloper.com/forum/showthread.php?t=175278
(i havent tried it, seems like a total hack :)
Just create something 1 inch wide and measure it in pixels!
首先,为了帮助解决与术语 DPI(每英寸点数)可能(且非常常见)的混淆:
DPI 并不完全是“显示设置”的一部分。 它被(错误地)用在两种不同的环境中:
打印图像时,有很多因素会影响图像的最终尺寸纸上图像:
最重要的是,您要打印的图像将有效地重新采样(缩小或放大),以匹配打印过程中使用的最终 DPI。 任何相关方都可能导致此问题(程序、打印驱动程序、打印机)。
现在回到你的问题。 不,您无法确定屏幕的 DPI,因为它不在软件域中。 这是一个硬件领域术语,描述用户可以购买多大的显示器。更新:我最初是在 2009 年根据对当前技术的了解写下这个答案的。 正如 @thure 指出的,您现在(从 2012 年开始?)可以使用 窗口.matchMedia 函数来确定屏幕的DPI。如果您想在打印 HTML 布局时达到精确度,正如其他人所建议的那样,您的 CSS 应该使用 em、pt 或 pc 等打印尺寸,而不是 px。 不过,最终结果可能仍取决于所使用的浏览器。 如果您可以选择将 HTML 转换为 PDF(或从头开始生成 PDF),那么打印 PDF 将为您在屏幕和纸张上提供最真实的所见即所得。
Firstly, to help with the possible (and very common) confusion with the term DPI (dots per inch):
DPI isn't exactly a part of "display settings". It's (mis)used in two different contexts:
When printing an image, there are many things that affect the final dimensions of the image on paper:
The bottom line is, the image that you're printing will effectively get resampled (reduced or enlarged) to match the final DPI that's used in the print process. Any of the parties involed may be causing this (the program, the print driver, the printer).
Now, coming back to your question. No, you can't determine the DPI of the screen, because it's not in software domain. It's a hardware domain term, describing how large a monitor a user could afford to buy.Update: I initially wrote this answer back in 2009, with my understanding of the current technologies. As @thure pointed out, you can now (since 2012?) use the window.matchMedia function to determine the DPI of the screen.If you're trying to achieve precision in printing an HTML layout, as others have suggested, your CSS should use print dimensions like em, pt or pc instead of px. However, the final outcome might still depend on the browser using. If converting your HTML to PDF (or generating PDF from scratch) is an option for you, printing a PDF would give you the truest WYSIWYG both on screen and paper.
您可以使用
window.devicePixelRatio
< /a> 属性获取屏幕/页面的缩放比例。 这在桌面版 (Windows) 上当前的 IE、Edge、Chrome 和 Firefox 中运行良好,但它似乎不是当前的标准。 它在配备传统显示器的台式电脑上返回 1,在具有 200% 缩放比例的 Surface 上返回 2。 如今,值的范围应为 1.0 到 3.0。 我可以使用它来校正动态图像服务大小,以便在高分辨率屏幕上提供更清晰的图像。如果您需要一些逻辑 dpi/ppi,请将该值乘以 96。不过,它不会是实际的物理 ppi,只是操作系统对待它的方式。
You might use the
window.devicePixelRatio
property to get the scaling ratio of the screen/page. This works well in current IE, Edge, Chrome and Firefox on the desktop (Windows), but it doesn't seem to be a current standard. It returns 1 on my desktop PC with a conventional monitor and 2 on the Surface with 200% scaling. Values should range from 1.0 to 3.0 these days. I could use this to correct dynamic image serving size to provide sharper images on high-resolution screens.If you need some logical dpi/ppi, multiply that value with 96. It won't be the actual physical ppi though, just what the OS treats it like.
据我所知,没有一种方法,但是,它们可能是替代解决方案:
在“pt”和“em”中指定您的测量值,它们是屏幕相对指标。
http://www.w3schools.com/cssref/css_units.asp
https://css-tricks.com/the- css 长度/
There isn't a way that I know of, however, they may be an alternative solution:
Specify your measurements in 'pt' and 'em', which are screen relative metrics.
http://www.w3schools.com/cssref/css_units.asp
https://css-tricks.com/the-lengths-of-css/