Skip to content

Commit

Permalink
disconnect observer when remake surface
Browse files Browse the repository at this point in the history
  • Loading branch information
dera- committed Jun 5, 2024
1 parent fbade1e commit 6bc0927
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/ContainerController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export class ContainerController {
surface: CanvasSurface = undefined!;
inputHandlerLayer: InputHandlerLayer = undefined!;
rootView: HTMLElement = undefined!;
// Canvas 要素のサイズ変更に追従するための Observer
observer: MutationObserver = undefined!;

/**
* ゲームコンテンツのCanvas拡大・縮小時に内部のコンテキスト領域のリサイズを行うかどうか。初期値はfalse。
Expand Down Expand Up @@ -124,12 +126,16 @@ export class ContainerController {
// 入力受け付けレイヤー > 描画レイヤー
this.surface = this.resourceFactory.createPrimarySurface(width, height);
const surfaceElement = this.surface.getHTMLElement();
// メモリリーク防止のため、過去の Canvas に対する Observer を削除しておく
if (this.observer) {
this.observer.disconnect();
}
// Canvasの親要素のwidthとheightは範囲外判定で使用するため、Canvasに追従できるようにする必要がある
const observer = new MutationObserver(() => {
this.observer = new MutationObserver(() => {
this.inputHandlerLayer.view.style.width = surfaceElement.offsetWidth + "px";
this.inputHandlerLayer.view.style.height = surfaceElement.offsetHeight + "px";
});
observer.observe(surfaceElement, { attributeFilter: ["width", "height", "style"] });
this.observer.observe(surfaceElement, { attributeFilter: ["width", "height", "style"] });
this.inputHandlerLayer.view.appendChild(surfaceElement);
// containerController -> input -> canvas
this.container.appendChild(this.inputHandlerLayer.view);
Expand Down

0 comments on commit 6bc0927

Please sign in to comment.