Angular Integration
The @tsparticles/angular package provides Angular components, modules, and services for tsParticles. This guide covers the traditional NgModule approach as well as Angular 17+ standalone components.
Installation
npm install @tsparticles/angular @tsparticles/engineFor the full feature set, install the complete bundle:
npm install tsparticlesOptional preset packages:
npm install @tsparticles/preset-confetti
npm install @tsparticles/preset-fireworks
npm install @tsparticles/preset-snow
npm install @tsparticles/preset-starsBasic Usage (NgModule)
1. Import the Module
import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { NgParticlesModule } from "@tsparticles/angular";
import { AppComponent } from "./app.component";
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, NgParticlesModule],
bootstrap: [AppComponent],
})
export class AppModule {}2. Initialise the Engine
import { Component, OnInit } from "@angular/core";
import type { Container, Engine, ISourceOptions } from "@tsparticles/engine";
import { loadFull } from "tsparticles";
import { NgParticlesService } from "@tsparticles/angular";
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"],
})
export class AppComponent implements OnInit {
constructor(private readonly ngParticlesService: NgParticlesService) {}
ngOnInit(): void {
void this.ngParticlesService.init(async (engine: Engine) => {
await loadFull(engine);
});
}
particlesOptions: ISourceOptions = {
background: {
color: "#0d47a1",
},
fpsLimit: 120,
particles: {
number: {
value: 80,
},
color: {
value: "#ffffff",
},
shape: {
type: "circle",
},
opacity: {
value: 0.5,
},
size: {
value: { min: 1, max: 5 },
},
links: {
enable: true,
distance: 150,
color: "#ffffff",
opacity: 0.4,
width: 1,
},
move: {
enable: true,
speed: 2,
outModes: "out",
},
},
};
particlesLoaded(container: Container): void {
console.log("Particles container loaded", container);
}
}3. Template
<ngx-particles
id="tsparticles"
[options]="particlesOptions"
(particlesLoaded)="particlesLoaded($event)"
></ngx-particles>Engine Initialisation Details
The NgParticlesService.init() method must be called exactly once, typically in AppComponent.ngOnInit(). It receives a callback where you load the plugins/presets your application needs.
import { Component, OnInit } from "@angular/core";
import { NgParticlesService } from "@tsparticles/angular";
import type { Engine } from "@tsparticles/engine";
@Component({ ... })
export class AppComponent implements OnInit {
constructor(private readonly ngParticlesService: NgParticlesService) {}
ngOnInit(): void {
void this.ngParticlesService.init(async (engine: Engine) => {
// Load only what you need for smaller bundles
await loadBasic(engine); // basic shapes + move
await loadEmittersPlugin(engine); // emitter shapes
});
}
}Available loader functions from tsparticles:
| Function | Description |
|---|---|
loadFull(engine) | All features (largest bundle) |
loadBasic(engine) | Core shapes (circle, square, polygon, etc.) |
loadSlim(engine) | Most features minus rarely used plugins |
loadAll(engine) | Deprecated alias for loadFull |
Confetti Effect
npm install @tsparticles/preset-confettiimport { loadConfettiPreset } from "@tsparticles/preset-confetti";
// In NgParticlesService.init callback:
await loadConfettiPreset(engine);particlesOptions: ISourceOptions = {
preset: "confetti",
background: {
color: "transparent",
},
};Or use the convenience <ngx-confetti> component:
// app.module.ts
import { NgParticlesModule } from "@tsparticles/angular";
@NgModule({
imports: [NgParticlesModule],
})
export class AppModule {}<ngx-confetti
[options]="{
particleCount: 200,
colors: ['#ff0000', '#00ff00', '#0000ff', '#ffff00']
}"
></ngx-confetti>Fireworks Effect
npm install @tsparticles/preset-fireworksimport { loadFireworksPreset } from "@tsparticles/preset-fireworks";
// In NgParticlesService.init callback:
await loadFireworksPreset(engine);particlesOptions: ISourceOptions = {
preset: "fireworks",
background: {
color: "#000000",
},
};Or use the <ngx-fireworks> component:
<ngx-fireworks
[options]="{
explosion: 8,
intensity: 30,
flickering: 50,
traceLength: 3
}"
></ngx-fireworks>Avoid auto-starting fireworks; bind them to a user action (click, scroll) to prevent unwanted resource usage.
Custom Particles Configuration
Full-featured custom particle setup with interactivity:
import { Component, OnInit } from "@angular/core";
import type { Container, Engine, ISourceOptions } from "@tsparticles/engine";
import { loadFull } from "tsparticles";
import { NgParticlesService } from "@tsparticles/angular";
@Component({
selector: "app-particles",
templateUrl: "./particles.component.html",
})
export class ParticlesComponent implements OnInit {
constructor(private readonly ngParticlesService: NgParticlesService) {}
ngOnInit(): void {
void this.ngParticlesService.init(async (engine: Engine) => {
await loadFull(engine);
});
}
particlesOptions: ISourceOptions = {
background: {
color: "#0d0d0d",
},
fpsLimit: 60,
particles: {
number: {
value: 100,
density: {
enable: true,
},
},
color: {
value: ["#ff0000", "#00ff00", "#0000ff", "#ffff00", "#ff00ff"],
},
shape: {
type: ["circle", "square", "triangle"],
},
opacity: {
value: 0.8,
random: true,
anim: {
enable: true,
speed: 1,
sync: false,
},
},
size: {
value: { min: 2, max: 8 },
random: true,
anim: {
enable: true,
speed: 4,
size_min: 1,
sync: false,
},
},
links: {
enable: true,
distance: 150,
color: "#ffffff",
opacity: 0.3,
width: 1,
triangles: {
enable: true,
color: "#ffffff",
opacity: 0.05,
},
},
move: {
enable: true,
speed: 3,
direction: "none",
random: true,
straight: false,
outModes: "bounce",
attract: {
enable: true,
rotateX: 600,
rotateY: 600,
},
},
life: {
duration: {
value: 5,
random: true,
},
count: 0,
},
},
interactivity: {
events: {
onHover: {
enable: true,
mode: "grab",
},
onClick: {
enable: true,
mode: "push",
},
resize: {
enable: true,
},
},
modes: {
grab: {
distance: 200,
links: {
opacity: 0.5,
},
},
push: {
quantity: 4,
},
},
},
detectRetina: true,
};
particlesLoaded(container: Container): void {
console.log("Container loaded", container);
}
}<ngx-particles
id="tsparticles"
[options]="particlesOptions"
(particlesLoaded)="particlesLoaded($event)"
></ngx-particles>Events
The ngx-particles component emits the particlesLoaded event:
import type { Container } from "@tsparticles/engine";
// Component method
onParticlesLoaded(container: Container): void {
// Access the container API
container.pause();
container.play();
container.destroy();
container.exportImage().then((blob) => { /* ... */ });
}<ngx-particles
id="tsparticles"
[options]="particlesOptions"
(particlesLoaded)="onParticlesLoaded($event)"
></ngx-particles>The container reference gives you full programmatic control: pause, resume, destroy, export, and more.
Template Syntax & Conditional Rendering
Use Angular structural directives to toggle the component:
<button (click)="showParticles = !showParticles">Toggle Particles</button>
<ngx-particles
*ngIf="showParticles"
id="tsparticles"
[options]="particlesOptions"
(particlesLoaded)="particlesLoaded($event)"
></ngx-particles>export class AppComponent {
showParticles = true;
// ...
}When *ngIf evaluates to false, the component is destroyed (including the canvas and all particle instances). Re-creating it re-initialises everything from scratch.
Standalone Components (Angular 17+)
In Angular 17+, you can import NgParticlesModule directly into a standalone component:
import { Component, OnInit } from "@angular/core";
import { NgParticlesModule, NgParticlesService } from "@tsparticles/angular";
import { loadFull } from "tsparticles";
import type { Container, Engine, ISourceOptions } from "@tsparticles/engine";
@Component({
selector: "app-particles",
standalone: true,
imports: [NgParticlesModule],
template: `
<ngx-particles
id="tsparticles"
[options]="particlesOptions"
(particlesLoaded)="particlesLoaded($event)"
></ngx-particles>
`,
})
export class ParticlesComponent implements OnInit {
constructor(private readonly ngParticlesService: NgParticlesService) {}
ngOnInit(): void {
void this.ngParticlesService.init(async (engine: Engine) => {
await loadFull(engine);
});
}
particlesOptions: ISourceOptions = {
background: { color: "#000" },
particles: {
number: { value: 50 },
color: { value: "#fff" },
shape: { type: "circle" },
move: { enable: true, speed: 2 },
},
};
particlesLoaded(container: Container): void {
console.log("Loaded", container);
}
}No NgModule wrapper needed — just import NgParticlesModule in the component's imports array.
Full Component Example
app.component.ts
import { Component, OnInit } from "@angular/core";
import type { Container, Engine, ISourceOptions } from "@tsparticles/engine";
import { loadSlim } from "@tsparticles/slim";
import { NgParticlesService } from "@tsparticles/angular";
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"],
})
export class AppComponent implements OnInit {
title = "tsParticles Angular Demo";
constructor(private readonly ngParticlesService: NgParticlesService) {}
ngOnInit(): void {
void this.ngParticlesService.init(async (engine: Engine) => {
await loadSlim(engine);
});
}
particlesOptions: ISourceOptions = {
autoPlay: true,
background: {
color: "#1e1e2e",
image: "",
position: "50% 50%",
repeat: "no-repeat",
size: "cover",
},
backgroundMask: {
cover: {
color: "#1e1e2e",
},
enable: false,
},
fullScreen: {
enable: true,
zIndex: -1,
},
detectRetina: true,
fpsLimit: 60,
particles: {
color: {
value: "#cdd6f4",
},
links: {
color: "#cdd6f4",
distance: 150,
enable: true,
opacity: 0.3,
width: 1,
},
move: {
enable: true,
speed: 1,
},
number: {
value: 60,
},
opacity: {
value: 0.6,
},
size: {
value: { min: 1, max: 3 },
},
},
};
particlesLoaded(container: Container): void {
console.log("Particles loaded", container);
}
}app.component.html
<div style="position: relative; width: 100%; height: 100vh;">
<ngx-particles
id="tsparticles"
[options]="particlesOptions"
(particlesLoaded)="particlesLoaded($event)"
></ngx-particles>
<div
style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); color: white; text-align: center;"
>
<h1>{{ title }}</h1>
<p>Particles are running in the background.</p>
</div>
</div>app.component.css
:host {
display: block;
width: 100%;
height: 100%;
}API Reference
| Component | Selector | Description |
|---|---|---|
| Particles | ngx-particles | Full particle system component |
| Confetti | ngx-confetti | Pre-configured confetti effect |
| Fireworks | ngx-fireworks | Pre-configured fireworks effect |
ngx-particles Inputs
| Input | Type | Default | Description |
|---|---|---|---|
id | string | "tsparticles" | Canvas element ID |
options | ISourceOptions | {} | Particle configuration |
url | string | — | Remote JSON config URL |
ngx-particles Outputs
| Output | Payload | Description |
|---|---|---|
particlesLoaded | Container | Emitted when the container is initialised |
Troubleshooting
- Blank / invisible canvas — Ensure the parent element has a defined height (e.g.,
height: 100vh). The canvas takes the container dimensions. NgParticlesService.init()called multiple times — Call it only once, typically inAppComponent.ngOnInit(). Subsequent calls are safe but redundant.- Module not found — Verify
@tsparticles/angularis listed inpackage.jsondependencies and that you importedNgParticlesModule. NullInjectorError: No provider for NgParticlesService— You must importNgParticlesModule(or re-export it) in the module where you provide the component.
