Skip to content

Commit 8567bb4

Browse files
authored
fix resizable canvas on safari (#2003)
1 parent 34bd217 commit 8567bb4

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

apps/typegpu-docs/src/components/ExampleView.tsx

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,28 @@ function useResizableCanvas(exampleHtmlRef: RefObject<HTMLDivElement | null>) {
254254

255255
canvas.parentElement?.replaceChild(container, canvas);
256256

257-
const onResize: ResizeObserverCallback = (entries) => {
258-
const size = entries[0]?.devicePixelContentBoxSize[0];
259-
if (size) {
260-
newCanvas.width = size.inlineSize;
261-
newCanvas.height = size.blockSize;
257+
const onResize: ResizeObserverCallback = ([entry]) => {
258+
if (!entry) {
259+
return;
262260
}
261+
262+
// Despite what the types say this property does not exist in Safari (hence the optional chaining).
263+
const dpcb = entry.devicePixelContentBoxSize?.[0] as
264+
| ResizeObserverSize
265+
| undefined;
266+
267+
const dpr = dpcb ? 1 : window.devicePixelRatio || 1;
268+
const box = dpcb ??
269+
(Array.isArray(entry.contentBoxSize)
270+
? entry.contentBoxSize[0]
271+
: entry.contentBoxSize);
272+
273+
if (!box) {
274+
return;
275+
}
276+
277+
newCanvas.width = Math.round(box.inlineSize * dpr);
278+
newCanvas.height = Math.round(box.blockSize * dpr);
263279
};
264280

265281
const observer = new ResizeObserver(onResize);

0 commit comments

Comments
 (0)