Astro Integration
Use tsParticles in your Astro site with the official @tsparticles/astro integration package.
Installation
Install the Astro integration and tsParticles core via your package manager:
npm install @tsparticles/astro tsparticlespnpm add @tsparticles/astro tsparticlesyarn add @tsparticles/astro tsparticlesEngine Initialization
tsParticles uses a modular architecture. Before rendering particles, you must initialize the engine with the features you need. Create a client script (e.g., src/scripts/particles-init.ts) or use an inline <script> in your Astro component:
import { initParticlesEngine } from "@tsparticles/astro";
void initParticlesEngine(async (engine) => {
const { loadFull } = await import("tsparticles");
await loadFull(engine);
});
initParticlesEngineis a wrapper aroundtsParticles.init()that ensures the engine is ready before the<Particles>component mounts. It returns aPromisethat resolves once initialization is complete.
Basic Usage
Place the <Particles /> component in any .astro template. Pass your configuration via the options prop:
---
import Particles from "@tsparticles/astro";
import type { ISourceOptions } from "@tsparticles/engine";
const options: ISourceOptions = {
background: {
color: "#000000",
},
particles: {
number: { value: 80 },
links: { enable: true, color: "#ffffff" },
move: { enable: true, speed: 2 },
},
};
---
<Particles id="tsparticles" options={options} />
<script>
import { initParticlesEngine } from "@tsparticles/astro";
void initParticlesEngine(async (engine) => {
const { loadFull } = await import("tsparticles");
await loadFull(engine);
});
</script>The
idprop is passed to the underlying canvas container<div>. Use it for styling or imperative access viadocument.getElementById().
TypeScript Support
The integration ships full TypeScript declarations. Use ISourceOptions from @tsparticles/engine to type your configuration:
import type { ISourceOptions } from "@tsparticles/engine";
const options: ISourceOptions = {
background: { color: "#0d47a1" },
fpsLimit: 60,
particles: {
color: {
value: ["#ff0000", "#00ff00", "#0000ff"],
},
move: {
enable: true,
speed: 3,
outModes: "bounce",
},
number: {
value: 50,
density: { enable: true },
},
opacity: { value: 0.5 },
shape: { type: "circle" },
size: { value: { min: 2, max: 6 } },
},
interactivity: {
events: {
onHover: { enable: true, mode: "repulse" },
},
modes: {
repulse: { distance: 200 },
},
},
};Custom Configuration
Below is a more elaborate configuration that you can drop into any Astro page:
---
import Particles from "@tsparticles/astro";
import type { ISourceOptions } from "@tsparticles/engine";
const options: ISourceOptions = {
autoPlay: true,
background: {
color: "#0d47a1",
image: "",
position: "50% 50%",
repeat: "no-repeat",
size: "cover",
},
backgroundMode: {
enable: true,
zIndex: -1,
},
fpsLimit: 120,
particles: {
color: {
value: "#ffffff",
animation: {
enable: true,
speed: 20,
sync: false,
},
},
links: {
enable: true,
distance: 150,
color: "#ffffff",
opacity: 0.4,
width: 1,
triangles: {
enable: true,
opacity: 0.1,
},
},
move: {
enable: true,
speed: 3,
direction: "none",
random: true,
straight: false,
outModes: "out",
attract: { enable: false },
},
number: {
value: 80,
density: {
enable: true,
},
},
opacity: {
value: 0.5,
animation: {
enable: true,
speed: 1,
sync: false,
},
},
shape: {
type: "circle",
},
size: {
value: { min: 1, max: 5 },
animation: {
enable: true,
speed: 3,
sync: false,
},
},
},
interactivity: {
events: {
onHover: {
enable: true,
mode: "grab",
},
onClick: {
enable: true,
mode: "push",
},
resize: true,
},
modes: {
grab: {
distance: 200,
links: { opacity: 0.5 },
},
push: { quantity: 4 },
},
},
detectRetina: true,
};
---
<Particles id="tsparticles" options={options} />
<script>
import { initParticlesEngine } from "@tsparticles/astro";
void initParticlesEngine(async (engine) => {
const { loadFull } = await import("tsparticles");
await loadFull(engine);
});
</script>Using Presets
Instead of building a configuration manually, load a preset during engine initialization and reference it in the options:
---
import Particles from "@tsparticles/astro";
import type { ISourceOptions } from "@tsparticles/engine";
const options: ISourceOptions = {
preset: "stars",
background: { color: "#000000" },
};
---
<Particles id="tsparticles" options={options} />
<script>
import { initParticlesEngine } from "@tsparticles/astro";
void initParticlesEngine(async (engine) => {
const { loadStarsPreset } = await import("tsparticles-preset-stars");
await loadStarsPreset(engine);
});
</script>Integration with Other Frameworks
Because Astro supports UI frameworks like React, Vue, Svelte, and Solid, you can use the framework-specific tsParticles component within .astro files:
React in Astro
---
import Particles from "@tsparticles/react";
import type { ISourceOptions } from "@tsparticles/engine";
const options: ISourceOptions = {
background: { color: "#000" },
particles: {
number: { value: 60 },
links: { enable: true },
move: { enable: true },
},
};
---
<Particles client:load id="tsparticles" options={options} />Vue in Astro
---
import Particles from "@tsparticles/vue3";
import type { ISourceOptions } from "@tsparticles/engine";
const options: ISourceOptions = {
background: { color: "#000" },
particles: {
number: { value: 60 },
links: { enable: true },
move: { enable: true },
},
};
---
<Particles client:load id="tsparticles" :options="options" />The
client:loaddirective tells Astro to hydrate the component immediately on page load. Useclient:visiblefor deferred loading.
Full Page Example
A complete Astro page with particles serving as an animated background:
---
import Layout from "../layouts/Layout.astro";
import Particles from "@tsparticles/astro";
import type { ISourceOptions } from "@tsparticles/engine";
const options: ISourceOptions = {
background: { color: "#0d47a1" },
fpsLimit: 60,
particles: {
number: { value: 100 },
color: { value: "#ffffff" },
links: {
enable: true,
distance: 150,
color: "#ffffff",
opacity: 0.4,
},
move: {
enable: true,
speed: 2,
outModes: "out",
},
size: {
value: { min: 1, max: 4 },
},
},
interactivity: {
events: {
onHover: { enable: true, mode: "grab" },
onClick: { enable: true, mode: "push" },
},
modes: {
grab: { distance: 200, links: { opacity: 0.5 } },
push: { quantity: 4 },
},
},
};
---
<Layout title="Particles Background">
<main>
<h1>Welcome</h1>
<p>This page has a particle background powered by tsParticles.</p>
</main>
<Particles id="bg-particles" options={options} />
</Layout>
<style is:global>
#bg-particles {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
}
main {
position: relative;
z-index: 1;
color: white;
text-align: center;
padding-top: 20vh;
}
</style>
<script>
import { initParticlesEngine } from "@tsparticles/astro";
void initParticlesEngine(async (engine) => {
const { loadFull } = await import("tsparticles");
await loadFull(engine);
});
</script>Component Props
| Prop | Type | Default | Description |
|---|---|---|---|
id | string | "tsparticles" | DOM element id for the container |
options | ISourceOptions | {} | Full tsParticles configuration object |
url | string | — | Load configuration from a remote JSON URL |
particlesClassName | string | "tsparticles-canvas-el" | CSS class for the canvas element |
container | object | — | Pre-existing Container instance (advanced) |
