Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Engine VisibleEvent is not firing on visibility change #2409

Closed
mattjennings opened this issue Jul 12, 2022 · 1 comment · Fixed by #2412
Closed

Engine VisibleEvent is not firing on visibility change #2409

mattjennings opened this issue Jul 12, 2022 · 1 comment · Fixed by #2412
Labels
bug This issue describes undesirable, incorrect, or unexpected behavior

Comments

@mattjennings
Copy link
Contributor

Steps to Reproduce

This is reproducible with the sample platformer

  • Start game
  • Switch tabs or unfocus window
  • Go back to game and it remains paused
  • console log only shows "pause", no "start", which means the visible event never fired

Expected Result

Engine should emit the "visible" event when window is focused

Actual Result

It does not emit the "visible" event

Environment

  • browsers and versions: Chrome 103.0.5060.114 , FireFox 102.0.1, Safari 15.5
  • operating system: macOS 12.4
  • Excalibur versions: 0.27

Current Workaround

I see that we have some browser-specific behaviour here

Excalibur/src/engine/Engine.ts

Lines 1112 to 1133 in 88a30ce

let hidden: keyof HTMLDocument, visibilityChange: string;
if (typeof document.hidden !== 'undefined') {
// Opera 12.10 and Firefox 18 and later support
hidden = 'hidden';
visibilityChange = 'visibilitychange';
} else if ('msHidden' in document) {
hidden = <keyof HTMLDocument>'msHidden';
visibilityChange = 'msvisibilitychange';
} else if ('webkitHidden' in document) {
hidden = <keyof HTMLDocument>'webkitHidden';
visibilityChange = 'webkitvisibilitychange';
}
this.browser.document.on(visibilityChange, () => {
if (document[hidden]) {
this.eventDispatcher.emit('hidden', new HiddenEvent(this));
this._logger.debug('Window hidden');
} else {
this.eventDispatcher.emit('visible', new VisibleEvent(this));
this._logger.debug('Window visible');
}
});
but I am currently doing this instead:

document.addEventListener('visibilitychange', () => {
  if (document.visibilityState === 'hidden') {
    engine.stop()
  } else if (document.visibilityState === 'visible') {
    engine.start()
  }
})

which seems to have good browser support

@eonarheim eonarheim added bug This issue describes undesirable, incorrect, or unexpected behavior labels Jul 12, 2022
@eonarheim
Copy link
Member

@mattjennings Thanks for the bug! Good find!

It definitely seems the browsers have improved their support for visibility. I would also support simplifying the code to remove the old prefixed polyfills.

mattjennings added a commit to mattjennings/Excalibur that referenced this issue Jul 13, 2022
eonarheim pushed a commit that referenced this issue Jul 15, 2022
Closes #2409

## Changes:

- Fixed `engine.on('visible')` event not firing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue describes undesirable, incorrect, or unexpected behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants