Skip to content

Commit

Permalink
Added PlatformBrowser check for IntersectionObserver to support SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Block committed Sep 25, 2024
1 parent 20d0574 commit 1f32345
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 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-particles",
"description": "A simple component library to create a container with an animated background.",
"version": "1.1.4",
"version": "1.1.5",
"peerDependencies": {
"@angular/common": "^18.2.0",
"@angular/core": "^18.2.0"
Expand Down
43 changes: 26 additions & 17 deletions src/lib/ngx-particles.component.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { CommonModule } from "@angular/common";
import {CommonModule, isPlatformBrowser} from "@angular/common";
import {
AfterViewInit,
Component,
ElementRef,
HostListener,
Inject,
Input,
OnDestroy,
PLATFORM_ID,
ViewChild,
} from "@angular/core";
import { Circle } from "./ngx-particles.types";
import {Circle} from "./ngx-particles.types";

@Component({
selector: "om-particles",
Expand Down Expand Up @@ -48,8 +50,8 @@ export class NgxParticlesComponent implements AfterViewInit, OnDestroy {
@Input("vy")
vy = 0;

private mousePosition: { x: number; y: number } = { x: 0, y: 0 };
private mouse: { x: number; y: number } = { x: 0, y: 0 };
private mousePosition: { x: number; y: number } = {x: 0, y: 0};
private mouse: { x: number; y: number } = {x: 0, y: 0};

private circles: Circle[] = [];

Expand All @@ -68,14 +70,21 @@ export class NgxParticlesComponent implements AfterViewInit, OnDestroy {
private animationFrameId?: number;
private intersectionObserver?: IntersectionObserver;

constructor(
@Inject(PLATFORM_ID) private platformId: object
) {
}

ngAfterViewInit(): void {
this.setCanvasSize();
this.animate();

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

window.addEventListener("resize", () => this.setCanvasSize());
}
Expand Down Expand Up @@ -156,7 +165,7 @@ export class NgxParticlesComponent implements AfterViewInit, OnDestroy {
return;
}

const { x, y, translateX, translateY, size, alpha } = circle;
const {x, y, translateX, translateY, size, alpha} = circle;
const rgb = this.hexToRgb(this.color);

context.translate(translateX, translateY);
Expand Down Expand Up @@ -186,14 +195,14 @@ export class NgxParticlesComponent implements AfterViewInit, OnDestroy {
const edge = [
circle.x + circle.translateX - circle.size, // distance from left edge
this.canvasRef.nativeElement.width -
circle.x -
circle.translateX -
circle.size, // distance from right edge
circle.x -
circle.translateX -
circle.size, // distance from right edge
circle.y + circle.translateY - circle.size, // distance from top edge
this.canvasRef.nativeElement.height -
circle.y -
circle.translateY -
circle.size, // distance from bottom edge
circle.y -
circle.translateY -
circle.size, // distance from bottom edge
];

const closestEdge = edge.reduce((a, b) => Math.min(a, b));
Expand Down Expand Up @@ -259,7 +268,7 @@ export class NgxParticlesComponent implements AfterViewInit, OnDestroy {

private onMouseMove(): void {
const rect = this.canvasRef.nativeElement.getBoundingClientRect();
const { w, h } = {
const {w, h} = {
w: this.canvasRef.nativeElement.width,
h: this.canvasRef.nativeElement.height,
};
Expand All @@ -270,7 +279,7 @@ export class NgxParticlesComponent implements AfterViewInit, OnDestroy {
const inside = x < w / 2 && x > -w / 2 && y < h / 2 && y > -h / 2;

if (inside) {
this.mouse = { x: x, y: y };
this.mouse = {x: x, y: y};
}
}

Expand Down

0 comments on commit 1f32345

Please sign in to comment.