Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ref test for ResizeObserver devicePixelContenBoxSize #36057

Closed

Conversation

greggman
Copy link
Contributor

This test creates centered canvases with percentage sizes and applies a pattern to each one in device pixels

The ref creates divs of the same size and applies the same pattern using CSS

The test and the ref should match.

works in Chrome, fails in Firefox, Safari doesn't support devicePixelContentBoxSize so both pages appear blank

This test creates centered canvases with percentage
sizes and applies a pattern to each one in device pixels

The ref creates divs of the same size and applies the
same pattern using CSS

The test and the ref should match.
Copy link
Contributor

@atotic atotic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever way to test the device-pixel-content-box.

When I ran the tests I noticed something unexpected: on some page sizes, different divs (ex: 7% and 20%) look slightly different (screenshot attached). I believe this is caused by MacOS rendering. I see the same effect on a screenshot, and the div colors change as I scroll.
Chrome, MacOS, 4K external monitor.

Screen Shot 2022-09-24 at 4 36 57 PM

* 1 pixel in the pattern maps to 1 devicePixel.
*/
function setPattern(elem) {
const oneDevicePixel = 1 / devicePixelRatio;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am always cautious about usage of multiplications by window.devicePixelRatio, fractional sizes can lead to surprising rounding errors. I trust that you understand this better than I do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean by "slight different". If you mean the patterns don't align between divs that's expected as the pattern starts at the left most pixel of each div

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is what can happen.

Example: devicePixelRation = 1.66.
You will set background size to a float.
To display a background image, browser will round background size to an integer.
There is no way for you to know if size got rounded up or down.
patternPixel pattern size might end up not being an exact multiple of background size, so you can't guarantee it'll display correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that can happen. The browser computes the element size (and therefore the background size) in CSS pixels. The background size is also specified in CSS pixels. Because they are the same units they'll both be converted to device pixels in the same way. It would be a background pattern bug for the pattern not to match what's specified

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I no longer work on Chrome, so I can't consult the rendering team for the details why rendering fractional pixel widths can create scaling artifacts. If you can find a reviewer that agrees with your interpretation what rendering will look like, I am happy to ok this pull request.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside: Trippy test! It's cool how this shows when MacOS re-computes window-to-device mapping after you stop moving the window.

Is this test testing devicePixelContentBoxSize? If so, do we really need to render pixels to test it, rather than just checking the values in javascript? The javascript approach will be a faster test to run, and won't have a dependency on how css background images paint.

WPT uses a device pixel ratio of 1 (issue) so it is strange that firefox is failing. Is their failure just related to devicePixelContentBoxSize with fractional css width/heights? If so, this would also be cleaner to test using javascript.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, @progers explained that browsers do random things with patterns (or at least Chrome does) and while this test doesn't trigger those random things there's no guarantee that won't change in the future.

So, closing this PR

@greggman greggman closed this Mar 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants