Astro-Integration
Verwenden Sie tsParticles auf Ihrer Astro-Seite mit dem offiziellen @tsparticles/astro-Integrationspaket.
Installation
Installieren Sie die Astro-Integration und den tsParticles-Kern über Ihren Paketmanager:
npm install @tsparticles/astro tsparticlespnpm add @tsparticles/astro tsparticlesyarn add @tsparticles/astro tsparticlesEngine-Initialisierung
tsParticles verwendet eine modulare Architektur. Bevor Partikel gerendert werden, müssen Sie die Engine mit den benötigten Funktionen initialisieren. Erstellen Sie ein Client-Script (z. B. src/scripts/particles-init.ts) oder verwenden Sie ein Inline-<script> in Ihrer Astro-Komponente:
import { initParticlesEngine } from "@tsparticles/astro";
void initParticlesEngine(async (engine) => {
const { loadFull } = await import("tsparticles");
await loadFull(engine);
});
initParticlesEngineist ein Wrapper umtsParticles.init(), der sicherstellt, dass die Engine bereit ist, bevor die<Particles>-Komponente montiert wird. Sie gibt einPromisezurück, das nach Abschluss der Initialisierung aufgelöst wird.
Grundlegende Verwendung
Platzieren Sie die <Particles />-Komponente in einer beliebigen .astro-Vorlage. Übergeben Sie Ihre Konfiguration über die options-Eigenschaft:
---
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>Die
id-Eigenschaft wird an den zugrunde liegenden Canvas-Container<div>weitergegeben. Verwenden Sie sie für Styling oder den imperativen Zugriff viadocument.getElementById().
TypeScript-Unterstützung
Die Integration enthält vollständige TypeScript-Deklarationen. Verwenden Sie ISourceOptions von @tsparticles/engine, um Ihre Konfiguration zu typisieren:
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 },
},
},
};Benutzerdefinierte Konfiguration
Nachfolgend eine umfangreichere Konfiguration, die Sie in jede Astro-Seite einfügen können:
---
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>Presets verwenden
Statt eine Konfiguration manuell zu erstellen, laden Sie ein Preset während der Engine-Initialisierung und referenzieren Sie es in den Optionen:
---
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 mit anderen Frameworks
Da Astro UI-Frameworks wie React, Vue, Svelte und Solid unterstützt, können Sie die frameworkspezifische tsParticles-Komponente in .astro-Dateien verwenden:
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" />Die Direktive
client:loadteilt Astro mit, die Komponente sofort beim Seitenladen zu hydrieren. Verwenden Sieclient:visiblefür verzögertes Laden.
Vollständiges Seiten-Beispiel
Eine vollständige Astro-Seite mit Partikeln als animiertem Hintergrund:
---
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="Partikel-Hintergrund">
<main>
<h1>Willkommen</h1>
<p>Diese Seite hat einen Partikel-Hintergrund, betrieben von 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>Reactive Behavior
The <Particles> component reacts to prop changes at runtime:
id,options, orurlchange → the existing container is destroyed and particles are reloaded with the new values.themechange →loadThemeis called on the existing container. This requires the optional@tsparticles/plugin-themespackage to be loaded (otherwise it is a safe no-op).
On component unmount, the particles container is automatically destroyed — no orphan animations remain.
Komponenten-Eigenschaften
| Eigenschaft | Typ | Standard | Beschreibung |
|---|---|---|---|
id | string | "tsparticles" | DOM-Element-ID für den Container |
options | ISourceOptions | {} | Vollständiges tsParticles-Konfigurationsobjekt |
url | string | — | Konfiguration von einer Remote-JSON-URL laden |
particlesClassName | string | "tsparticles-canvas-el" | CSS-Klasse für das Canvas-Element |
container | object | — | Vorhandene Container-Instanz (fortgeschritten) |
theme | string | — | Theme name (requires @tsparticles/plugin-themes; safe no-op otherwise). |
