|
19 | 19 | height: 100px;
|
20 | 20 | display: block;
|
21 | 21 | }
|
| 22 | + .bad { |
| 23 | + color: red; |
| 24 | + font-weight: bold; |
| 25 | + } |
22 | 26 | </style>
|
23 | 27 | </head>
|
24 | 28 | <body>
|
|
27 | 31 | <canvas id="use-clientWidth"></canvas>
|
28 | 32 | <div>using round(getBoundingClientRect().width * devicePixelRatio)</div>
|
29 | 33 | <canvas id="use-getBoundingClientRect"></canvas>
|
30 |
| - <div>using ResizeObserver</div> |
| 34 | + <div>using ResizeObserver (<span id="dpr-support"></span>)</div> |
31 | 35 | <canvas id="use-ResizeObserver"></canvas>
|
32 | 36 | <div id="warning" style="color: red; display: none">(warning: no support for device-pixel-content-box)</div>
|
33 | 37 | </div>
|
|
89 | 93 |
|
90 | 94 | setupCanvas(document.querySelector('#use-ResizeObserver'), (canvas, drawFn) => {
|
91 | 95 | const canvasToDisplaySizeMap = new Map([[canvas, [300, 150]]]);
|
| 96 | + const dprSupportElem = document.querySelector('#dpr-support'); |
92 | 97 |
|
93 | 98 | const resizeAndDraw = () => {
|
94 | 99 | const [displayWidth, displayHeight] = canvasToDisplaySizeMap.get(canvas);
|
|
100 | 105 | let width;
|
101 | 106 | let height;
|
102 | 107 | let dpr = window.devicePixelRatio;
|
| 108 | + let dprSupport = false; |
103 | 109 | if (entry.devicePixelContentBoxSize) {
|
104 | 110 | // NOTE: Only this path gives the correct answer
|
105 | 111 | // The other paths are an imperfect fallback
|
106 | 112 | // for browsers that don't provide anyway to do this
|
107 | 113 | width = entry.devicePixelContentBoxSize[0].inlineSize;
|
108 | 114 | height = entry.devicePixelContentBoxSize[0].blockSize;
|
109 | 115 | dpr = 1; // it's already in width and height
|
| 116 | + dprSupport = true; |
110 | 117 | } else if (entry.contentBoxSize) {
|
111 | 118 | if (entry.contentBoxSize[0]) {
|
112 | 119 | width = entry.contentBoxSize[0].inlineSize;
|
|
125 | 132 | const displayHeight = Math.round(height * dpr);
|
126 | 133 | canvasToDisplaySizeMap.set(entry.target, [displayWidth, displayHeight]);
|
127 | 134 | resizeAndDraw();
|
| 135 | + dprSupportElem.textContent = dprSupport ? "dpr supported 😀" : "dpr not supported 😢"; |
| 136 | + dprSupportElem.classList.toggle('bad', !dprSupport); |
128 | 137 | }
|
129 | 138 | }
|
130 | 139 |
|
|
0 commit comments