From 62e0f6b3037434d3a26772ca1c3233a1fa9df382 Mon Sep 17 00:00:00 2001 From: thePR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> Date: Mon, 27 Oct 2025 09:25:12 -0400 Subject: [PATCH] Handle missing canvas context in ambient background --- js/ui/ambientBackground.js | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/js/ui/ambientBackground.js b/js/ui/ambientBackground.js index 305952b3..e51c42ac 100644 --- a/js/ui/ambientBackground.js +++ b/js/ui/ambientBackground.js @@ -1,5 +1,7 @@ // js/ui/ambientBackground.js +import { devLogger as defaultLogger } from "../utils/logger.js"; + const DEFAULT_THROTTLE_MS = 66; function getGlobalFromCanvas(canvas) { @@ -100,14 +102,29 @@ export function attachAmbientBackground(videoElement, canvasElement, options = { return () => {}; } - const ctx = canvasElement.getContext("2d", { alpha: false }); + const normalizedOptions = + options && typeof options === "object" ? options : {}; + const logger = normalizedOptions.logger || defaultLogger; + + let ctx = null; + try { + ctx = canvasElement.getContext("2d", { alpha: false }); + } catch (error) { + if (logger && typeof logger.warn === "function") { + logger.warn("[ambientBackground] Canvas context unavailable:", error); + } + return () => {}; + } if (!ctx) { + if (logger && typeof logger.warn === "function") { + logger.warn("[ambientBackground] Canvas context unavailable."); + } return () => {}; } const { doc, win } = getGlobalFromCanvas(canvasElement); - const throttleMs = Number.isFinite(options.throttleMs) - ? Math.max(0, options.throttleMs) + const throttleMs = Number.isFinite(normalizedOptions.throttleMs) + ? Math.max(0, normalizedOptions.throttleMs) : DEFAULT_THROTTLE_MS; const requestFrame = win?.requestAnimationFrame?.bind(win) || ((cb) => setTimeout(() => cb(Date.now()), throttleMs));