Skip to content

Commit ae27ebe

Browse files
committed
resize example tweaks
1 parent db5d389 commit ae27ebe

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

webgl/webgl-resize-the-canvas-comparison-fullwindow.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,15 @@
148148
let width;
149149
let height;
150150
let dpr = window.devicePixelRatio;
151+
let dprSupport = false;
151152
if (entry.devicePixelContentBoxSize) {
152153
// NOTE: Only this path gives the correct answer
153154
// The other paths are an imperfect fallback
154155
// for browsers that don't provide anyway to do this
155156
width = entry.devicePixelContentBoxSize[0].inlineSize;
156157
height = entry.devicePixelContentBoxSize[0].blockSize;
157158
dpr = 1; // it's already in width and height
159+
dprSupport = true;
158160
} else if (entry.contentBoxSize) {
159161
if (entry.contentBoxSize[0]) {
160162
width = entry.contentBoxSize[0].inlineSize;
@@ -173,12 +175,14 @@
173175
const displayHeight = Math.round(height * dpr);
174176
canvasToDisplaySizeMap.set(entry.target, [displayWidth, displayHeight]);
175177
resizeAndDraw();
178+
if (!dprSupport) {
179+
document.querySelector('#warning').style.display = '';
180+
}
176181
}
177182
}
178183

179184
const resizeObserver = new ResizeObserver(onResize);
180185
resizeObserver.observe(canvas, {box: 'content-box'});
181-
document.querySelector('#warning').style.display = '';
182186
</script>
183187
</html>
184188

webgl/webgl-resize-the-canvas-comparison.html

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
height: 100px;
2020
display: block;
2121
}
22+
.bad {
23+
color: red;
24+
font-weight: bold;
25+
}
2226
</style>
2327
</head>
2428
<body>
@@ -27,7 +31,7 @@
2731
<canvas id="use-clientWidth"></canvas>
2832
<div>using round(getBoundingClientRect().width * devicePixelRatio)</div>
2933
<canvas id="use-getBoundingClientRect"></canvas>
30-
<div>using ResizeObserver</div>
34+
<div>using ResizeObserver (<span id="dpr-support"></span>)</div>
3135
<canvas id="use-ResizeObserver"></canvas>
3236
<div id="warning" style="color: red; display: none">(warning: no support for device-pixel-content-box)</div>
3337
</div>
@@ -89,6 +93,7 @@
8993

9094
setupCanvas(document.querySelector('#use-ResizeObserver'), (canvas, drawFn) => {
9195
const canvasToDisplaySizeMap = new Map([[canvas, [300, 150]]]);
96+
const dprSupportElem = document.querySelector('#dpr-support');
9297

9398
const resizeAndDraw = () => {
9499
const [displayWidth, displayHeight] = canvasToDisplaySizeMap.get(canvas);
@@ -100,13 +105,15 @@
100105
let width;
101106
let height;
102107
let dpr = window.devicePixelRatio;
108+
let dprSupport = false;
103109
if (entry.devicePixelContentBoxSize) {
104110
// NOTE: Only this path gives the correct answer
105111
// The other paths are an imperfect fallback
106112
// for browsers that don't provide anyway to do this
107113
width = entry.devicePixelContentBoxSize[0].inlineSize;
108114
height = entry.devicePixelContentBoxSize[0].blockSize;
109115
dpr = 1; // it's already in width and height
116+
dprSupport = true;
110117
} else if (entry.contentBoxSize) {
111118
if (entry.contentBoxSize[0]) {
112119
width = entry.contentBoxSize[0].inlineSize;
@@ -125,6 +132,8 @@
125132
const displayHeight = Math.round(height * dpr);
126133
canvasToDisplaySizeMap.set(entry.target, [displayWidth, displayHeight]);
127134
resizeAndDraw();
135+
dprSupportElem.textContent = dprSupport ? "dpr supported 😀" : "dpr not supported 😢";
136+
dprSupportElem.classList.toggle('bad', !dprSupport);
128137
}
129138
}
130139

0 commit comments

Comments
 (0)