Skip to content

Commit 104cb06

Browse files
committed
fix: rAF timeout fallback so Safari captures don't hang in occluded windows
1 parent 50ea4eb commit 104cb06

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

src/exporters/toCanvas.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,13 @@ async function waitForImgPaint(img, verify) {
282282
const probe = document.createElement('canvas')
283283
probe.width = 16
284284
probe.height = 16
285+
// rAF with a timeout fallback: WebKit suspends rAF in occluded windows and
286+
// background tabs, and a capture must not hang there.
287+
const frame = () => new Promise(r => { requestAnimationFrame(r); setTimeout(r, 50) })
285288
const pctx = probe.getContext('2d', { willReadFrequently: true })
286289
if (!pctx) {
287-
await new Promise(r => requestAnimationFrame(() => requestAnimationFrame(r)))
290+
await frame()
291+
await frame()
288292
return
289293
}
290294
const deadline = performance.now() + (verify ? 600 : 150)
@@ -295,7 +299,7 @@ async function waitForImgPaint(img, verify) {
295299
let ink = false
296300
for (let i = 3; i < d.length; i += 4) { if (d[i] > 0) { ink = true; break } }
297301
if (ink || performance.now() > deadline) return
298-
await new Promise(r => requestAnimationFrame(r))
302+
await frame()
299303
}
300304
} finally {
301305
try { img.remove() } catch { /* ok */ }

0 commit comments

Comments
 (0)