Source code

Revision control

Copy as Markdown

Other Tools

Test Info:

<!doctype html>
<link rel="match" href="devicepixel2-ref.html">
<meta name="assert" content="Resize Observer's reported device pixel content box size should be consistent with the actual pixel-snapped painted box size">
html, body {
margin: 0;
.line {
width: 100px;
display: flex;
.line>* {
flex: 1;
height: 20px;
<script type="module">
import createPatternDataURL from './create-pattern-data-url.js';
const {patternSize, dataURL} = createPatternDataURL();
* Set the pattern's size on this element so that it draws where
* 1 pixel in the pattern maps to 1 devicePixel and then set
* its position so it's aligned to the pattern of the body element.
function setPattern(elem, x, y) {
const oneDevicePixel = 1 / devicePixelRatio;
const patternPixels = oneDevicePixel * patternSize; = `url("${dataURL}")`; = `${patternPixels}px ${patternPixels}px`; = `${-x * oneDevicePixel}px ${-y * oneDevicePixel}px`;
This test creates elements like this
<div class="line"><div></div><div></div></div>
<div class="line"><div></div><div></div><div></div></div>
<div class="line"><div></div><div></div><div></div><div></div></div>
It has the first `<div>` starting at the top left corner of the page, the
same as the body so they should both have a devicePixel position of 0,0
The devicePixelContentBoxSize of all the elements is queried with
a ResizeObserver.
It then sets a repeating background-image, background-size and
background-position on the all of the innermost children elements so
they have a 4x4 checkerboard image at a size that should draw each pixel
of the 4x4 checkerboard pattern in single devicePixels and so they should
all align with the same pattern applied in a similar manner to the body.
In other words
<div class="line">
The first child will be displayed at left = 0 so its background-position is
set to 0px 0px. The second child should be displayed at
secondChildLeft = entry(firstChild).devicePixelContentBoxSize[0].inlineSize.
The 3rd child should be displayed at
thirdChildLeft = entry(firstChild).devicePixelContentBoxSize[0].inlineSize +
Using the children's device-pixel positions (determined as described above),
we then set each child's background-position to the negated version of the
position that we computed for that child. This effectively makes its pattern's
origin match the document origin.
If what devicePixelContentBox reports is correct the children's patterns
will align with the pattern in the body and the children will be invisible.
If what devicePixelContentBox reports is incorrect the patterns will not
align and it should be clearly visible.
const wrapperElem = document.querySelector('div');
const elemToDevicePixelSize = new Map();
function setPatternsUsingSizeInfo(entries) {
setPattern(document.body, 0, 0);
for (const entry of entries) {
elemToDevicePixelSize.set(, {
inlineSize: entry.devicePixelContentBoxSize[0].inlineSize,
blockSize: entry.devicePixelContentBoxSize[0].blockSize,
let devicePixelY = 0;
for (const lineElem of wrapperElem.children) {
let devicePixelX = 0;
for (const childElem of lineElem.children) {
setPattern(childElem, devicePixelX, devicePixelY);
const devicePixelSize = elemToDevicePixelSize.get(childElem);
devicePixelX += devicePixelSize.inlineSize;
const lineEntry = elemToDevicePixelSize.get(lineElem);
const lineHeight = lineEntry.blockSize;
devicePixelY += lineHeight;
const observer = new ResizeObserver(setPatternsUsingSizeInfo);
for (let numFlexItems = 2; numFlexItems < 15; ++numFlexItems) {
const lineElem = document.createElement('div');
lineElem.className = 'line';
observer.observe(lineElem, {box:"device-pixel-content-box"});
for (let j = 0; j < numFlexItems; ++j) {
const inner = document.createElement('div');
observer.observe(inner, {box:"device-pixel-content-box"});