Skip to content

Commit c26e9f2

Browse files
committed
feat: added service for initializing particles plugins
1 parent 168f122 commit c26e9f2

File tree

8 files changed

+83
-29
lines changed

8 files changed

+83
-29
lines changed

apps/angular-demo/src/app/app.component.html

+1-2
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@
226226

227227
.circle-link:hover {
228228
transform: translateY(-0.25rem);
229-
box-shadow: 0px 3px 15px rgba(0, 0, 0, 0.2);
229+
box-shadow: 0 3px 15px rgba(0, 0, 0, 0.2);
230230
}
231231

232232
footer {
@@ -836,7 +836,6 @@ <h2>Next Steps</h2>
836836
<ngx-particles
837837
id="tsparticles"
838838
[options]="particlesOptions"
839-
[particlesInit]="particlesInit"
840839
(particlesLoaded)="particlesLoaded($event)"
841840
*ngIf="particlesVisible"
842841
></ngx-particles>

apps/angular-demo/src/app/app.component.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import { Component } from "@angular/core";
22
import type { Container, Engine, ISourceOptions } from "@tsparticles/engine";
33
import { loadFull } from "tsparticles";
44
import configs from "@tsparticles/configs";
5+
import { NgParticlesService } from "@tsparticles/angular";
56

67
@Component({
78
selector: "app-root",
89
templateUrl: "./app.component.html",
9-
styleUrls: ["./app.component.css"]
10+
styleUrls: [ "./app.component.css" ]
1011
})
1112
export class AppComponent {
12-
title = "angular13";
13+
title = "angular";
1314
id = "tsparticles";
1415
fire = 0;
1516
particlesVisible = true;
@@ -42,16 +43,15 @@ export class AppComponent {
4243
//this.confettiVisible = !this.confettiVisible;
4344
}
4445

45-
constructor() {
46+
constructor(private ngParticlesService: NgParticlesService) {
4647
}
4748

4849
ngOnInit(): void {
49-
}
50-
51-
async particlesInit(engine: Engine): Promise<void> {
52-
console.log("init", engine);
50+
void this.ngParticlesService.init(async (engine: Engine) => {
51+
console.log("init", engine);
5352

54-
await loadFull(engine);
53+
await loadFull(engine);
54+
});
5555
}
5656

5757
public particlesLoaded(container: Container): void {
+13-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
import { Component } from '@angular/core';
2+
import { NgParticlesService } from "@tsparticles/angular";
3+
import { loadFull } from "tsparticles";
24

35
@Component({
46
selector: 'app-root',
57
templateUrl: 'app.component.html',
6-
styleUrls: ['app.component.scss'],
8+
styleUrls: [ 'app.component.scss' ],
79
})
810
export class AppComponent {
9-
constructor() {}
11+
constructor(private ngParticlesService: NgParticlesService) {
12+
}
13+
14+
ngOnInit(): void {
15+
void this.ngParticlesService.init(async (engine) => {
16+
console.log("init", engine);
17+
18+
await loadFull(engine);
19+
});
20+
}
1021
}

apps/ionic-demo/src/app/tab1/tab1.page.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@
1616
<app-explore-container name="Tab 1 page"></app-explore-container>
1717

1818
<ngx-particles [id]="particlesId" [options]="particlesOptions"
19-
(particlesLoaded)="particlesLoaded($event)" [particlesInit]="particlesInit"></ngx-particles>
19+
(particlesLoaded)="particlesLoaded($event)"></ngx-particles>
2020
</ion-content>

apps/ionic-demo/src/app/tab1/tab1.page.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Component } from "@angular/core";
2-
import type { Container, Engine } from "@tsparticles/engine";
3-
import { loadFull } from "tsparticles";
2+
import type { Container } from "@tsparticles/engine";
43
import configs from "@tsparticles/configs";
54

65
@Component({
@@ -21,8 +20,4 @@ export class Tab1Page {
2120
container.refresh();
2221
}, 500);
2322
}
24-
25-
async particlesInit(main: Engine): Promise<void> {
26-
await loadFull(main);
27-
}
2823
}
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,65 @@
1-
import { AfterViewInit, Component, EventEmitter, Inject, Input, OnDestroy, Output, PLATFORM_ID } from '@angular/core';
1+
import {
2+
AfterViewInit,
3+
Component,
4+
EventEmitter,
5+
Inject,
6+
Input,
7+
OnDestroy,
8+
OnInit,
9+
Output,
10+
PLATFORM_ID,
11+
} from '@angular/core';
212
import { isPlatformServer } from '@angular/common';
3-
import { from, mergeMap, Subject, takeUntil } from 'rxjs';
13+
import { from, mergeMap, Subject, Subscription, takeUntil } from 'rxjs';
414
import { tsParticles } from '@tsparticles/engine';
515
import type { Container, Engine } from '@tsparticles/engine';
616
import { IParticlesProps } from './ng-particles.module';
17+
import { NgParticlesService } from './ng-particles.service';
718

819
@Component({
920
selector: 'ngx-particles',
1021
template: '<div [id]="id"></div>',
1122
})
12-
export class NgxParticlesComponent implements AfterViewInit, OnDestroy {
23+
export class NgxParticlesComponent implements OnInit, AfterViewInit, OnDestroy {
1324
@Input() options?: IParticlesProps;
1425
@Input() url?: string;
1526
@Input() id: string;
1627
@Input() particlesInit?: (engine: Engine) => Promise<void>;
1728
@Output() particlesLoaded: EventEmitter<Container> = new EventEmitter<Container>();
1829

30+
private subscription?: Subscription;
1931
private destroy$ = new Subject<void>();
2032
private container?: Container;
2133

22-
constructor(@Inject(PLATFORM_ID) protected platformId: string) {
34+
constructor(
35+
@Inject(PLATFORM_ID) protected platformId: string,
36+
private readonly particlesService: NgParticlesService,
37+
) {
2338
this.id = 'tsparticles';
2439
}
2540

41+
public ngOnInit() {
42+
this.subscription = this.particlesService.getInstallationStatus().subscribe(() => {
43+
this.container?.destroy();
44+
this.loadParticles();
45+
});
46+
}
47+
2648
public ngAfterViewInit(): void {
2749
if (isPlatformServer(this.platformId)) {
2850
return;
2951
}
3052

53+
this.loadParticles();
54+
}
55+
56+
public ngOnDestroy(): void {
57+
this.container?.destroy();
58+
this.subscription?.unsubscribe();
59+
this.destroy$.next();
60+
}
61+
62+
private loadParticles(): void {
3163
const cb = (container?: Container) => {
3264
this.container = container;
3365
this.particlesLoaded.emit(container);
@@ -42,9 +74,4 @@ export class NgxParticlesComponent implements AfterViewInit, OnDestroy {
4274
)
4375
.subscribe(cb);
4476
}
45-
46-
public ngOnDestroy(): void {
47-
this.container?.destroy();
48-
this.destroy$.next();
49-
}
5077
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { NgModule } from '@angular/core';
22
import { NgxParticlesComponent } from './ng-particles.component';
33
import type { ISourceOptions } from '@tsparticles/engine';
4+
import { NgParticlesService } from './ng-particles.service';
45

56
@NgModule({
67
declarations: [NgxParticlesComponent],
78
exports: [NgxParticlesComponent],
9+
providers: [NgParticlesService],
810
})
911
export class NgxParticlesModule {}
1012

1113
export type IParticlesProps = ISourceOptions;
12-
export type IParticlesParams = IParticlesProps;
14+
export { NgParticlesService };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Injectable } from '@angular/core';
2+
import { BehaviorSubject } from 'rxjs';
3+
import { Engine, tsParticles } from '@tsparticles/engine';
4+
5+
@Injectable({
6+
providedIn: 'root',
7+
})
8+
export class NgParticlesService {
9+
private initialized = new BehaviorSubject<boolean>(false);
10+
11+
getInstallationStatus() {
12+
return this.initialized.asObservable();
13+
}
14+
15+
async init(particlesInit: (engine: Engine) => Promise<void>) {
16+
await particlesInit(tsParticles);
17+
18+
this.initialized.next(true);
19+
}
20+
}

0 commit comments

Comments
 (0)