Skip to content

Commit 8c14e6b

Browse files
committed
fix(fov): guard canvas render against 0x0 dimensions
A ResizeObserver firing on a collapsed parent (mobile/desktop display:none swap or a pre-layout pass) sized the canvas to 0x0, and copying a 0-dimension source canvas into cleanCanvas threw InvalidStateError. Bail out of both draw() and the resize handler when width/height is 0, which also preserves the last good render while the panel is hidden. Fixes Sentry issue 7547781808.
1 parent 9a5b6bd commit 8c14e6b

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

  • src/app/[locale]/fov-simulator/_components

src/app/[locale]/fov-simulator/_components/Canvas.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ export function Canvas({ lenses, imageIndex, orientation, canvasRef, cleanCanvas
4545
const ctx = canvas.getContext('2d')
4646
if (!ctx) return
4747
const w = canvas.width; const h = canvas.height; const dpr = window.devicePixelRatio || 1
48+
// A collapsed parent (hidden tab, pre-layout pass, or a 0-width flex frame)
49+
// can size the canvas to 0×0. Copying a 0-dimension canvas into cleanCanvas
50+
// throws InvalidStateError, so bail before any drawImage runs.
51+
if (w === 0 || h === 0) return
4852

4953
drawImageCover(ctx, img, 0, 0, w, h)
5054
const cleanCanvas = cleanCanvasRef.current
@@ -92,6 +96,9 @@ export function Canvas({ lenses, imageIndex, orientation, canvasRef, cleanCanvas
9296
const isMobile = window.innerWidth < 1024
9397
let w = rect.width; let h = w / aspect
9498
if (!isMobile && h > rect.height) { h = rect.height; w = h * aspect }
99+
// Skip while the parent is collapsed (e.g. behind a mobile/desktop
100+
// display:none switch); sizing to 0 would clear the canvas and crash draw().
101+
if (w <= 0 || h <= 0) return
95102
canvas.style.width = `${w}px`; canvas.style.height = `${h}px`
96103
canvas.width = w * dpr; canvas.height = h * dpr; draw()
97104
})

0 commit comments

Comments
 (0)