Skip to content

Commit f61e507

Browse files
author
Markus Block
committed
Added PlatformBrowser check for IntersectionObserver to support SSR
1 parent 0dfb9a4 commit f61e507

File tree

2 files changed

+24
-28
lines changed

2 files changed

+24
-28
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@omnedia/ngx-three-globe",
33
"description": "A simple component library to create a container with an animated and interactive globe.",
4-
"version": "1.0.1",
4+
"version": "1.0.2",
55
"peerDependencies": {
66
"@angular/common": "^18.2.0",
77
"@angular/core": "^18.2.0",

src/lib/ngx-three-globe.component.ts

+23-27
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
import { CommonModule } from "@angular/common";
2-
import {
3-
AfterViewInit,
4-
Component,
5-
ElementRef,
6-
Input,
7-
OnDestroy,
8-
ViewChild,
9-
} from "@angular/core";
1+
import {CommonModule, isPlatformBrowser} from "@angular/common";
2+
import {AfterViewInit, Component, ElementRef, Inject, Input, OnDestroy, PLATFORM_ID, ViewChild,} from "@angular/core";
103
import {
114
AmbientLight,
125
Color,
@@ -19,13 +12,9 @@ import {
1912
WebGLRenderer,
2013
} from "three";
2114
import ThreeGlobe from "three-globe";
22-
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
23-
import { getData } from "./globe-data";
24-
import {
25-
ThreeGlobeConfig,
26-
ThreeGlobeData,
27-
ThreeGlobePosition,
28-
} from "./ngx-three-globe.types";
15+
import {OrbitControls} from "three/examples/jsm/controls/OrbitControls.js";
16+
import {getData} from "./globe-data";
17+
import {ThreeGlobeConfig, ThreeGlobeData, ThreeGlobePosition,} from "./ngx-three-globe.types";
2918

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

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

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

6049
@Input("globeConfig")
6150
set newGlobeConfig(config: ThreeGlobeConfig) {
62-
this.globeConfig = { ...this.globeConfig, ...config };
51+
this.globeConfig = {...this.globeConfig, ...config};
6352
}
6453

6554
private globeConfig: ThreeGlobeConfig = {
@@ -426,15 +415,22 @@ export class NgxThreeGlobeComponent implements AfterViewInit, OnDestroy {
426415
private animationFrameId?: number;
427416
private intersectionObserver?: IntersectionObserver;
428417

418+
constructor(
419+
@Inject(PLATFORM_ID) private platformId: object
420+
) {
421+
}
422+
429423
ngAfterViewInit(): void {
430424
this.countries = getData();
431425
this.setArcColors();
432426
this.initRenderer();
433427

434-
this.intersectionObserver = new IntersectionObserver(([entry]) => {
435-
this.renderContents(entry.isIntersecting);
436-
});
437-
this.intersectionObserver.observe(this.rendererContainer.nativeElement);
428+
if (isPlatformBrowser(this.platformId)) {
429+
this.intersectionObserver = new IntersectionObserver(([entry]) => {
430+
this.renderContents(entry.isIntersecting);
431+
});
432+
this.intersectionObserver.observe(this.rendererContainer.nativeElement);
433+
}
438434
}
439435

440436
ngOnDestroy(): void {
@@ -667,7 +663,7 @@ export class NgxThreeGlobeComponent implements AfterViewInit, OnDestroy {
667663
.ringPropagationSpeed(3)
668664
.ringRepeatPeriod(
669665
((this.globeConfig.arcTime ?? 0) * (this.globeConfig.arcLength ?? 0)) /
670-
(this.globeConfig.rings ?? 1)
666+
(this.globeConfig.rings ?? 1)
671667
);
672668
}
673669

@@ -680,10 +676,10 @@ export class NgxThreeGlobeComponent implements AfterViewInit, OnDestroy {
680676
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
681677
return result
682678
? {
683-
r: parseInt(result[1], 16),
684-
g: parseInt(result[2], 16),
685-
b: parseInt(result[3], 16),
686-
}
679+
r: parseInt(result[1], 16),
680+
g: parseInt(result[2], 16),
681+
b: parseInt(result[3], 16),
682+
}
687683
: null;
688684
}
689685

0 commit comments

Comments
 (0)