Skip to content

Commit

Permalink
Restore resize observer since the window resize event does not fire o…
Browse files Browse the repository at this point in the history
…n body resize, but on window resize. It's supported by 93% of the browsers.
  • Loading branch information
davidetan committed Oct 3, 2024
1 parent 8cc5a2b commit bedce14
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions spine-ts/spine-webgl/src/SpineWebComponentWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,7 @@ class SpineWebComponentOverlay extends HTMLElement implements Disposable {
private fpsAppended = false;

private intersectionObserver?: IntersectionObserver;
private resizeObserver?:ResizeObserver;
private input?: Input;

// how many pixels to add to the edges to prevent "edge cuttin" on fast scrolling
Expand Down Expand Up @@ -924,7 +925,6 @@ class SpineWebComponentOverlay extends HTMLElement implements Disposable {
}

connectedCallback (): void {
window.addEventListener("resize", this.resizeCallback);
window.addEventListener("scroll", this.scrollHandler);
window.addEventListener("load", this.onLoadCallback);
if (this.loaded) this.onLoadCallback();
Expand All @@ -946,6 +946,14 @@ class SpineWebComponentOverlay extends HTMLElement implements Disposable {
}
})
}, { rootMargin: "30px 20px 30px 20px" });

// resize observer is supported by all major browsers today chrome started to support it in version 64 (early 2018)
// we cannot use window.resize event since it does not fire when body resizes, but not the window
// Alternatively, we can store the body size, check the current body size in the loop (like the translateCanvas), and
// if they differs call the resizeCallback. I already tested it, and it works. ResizeObserver should be more efficient.
this.resizeObserver = new ResizeObserver(this.resizeCallback);
this.resizeObserver.observe(document.body);

this.skeletonList.forEach((widget) => {
this.intersectionObserver?.observe(widget.getHTMLElementReference());
})
Expand All @@ -955,11 +963,11 @@ class SpineWebComponentOverlay extends HTMLElement implements Disposable {
}

disconnectedCallback (): void {
window.removeEventListener("resize", this.resizeCallback);
window.removeEventListener("scroll", this.scrollHandler);
window.removeEventListener("load", this.onLoadCallback);
window.screen.orientation.removeEventListener('change', this.orientationChangeCallback);
this.intersectionObserver?.disconnect();
this.resizeObserver?.disconnect();
this.input?.dispose();
}

Expand Down

0 comments on commit bedce14

Please sign in to comment.