Skip to content

Commit

Permalink
fix wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-tennert committed Oct 17, 2023
1 parent 42f3596 commit 2dd7340
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fn main() {
..default()
})
)
.add_plugins(WorldInspectorPlugin::new())
// .add_plugins(WorldInspectorPlugin::new())
// .add_plugins(DefaultPickingPlugins)
.add_plugins(LockOnPlugin)
.add_plugins(SerializationPlugin)
Expand Down
2 changes: 1 addition & 1 deletion src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct UIPlugin;
impl Plugin for UIPlugin {
fn build(&self, app: &mut App) {
app
// .add_plugins(EguiPlugin)
.add_plugins(EguiPlugin)
.register_type::<SimTime>()
.init_resource::<SimTime>()
.add_plugins(BlockInputPlugin)
Expand Down
17 changes: 17 additions & 0 deletions wasm/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!doctype html>
<html lang="en">

<body style="margin: 0px;">
<script type="module">
import './restart-audio-context.js'
import init from './bevy_game.js'

init().catch((error) => {
if (!error.message.startsWith("Using exceptions for control flow, don't mind me. This isn't actually an error!")) {
throw error;
}
});
</script>
</body>

</html>
57 changes: 57 additions & 0 deletions wasm/restart-audio-context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// taken from https://developer.chrome.com/blog/web-audio-autoplay/#moving-forward
(function () {
// An array of all contexts to resume on the page
const audioContextList = [];

// An array of various user interaction events we should listen for
const userInputEventNames = [
'click',
'contextmenu',
'auxclick',
'dblclick',
'mousedown',
'mouseup',
'pointerup',
'touchend',
'keydown',
'keyup',
];

// A proxy object to intercept AudioContexts and
// add them to the array for tracking and resuming later
self.AudioContext = new Proxy(self.AudioContext, {
construct(target, args) {
const result = new target(...args);
audioContextList.push(result);
return result;
},
});

// To resume all AudioContexts being tracked
function resumeAllContexts(event) {
let count = 0;

audioContextList.forEach(context => {
if (context.state !== 'running') {
context.resume();
} else {
count++;
}
});

// If all the AudioContexts have now resumed then we
// unbind all the event listeners from the page to prevent
// unnecessary resume attempts
if (count == audioContextList.length) {
userInputEventNames.forEach(eventName => {
document.removeEventListener(eventName, resumeAllContexts);
});
}
}

// We bind the resume function for each user interaction
// event on the page
userInputEventNames.forEach(eventName => {
document.addEventListener(eventName, resumeAllContexts);
});
})();

0 comments on commit 2dd7340

Please sign in to comment.