简体   繁体   English

独立于页面缩放级别和 Windows 缩放设置检测屏幕分辨率

[英]Detect screen resolution independently of page zoom level and Windows scale settings

I need to detect screen resolution.我需要检测屏幕分辨率。 The problem is, if scale setting in Windows 10 is not 100%, it affects screen.width / screen.height values.问题是,如果 Windows 10 中的缩放设置不是 100%,它会影响screen.width / screen.height值。

I could use pixelDeviceRatio for correct them, but here is another problem: if the page is zoomed, it also affects pixelDeviceRatio .我可以使用pixelDeviceRatio来纠正它们,但这是另一个问题:如果页面被缩放,它也会影响pixelDeviceRatio

For example, I have Windows scale setting is 150%, and page zoom is 67%.例如,我的 Windows 缩放设置为 150%,页面缩放为 67%。 My screen resolution is 1920x1080.我的屏幕分辨率是 1920x1080。 In this case, screen.width is 1280, and pixelDeviceRatio is 1.0 (because Windows scale and page zoom level compensate each other).在这种情况下, screen.width为 1280, pixelDeviceRatio为 1.0(因为 Windows 缩放和页面缩放级别相互补偿)。 How can I detect the real screen resolution?如何检测真实的屏幕分辨率?

Upd.: I'm working on HTML5 game.更新:我正在开发 HTML5 游戏。 I need to resize it to actual screen resolution when going to fullscreen mode.进入全屏模式时,我需要将其调整为实际屏幕分辨率。

Upd.更新。 2: I have <canvas> element and call its requestFullscreen method to switch into fullscreen mode. 2:我有<canvas>元素并调用它的requestFullscreen方法切换到全屏模式。 Then I have to set width and height of the canvas.然后我必须设置画布的宽度和高度。 If I set it, for example, less than actual screen resolution (1280 instead of 1920), empty margins around the canvas appears.例如,如果我将它设置为小于实际屏幕分辨率(1280 而不是 1920),画布周围会出现空白边距。

This is an XY problem , OP doesn't want the screen resolution, but the one of the document.这是一个XY 问题,OP 不想要屏幕分辨率,而是文档之一。


Note for future readers.供未来读者注意。
There are chances that if you also are trying to get the actual screen resolution, you are yourself in some kind of an XY problem.如果您还试图获得实际的屏幕分辨率,那么您可能会遇到某种 XY 问题。

If what you want is sharp pixels, getting the actual screen resolution won't help you much.如果您想要的是清晰的像素,那么获得实际的屏幕分辨率对您没有多大帮助。

Taking OP's example of a browser zoom of 75% itself scaled by the OS zoom at 150%, we get for a single white pixel,以 OP 的示例为例,浏览器自身缩放 75%,操作系统缩放 150%,我们得到单个白色像素,

  • 1 white pixel x 0.75 => impossible to render. 1 个白色像素 x 0.75 => 无法渲染。 => anti-aliasing with surrounding pixels. => 与周围像素抗锯齿。 So if we say that surrounding pixels were black, then we've now got a grayish pixel.所以如果我们说周围的像素是黑色的,那么我们现在就有了一个灰色的像素。

And this was only at the browser level... We still have to pass the OS zoom algorithm.而这只是在浏览器级别......我们仍然需要通过操作系统缩放算法。

  • 1 grayish pixel x 1.5 => impossible to render => anti-aliasing with surrounding pixels. 1 个灰色像素 x 1.5 => 无法渲染 => 与周围像素抗锯齿。 And yet a new pixel, even farther from our original white.还有一个新像素,甚至比我们原来的白色更远。

The best we can do here is to set our canvas bitmap's size to the one reported by the browser.我们在这里能做的最好的事情是将画布位图的大小设置为浏览器报告的大小。 This way, only the OS zoom would kick in.这样,只有操作系统缩放才会启动。

Note: You may want to try to detect high density screens aka retina® , which are actually able to render pixels at sub-px level, in this case, you might try using window.devicePixelRatio as a general scaling factor, but remember to round it, since a floating scale would mean more antialiasing (no high density screen will ever use 2.3 pixels to render one px).注意:您可能想尝试检测高密度屏幕 aka Retina® ,它实际上能够在亚 px 级别渲染像素,在这种情况下,您可以尝试使用window.devicePixelRatio作为一般缩放因子,但请记住四舍五入它,因为浮动比例意味着更多的抗锯齿(没有高密度屏幕会使用 2.3 像素来渲染一个像素)。

So to detect this document size, is an easy task, with multiple different ways of doing:因此,检测此文档大小是一项简单的任务,有多种不同的方法:

If you deal with hard-coded sizes in windowed mode, you can simply check for window.innerWidth and window.innerHeight when entering fullScreen:如果在窗口模式下处理硬编码大小,则可以在进入 fullScreen 时简单地检查window.innerWidthwindow.innerHeight

Unfortunately, StackSnippets® iframe do not allow fullscreen mode...不幸的是,StackSnippets® iframe 不允许全屏模式......

// listen for the resize event
window.onresize = function() {
  // if we are in fullscreen mode
  if (document.fullscreenElement === canvas) {
    // simply size our canvas to the reported innerWidth/innerHeight.
    W = canvas.width = window.innerWidth;
    H = canvas.height = window.innerHeight;
  }
  draw();
}
// in the fulscreenchange event
document.onfullscreenchange = function() {
    // if we are exiting the fullscreen mode
    if (!document.fullscreenElement) {
      // set  back to hard-coded values
      W = canvas.width = 500
      H = canvas.height = 200;
    }
    draw();
  }

Fiddle .小提琴

If you want something more dynamic, you could also make use of CSS to let you know the actual size.如果你想要更动态的东西,你也可以使用 CSS 来让你知道实际大小。 Indeed, you can resize your canvas through CSS, as long as you remember to set its bitmap's size to the displayed size.实际上,您可以通过 CSS 调整画布的大小,只要您记住将其位图的大小设置为显示的大小即可。

// listen only for the resize event
window.onresize = function() {
  // set the canvas size to its own rendered size
  W = canvas.width = canvas.offsetWidth;
  H = canvas.height = canvas.offsetHeight;
  draw();
}
/* target only when in fullscreen mode */
canvas::fullscreen {
  width: 100vw;
  height: 100vh;
}
/* windowed mode */
canvas {
  width: 50vw;
  height: 50vh;
}

Fiddle .小提琴

currently there is no javascript api to detect os scale factor.目前没有用于检测操作系统比例因子的 javascript api。 Because i have also looked for it too many time and couldn't find it.因为我也找了太多次都没找到。

You can try this:你可以试试这个:

var width = screen.width
var height = screen.height
width = width*window.devicePixelRatio;
height = height*window.devicePixelRatio;

alert("width : "+width);
alert("height : "+height);

Hope my answer helps!希望我的回答有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM