Skip to content

Commit

Permalink
perf(std): add cache for gfx viewport (#9003)
Browse files Browse the repository at this point in the history
  • Loading branch information
doodlewind authored Dec 18, 2024
1 parent 91118b8 commit 0f25a1e
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions packages/framework/block-std/src/gfx/viewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ export const ZOOM_MAX = 6.0;
export const ZOOM_MIN = 0.1;

export class Viewport {
private _cachedHeight: number = 0;

private _cachedWidth: number = 0;

private _resizeObserver: ResizeObserver | null = null;

protected _center: IPoint = { x: 0, y: 0 };

protected _el: HTMLElement | null = null;
Expand Down Expand Up @@ -68,8 +74,7 @@ export class Viewport {
}

get height() {
this._height = this._el?.offsetHeight ?? this._height;
return this._height;
return this._cachedHeight;
}

get left() {
Expand Down Expand Up @@ -144,8 +149,7 @@ export class Viewport {
}

get width() {
this._width = this._el?.offsetWidth ?? this._width;
return this._width;
return this._cachedWidth;
}

get zoom() {
Expand All @@ -157,10 +161,18 @@ export class Viewport {
}

clearViewportElement() {
if (this._resizeObserver && this._el) {
this._resizeObserver.unobserve(this._el);
this._resizeObserver.disconnect();
}
this._resizeObserver = null;
this._el = null;
this._cachedWidth = 0;
this._cachedHeight = 0;
}

dispose() {
this.clearViewportElement();
this.sizeUpdated.dispose();
this.viewportMoved.dispose();
this.viewportUpdated.dispose();
Expand Down Expand Up @@ -213,6 +225,9 @@ export class Viewport {
centerX - (oldWidth - width) / zoom / 2,
centerY - (oldHeight - height) / zoom / 2
);

this._width = width;
this._height = height;
}

setCenter(centerX: number, centerY: number) {
Expand Down Expand Up @@ -285,6 +300,19 @@ export class Viewport {
this._el = el;

this.setRect(rect.left, rect.top, el.offsetWidth, el.offsetHeight);

this._resizeObserver = new ResizeObserver(entries => {
for (const entry of entries) {
if (entry.target === this._el) {
this._cachedWidth = entry.contentRect.width;
this._cachedHeight = entry.contentRect.height;
this.onResize();
}
}
});
this._resizeObserver.observe(el);
this._cachedWidth = el.offsetWidth;
this._cachedHeight = el.offsetHeight;
}

setZoom(zoom: number, focusPoint?: IPoint) {
Expand Down

0 comments on commit 0f25a1e

Please sign in to comment.