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 0dfb9a4 commit f61e507
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 28 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-three-globe",
"description": "A simple component library to create a container with an animated and interactive globe.",
"version": "1.0.1",
"version": "1.0.2",
"peerDependencies": {
"@angular/common": "^18.2.0",
"@angular/core": "^18.2.0",
Expand Down
50 changes: 23 additions & 27 deletions src/lib/ngx-three-globe.component.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import { CommonModule } from "@angular/common";
import {
AfterViewInit,
Component,
ElementRef,
Input,
OnDestroy,
ViewChild,
} from "@angular/core";
import {CommonModule, isPlatformBrowser} from "@angular/common";
import {AfterViewInit, Component, ElementRef, Inject, Input, OnDestroy, PLATFORM_ID, ViewChild,} from "@angular/core";
import {
AmbientLight,
Color,
Expand All @@ -19,13 +12,9 @@ import {
WebGLRenderer,
} from "three";
import ThreeGlobe from "three-globe";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
import { getData } from "./globe-data";
import {
ThreeGlobeConfig,
ThreeGlobeData,
ThreeGlobePosition,
} from "./ngx-three-globe.types";
import {OrbitControls} from "three/examples/jsm/controls/OrbitControls.js";
import {getData} from "./globe-data";
import {ThreeGlobeConfig, ThreeGlobeData, ThreeGlobePosition,} from "./ngx-three-globe.types";

@Component({
selector: "om-three-globe",
Expand All @@ -47,7 +36,7 @@ export class NgxThreeGlobeComponent implements AfterViewInit, OnDestroy {

@ViewChild("GlobeCanvas") rendererContainer!: ElementRef;

private renderer = new WebGLRenderer({ alpha: true });
private renderer = new WebGLRenderer({alpha: true});
private scene = new Scene();
private globe = new ThreeGlobe();
private camera = new PerspectiveCamera();
Expand All @@ -59,7 +48,7 @@ export class NgxThreeGlobeComponent implements AfterViewInit, OnDestroy {

@Input("globeConfig")
set newGlobeConfig(config: ThreeGlobeConfig) {
this.globeConfig = { ...this.globeConfig, ...config };
this.globeConfig = {...this.globeConfig, ...config};
}

private globeConfig: ThreeGlobeConfig = {
Expand Down Expand Up @@ -426,15 +415,22 @@ export class NgxThreeGlobeComponent implements AfterViewInit, OnDestroy {
private animationFrameId?: number;
private intersectionObserver?: IntersectionObserver;

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

ngAfterViewInit(): void {
this.countries = getData();
this.setArcColors();
this.initRenderer();

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

ngOnDestroy(): void {
Expand Down Expand Up @@ -667,7 +663,7 @@ export class NgxThreeGlobeComponent implements AfterViewInit, OnDestroy {
.ringPropagationSpeed(3)
.ringRepeatPeriod(
((this.globeConfig.arcTime ?? 0) * (this.globeConfig.arcLength ?? 0)) /
(this.globeConfig.rings ?? 1)
(this.globeConfig.rings ?? 1)
);
}

Expand All @@ -680,10 +676,10 @@ export class NgxThreeGlobeComponent implements AfterViewInit, OnDestroy {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result
? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16),
}
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16),
}
: null;
}

Expand Down

0 comments on commit f61e507

Please sign in to comment.