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

Manually compute initial visibilty #276

Merged
merged 3 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/kind-radios-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'livekit-client': patch
---

compute initial visible value for element infos manually
32 changes: 30 additions & 2 deletions src/room/track/RemoteVideoTrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ export default class RemoteVideoTrack extends RemoteTrack {
// the tab comes into focus for the first time.
this.debouncedHandleResize();
this.updateVisibility();
} else {
log.warn('visibility resize observer not triggered');
}
}

Expand Down Expand Up @@ -294,9 +296,9 @@ class HTMLElementInfo implements ElementInfo {

handleVisibilityChanged?: () => void;

constructor(element: HTMLMediaElement, visible: boolean = false) {
constructor(element: HTMLMediaElement, visible?: boolean) {
this.element = element;
this.visible = visible;
this.visible = visible ?? isElementInViewport(element);
this.visibilityChangedAt = 0;
}

Expand Down Expand Up @@ -332,3 +334,29 @@ class HTMLElementInfo implements ElementInfo {
getResizeObserver()?.unobserve(this.element);
}
}

// does not account for occlusion by other elements
function isElementInViewport(el: HTMLElement) {
let top = el.offsetTop;
let left = el.offsetLeft;
const width = el.offsetWidth;
const height = el.offsetHeight;
const { hidden } = el;
const { opacity, display } = getComputedStyle(el);

while (el.offsetParent) {
el = el.offsetParent as HTMLElement;
top += el.offsetTop;
left += el.offsetLeft;
}

return (
top < window.pageYOffset + window.innerHeight &&
left < window.pageXOffset + window.innerWidth &&
top + height > window.pageYOffset &&
left + width > window.pageXOffset &&
!hidden &&
(opacity !== '' ? parseFloat(opacity) > 0 : true) &&
display !== 'none'
);
}