Astro 集成
在你的 Astro 站点中使用官方 @tsparticles/astro 集成包来使用 tsParticles。
安装
通过包管理器安装 Astro 集成和 tsParticles 核心:
npm install @tsparticles/astro tsparticlespnpm add @tsparticles/astro tsparticlesyarn add @tsparticles/astro tsparticles引擎初始化
tsParticles 采用模块化架构。在渲染粒子之前,你需要用所需功能初始化引擎。创建一个客户端脚本(例如 src/scripts/particles-init.ts)或在 Astro 组件中使用内联 <script>:
import { initParticlesEngine } from "@tsparticles/astro";
void initParticlesEngine(async (engine) => {
const { loadFull } = await import("tsparticles");
await loadFull(engine);
});
initParticlesEngine是tsParticles.init()的封装,确保在<Particles>组件挂载前引擎已就绪。它返回一个Promise,初始化完成后解析。
基本使用
在任何 .astro 模板中放置 <Particles /> 组件。通过 options 属性传入配置:
---
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>
id属性会传递给底层画布容器<div>。可将其用于样式设置或通过document.getElementById()进行命令式访问。
TypeScript 支持
该集成包附带完整的 TypeScript 声明。使用 @tsparticles/engine 中的 ISourceOptions 对配置进行类型化:
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 },
},
},
};自定义配置
以下是一个更精细的配置,可直接放入任何 Astro 页面:
---
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>使用预设
与其手动构建配置,不如在引擎初始化时加载预设并在选项中引用:
---
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>与其他框架集成
由于 Astro 支持 React、Vue、Svelte 和 Solid 等 UI 框架,你可以在 .astro 文件中使用特定框架的 tsParticles 组件:
React 在 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 在 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" />
client:load指令告诉 Astro 在页面加载时立即水合组件。使用client:visible可延迟加载。
完整页面示例
一个完整的 Astro 页面,粒子作为动画背景:
---
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="粒子背景">
<main>
<h1>欢迎</h1>
<p>此页面拥有由 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.
组件属性
| 属性 | 类型 | 默认值 | 描述 |
|---|---|---|---|
id | string | "tsparticles" | 容器的 DOM 元素 ID |
options | ISourceOptions | {} | 完整的 tsParticles 配置对象 |
url | string | — | 从远程 JSON URL 加载配置 |
particlesClassName | string | "tsparticles-canvas-el" | 画布元素的 CSS 类名 |
container | object | — | 预先存在的 Container 实例(高级用法) |
theme | string | — | Theme name (requires @tsparticles/plugin-themes; safe no-op otherwise). |
