Skip to content

Commit

Permalink
fix: Use setInterval() instead of requestAnimationFrame()
Browse files Browse the repository at this point in the history
- This makes animations continue to work while in XR/VR/AR because requestAnimationFrame() was attached to the main window.
- See immersive-web/webxr#225
  • Loading branch information
KallynGowdy committed Jan 21, 2021
1 parent 6a9af17 commit 0057f7a
Showing 1 changed file with 6 additions and 25 deletions.
31 changes: 6 additions & 25 deletions src/aux-common/runtime/AuxGlobalContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -740,31 +740,12 @@ export class MemoryGlobalContext implements AuxGlobalContext {

function animationLoop(): Observable<void> {
return new Observable<void>((observer) => {
if (globalThis.requestAnimationFrame) {
let running = true;
let handlerId: number;
let interval = setInterval(() => {
observer.next();
}, SET_INTERVAL_ANIMATION_FRAME_TIME);

const handler = () => {
if (!running) {
return;
}
observer.next();
handlerId = globalThis.requestAnimationFrame(handler);
};
handlerId = globalThis.requestAnimationFrame(handler);

return () => {
running = false;
globalThis.cancelAnimationFrame(handlerId);
};
} else {
let interval = setInterval(() => {
observer.next();
}, SET_INTERVAL_ANIMATION_FRAME_TIME);

return () => {
clearInterval(interval);
};
}
return () => {
clearInterval(interval);
};
});
}

0 comments on commit 0057f7a

Please sign in to comment.