Skip to content

Commit

Permalink
added changes to optimize performance
Browse files Browse the repository at this point in the history
  • Loading branch information
xSentry committed Aug 27, 2024
1 parent 228a258 commit a0ae2cb
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@omnedia/ngx-starry-sky",
"description": "A simple component library to create a container with an animated background.",
"version": "1.0.3",
"version": "1.0.4",
"peerDependencies": {
"@angular/common": "^18.2.0",
"@angular/core": "^18.2.0"
Expand Down
62 changes: 60 additions & 2 deletions src/lib/ngx-starry-sky.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,53 @@ export class NgxStarrySkyComponent implements AfterViewInit, OnDestroy {

private stars: StarProps[] = [];

private isInView = false;
private isAnimating = false;
private animationFrameIdSky?: number;
private animationFrameIdShootingStar?: number;
private intersectionObserver?: IntersectionObserver;

ngAfterViewInit(): void {
this.initStarSky();
this.initShootingStars();

this.intersectionObserver = new IntersectionObserver(([entry]) => {
this.renderContents(entry.isIntersecting);
});
this.intersectionObserver.observe(this.canvasRef.nativeElement);
}

ngOnDestroy(): void {
window.removeEventListener("resize", () => this.setCanvasSize());

if (this.intersectionObserver) {
this.intersectionObserver.disconnect();
}

if (this.animationFrameIdSky) {
cancelAnimationFrame(this.animationFrameIdSky);
}

if (this.animationFrameIdShootingStar) {
cancelAnimationFrame(this.animationFrameIdShootingStar);
}
}

renderContents(isIntersecting: boolean) {
if (isIntersecting && !this.isInView) {
this.isInView = true;

if (!this.isAnimating) {
this.animationFrameIdSky = requestAnimationFrame(() =>
this.renderStarSky()
);
this.animationFrameIdShootingStar = requestAnimationFrame(() =>
this.moveShootingStar()
);
}
} else if (!isIntersecting) {
this.isInView = false;
}
}

private initStarSky(): void {
Expand All @@ -104,6 +144,13 @@ export class NgxStarrySkyComponent implements AfterViewInit, OnDestroy {
}

private renderStarSky(): void {
if (!this.isInView) {
this.isAnimating = false;
return;
}

this.isAnimating = true;

const context = this.canvasRef.nativeElement.getContext("2d");

if (!context) {
Expand All @@ -130,7 +177,9 @@ export class NgxStarrySkyComponent implements AfterViewInit, OnDestroy {
}
});

window.requestAnimationFrame(() => this.renderStarSky());
this.animationFrameIdSky = requestAnimationFrame(() =>
this.renderStarSky()
);
}

private updateStars(): void {
Expand Down Expand Up @@ -211,7 +260,16 @@ export class NgxStarrySkyComponent implements AfterViewInit, OnDestroy {
return;
}

window.requestAnimationFrame(() => this.moveShootingStar());
if (!this.isInView) {
this.isAnimating = false;
return;
}

this.isAnimating = true;

this.animationFrameIdShootingStar = requestAnimationFrame(() =>
this.moveShootingStar()
);

if (!this.shootingStar) {
return;
Expand Down

0 comments on commit a0ae2cb

Please sign in to comment.