From c7b88dc17f01aa57333d0c6507b7e66e85715235 Mon Sep 17 00:00:00 2001 From: Alcibiades Athens Date: Sat, 28 Dec 2024 11:31:28 -0500 Subject: [PATCH] fix: remain focused on canvas on load --- web/index.html | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/web/index.html b/web/index.html index 252eddf..b4163f6 100644 --- a/web/index.html +++ b/web/index.html @@ -282,12 +282,30 @@

RUSTY PONG

playButton.textContent = "Click to Load Game"; gameContainer.appendChild(playButton); - // Helper function to move canvas into container + // Helper function to move canvas into container and focus it const moveCanvas = () => { const canvases = document.getElementsByTagName("canvas"); for (let canvas of canvases) { if (canvas.parentElement !== gameContainer) { gameContainer.appendChild(canvas); + // Add tabindex to make canvas focusable + canvas.setAttribute("tabindex", "0"); + // Focus the canvas + canvas.focus(); + // Maintain focus when clicking on canvas + canvas.addEventListener("mousedown", (e) => { + e.preventDefault(); + canvas.focus(); + }); + // Prevent focus loss on mobile touch + canvas.addEventListener( + "touchstart", + (e) => { + e.preventDefault(); + canvas.focus(); + }, + { passive: false }, + ); } } }; @@ -304,13 +322,15 @@

RUSTY PONG

window.webkitAudioContext)(); await audioContext.resume(); - // Watch for canvas creation and move it to container + // Enhanced observer to handle canvas creation and focus observer = new MutationObserver((mutations) => { for (let mutation of mutations) { if (mutation.addedNodes) { mutation.addedNodes.forEach((node) => { if (node.nodeName === "CANVAS") { gameContainer.appendChild(node); + node.setAttribute("tabindex", "0"); + node.focus(); } }); } @@ -324,21 +344,18 @@

RUSTY PONG

const { default: init } = await import("./rusty_pong.js"); await init(); - // Ensure canvas is in the right place + // Ensure canvas is in the right place and focused moveCanvas(); } catch (error) { - // Handle Bevy's control flow exception (not a real error) if (error.message?.includes("Using exceptions for control flow")) { loading.style.display = "none"; } else { - // Handle actual errors console.error("Failed to load WASM:", error); loading.textContent = "Failed to load game. Please refresh and try again."; loading.style.color = "#e43d1a"; } } finally { - // Clean up observer if it was created if (observer) { observer.disconnect(); }