diff --git a/Cargo.lock b/Cargo.lock index e17b649..977acc5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -499,7 +499,7 @@ checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35" [[package]] name = "curlywas" version = "0.1.0" -source = "git+https://github.com/exoticorn/curlywas.git?rev=557c3a84#557c3a842675a1ebd77e84021f9f5236d4fa5648" +source = "git+https://github.com/exoticorn/curlywas.git?rev=ebc701e#ebc701e2f2a5849a11ac94950b56803d70f4341d" dependencies = [ "anyhow", "ariadne", diff --git a/Cargo.toml b/Cargo.toml index 4ea7539..0248734 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ anyhow = "1" minifb = { version = "0.20", default-features = false, features = ["x11"] } notify = "4" pico-args = "0.4" -curlywas = { git = "https://github.com/exoticorn/curlywas.git", rev = "557c3a84" } +curlywas = { git = "https://github.com/exoticorn/curlywas.git", rev = "ebc701e" } wat = "1" uw8-tool = { path = "uw8-tool" } same-file = "1" diff --git a/src/run-web.html b/src/run-web.html index 60bb0a5..cfc1614 100644 --- a/src/run-web.html +++ b/src/run-web.html @@ -1 +1 @@ -uw8-run
\ No newline at end of file +uw8-run
\ No newline at end of file diff --git a/web/src/microw8.js b/web/src/microw8.js index 6070a6d..56c713c 100644 --- a/web/src/microw8.js +++ b/web/src/microw8.js @@ -99,9 +99,11 @@ export default function MicroW8(screen, config = {}) { let audioContext = new AudioContext({sampleRate: 44100}); let keepRunning = true; + let abortController = new AbortController(); cancelFunction = () => { audioContext.close(); keepRunning = false; + abortController.abort(); } let cartridgeSize = data.byteLength; @@ -229,6 +231,24 @@ export default function MicroW8(screen, config = {}) { let nextFrame = startTime; audioNode.connect(audioContext.destination); + + let isPaused = false; + let pauseTime = startTime; + let updateVisibility = isVisible => { + let now = Date.now(); + if(isVisible) { + isPaused = false; + audioContext.resume(); + startTime += now - pauseTime; + } else { + isPaused = true; + audioContext.suspend(); + pauseTime = now; + } + }; + window.addEventListener('focus', () => updateVisibility(true), { signal: abortController.signal }); + window.addEventListener('blur', () => updateVisibility(false), { signal: abortController.signal }); + updateVisibility(document.hasFocus()); function mainloop() { if (!keepRunning) { @@ -238,7 +258,7 @@ export default function MicroW8(screen, config = {}) { try { let now = Date.now(); let restart = false; - if (now >= nextFrame) { + if (now >= nextFrame && !isPaused) { let gamepads = navigator.getGamepads(); let gamepad = 0; for (let i = 0; i < 4; ++i) {