Skip to content

Commit

Permalink
fix: devicePixelContentBoxSize not available on Safari
Browse files Browse the repository at this point in the history
  • Loading branch information
jsulpis committed Oct 28, 2024
1 parent 32aaf60 commit 281bdfd
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/src/helpers/resize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ export function onCanvasResize(
let size: ResizeObserverSize;
let devicePixelSize: ResizeObserverSize;

new ResizeObserver((entries) => {
devicePixelSize = entries[0].devicePixelContentBoxSize[0];
size = entries[0].contentBoxSize[0];
const observer = new ResizeObserver((entries) => {
const entry = entries.find((entry) => entry.target === canvas)!;

size = entry.contentBoxSize[0];
devicePixelSize = entry.devicePixelContentBoxSize?.[0] || {
blockSize: Math.round(size.blockSize * window.devicePixelRatio),
inlineSize: Math.round(size.inlineSize * window.devicePixelRatio),
};

// resize after next paint, otherwise there are glitches if a render loop is active
setTimeout(() => {
Expand All @@ -28,5 +33,7 @@ export function onCanvasResize(
devicePixelSize: { width: devicePixelSize.inlineSize, height: devicePixelSize.blockSize },
});
}, 0);
}).observe(canvas);
});

observer.observe(canvas);
}

0 comments on commit 281bdfd

Please sign in to comment.