简体   繁体   English

为什么我的显示分辨率与 screen.width * devicePixelRatio 不同?

[英]Why is my display resolution different from screen.width * devicePixelRatio?

On the MDN documentation on devicePixelRatio it says:devicePixelRatio上的MDN 文档中,它说:

(devicePixelRatio) tells the browser how many of the screen's actual pixels should be used to draw a single CSS pixel. (devicePixelRatio) 告诉浏览器应该使用多少屏幕的实际像素来绘制单个 CSS 像素。

... so I would expect that my display horizontal resolution would be: ...所以我希望我的显示水平分辨率为:

window.devicePixelRatio * window.screen.width = 2 * 1792 = 3584 window.devicePixelRatio * window.screen.width = 2 * 1792 = 3584

... but no. ... 但不是。 My display horizontal resolution is 3072.我的显示器水平分辨率是 3072。

Why is that?这是为什么?

It seems to me that the specification is not accurate enough here.在我看来,规范在这里不够准确。

(devicePixelRatio) tells the browser how many of the screen's actual pixels should be used to draw a single CSS pixel. (devicePixelRatio) 告诉浏览器应该使用多少屏幕的实际像素来绘制单个 CSS 像素。

It's should be something like:它应该是这样的:

(devicePixelRatio) tells the browser how many of the screen's actual CSS pixels should be used to draw a single CSS pixel. (devicePixelRatio) 告诉浏览器应该使用多少屏幕的实际 CSS 像素来绘制单个 CSS 像素。

Because those "actual pixels" are not actually actual.因为那些“实际像素”实际上并不是真实的。 No pun intended.没有双关语的意思。

I found this excellent answer to a similar question, but I don't like the “you don't need it” rhetoric.我找到了一个类似问题的绝佳答案,但我不喜欢“你不需要它”的言论。 TL;DR - The browser does this mainly for legacy reasons. TL;DR - 浏览器这样做主要是出于遗留原因。

Also, I found a huge topic on w3c github about "Ability to address actual physical size", in CSS, but anyway.此外,我在w3c github上发现了一个关于“解决实际物理尺寸的能力”的巨大话题,在 CSS 中,但无论如何。 And there are so many "meh, it's not that simple... you know..." messages.而且有这么多“咩,没那么简单……你知道……”的消息。 I like this one: "there is no clear concept of what a px is".我喜欢这个:“对于什么是 px 没有明确的概念”。

Turns out that actually you can get the real aspect ratio.事实证明,实际上你可以获得真正的纵横比。 At least it works for me.至少它对我有用。 I have a MacBook with a 15-inch display.我有一台 15 英寸显示屏的 MacBook。 The real resolution is 2880x1800 .实际分辨率为2880x1800 In the settings, I have a resolution 1680x1050 (on which window.screen is relying).在设置中,我的分辨率为1680x1050window.screen所依赖的分辨率)。 And a real aspect ratio is 1.6 , not 2 (hello window.devicePixelRatio) .真正的纵横比是1.6 ,而不是2 (hello window.devicePixelRatio)

The aspectRatio property from MediaTrackConstraints returns the real aspect ratio: 1.6. MediaTrackConstraints 的 aspectRatio 属性返回实际的纵横比:1.6。

const [track] = (await navigator.mediaDevices.getDisplayMedia()).getVideoTracks();
console.log(track.getSettings().aspectRatio);

But there is one big caveat, this whole "Media Stream API" is about capturing video from the screen, that's why you have to get permissions for it to work.但是有一个很大的警告,整个“Media Stream API”是关于从屏幕上捕获视频的,这就是为什么你必须获得权限才能工作。 I couldn't find anything else that returns the real aspect ratio but this.除了这个,我找不到任何能返回真实纵横比的东西。

That's all I found in an hour or so, I hope there is enough information to make some conclusions.这就是我在一个小时左右找到的所有内容,我希望有足够的信息来得出一些结论。

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

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