diff --git a/index.html b/index.html index a65caf2ee8..3c709bb6c2 100644 --- a/index.html +++ b/index.html @@ -568,7 +568,6 @@ canvas = document.getElementById("canvas"); stage = new createjs.Stage(canvas); - createjs.Ticker.framerate = 60; // createjs.Ticker.addEventListener("tick", stage); // Managed by Activity class } document.addEventListener("DOMContentLoaded", init); diff --git a/js/activity.js b/js/activity.js index 36ec729e76..4e9fec78d0 100644 --- a/js/activity.js +++ b/js/activity.js @@ -2931,6 +2931,15 @@ class Activity { let lastActivity = Date.now(); let isIdle = false; + document.addEventListener("visibilitychange", () => { + if (document.hidden) { + createjs.Ticker.paused = true; + } else { + createjs.Ticker.paused = false; + resetIdleTimer(); + } + }); + // Wake up function - restores full framerate const resetIdleTimer = () => { lastActivity = Date.now(); @@ -2950,8 +2959,7 @@ class Activity { window.addEventListener("wheel", resetIdleTimer); // Periodic check for idle state - setInterval(() => { - // Check if music/code is playing + const idleCheck = () => { const isMusicPlaying = this.logo?._alreadyRunning || false; if (!isMusicPlaying && Date.now() - lastActivity > IDLE_THRESHOLD) { @@ -2961,10 +2969,13 @@ class Activity { console.log("⚡ Idle mode: Throttling to 1 FPS to save battery"); } } else if (isIdle && isMusicPlaying) { - // Music started playing - wake up immediately resetIdleTimer(); } - }, 1000); + + setTimeout(idleCheck, 1000); + }; + + setTimeout(idleCheck, 1000); // Expose activity instance for external checks if (typeof window !== "undefined") { @@ -7322,6 +7333,7 @@ class Activity { // Initialize Ticker with optimal framerate createjs.Ticker.framerate = 60; + createjs.Ticker.timingMode = createjs.Ticker.RAF; // ===== Idle Ticker Optimization ===== // Throttle rendering when user is inactive and no music is playing