Inferno 指南
目录
安装
通过 npm 安装 Inferno 封装和 tsParticles 引擎:
npm install @tsparticles/inferno tsparticles可选安装 slim 预设以获得更小的打包体积:
npm install @tsparticles/slim基本使用
@tsparticles/inferno 包导出两个项目:ParticlesProvider 和 Particles。用 ParticlesProvider 包裹粒子组件,它接受一个用于引擎设置的 init 回调,然后使用 <Particles> 渲染粒子画布。
import { render } from "inferno";
import { Particles, ParticlesProvider } from "@tsparticles/inferno";
import { loadSlim } from "@tsparticles/slim";
const options = {
fpsLimit: 60,
particles: {
number: { value: 80 },
color: { value: "#00d4ff" },
shape: { type: "circle" },
opacity: { value: 0.6 },
size: { value: { min: 2, max: 5 } },
links: {
enable: true,
distance: 150,
color: "#00d4ff",
opacity: 0.3,
width: 1,
},
move: {
enable: true,
speed: 1.5,
direction: "none",
random: true,
outModes: { default: "bounce" },
},
},
background: { color: "#0d1117" },
};
function App() {
return (
<ParticlesProvider
init={async (engine) => {
await loadSlim(engine);
}}
>
<Particles id="tsparticles" options={options} />
</ParticlesProvider>
);
}
render(<App />, document.getElementById("app"));ParticlesProvider 必须是每个 <Particles> 组件的祖先。它初始化引擎一次,并通过上下文将其提供给所有子组件。
引擎初始化
ParticlesProvider 接受一个 init 属性,该属性接收引擎实例。在这里加载应用所需的功能、形状、预设或更新器。
// 轻量级 — 圆形粒子、基本移动、连线
<ParticlesProvider init={async engine => {
const { loadSlim } = await import("@tsparticles/slim");
await loadSlim(engine);
}}>
// 完整功能集 — 所有形状、交互、效果
<ParticlesProvider init={async engine => {
const { loadFull } = await import("tsparticles");
await loadFull(engine);
}}>
// 特定预设 — 五彩纸屑、烟花、雪花、星星
<ParticlesProvider init={async engine => {
const { loadConfettiPreset } = await import("@tsparticles/preset-confetti");
await loadConfettiPreset(engine);
}}>在回调中使用动态 import() 可以实现代码分割:预设或功能模块仅在粒子组件挂载时加载。
自定义配置
以下是一个包含交互功能、多种形状类型和深色渐变背景的完整配置。
import { render } from "inferno";
import { Particles, ParticlesProvider } from "@tsparticles/inferno";
import type { ISourceOptions } from "@tsparticles/engine";
const options: ISourceOptions = {
fullScreen: { enable: true, zIndex: -1 },
fpsLimit: 60,
particles: {
number: {
value: 60,
density: { enable: true, width: 800, height: 800 },
},
color: {
value: ["#ff6b6b", "#feca57", "#48dbfb", "#ff9ff3", "#54a0ff"],
},
shape: {
type: ["circle", "triangle", "polygon"],
options: {
polygon: { sides: 6 },
},
},
opacity: { value: { min: 0.4, max: 0.8 } },
size: { value: { min: 3, max: 8 } },
links: {
enable: true,
distance: 200,
color: "#ffffff",
opacity: 0.15,
width: 1,
},
move: {
enable: true,
speed: 2,
direction: "none",
random: true,
straight: false,
outModes: { default: "out" },
},
},
interactivity: {
events: {
onHover: { enable: true, mode: "attract" },
onClick: { enable: true, mode: "repulse" },
},
modes: {
attract: { distance: 200, duration: 0.4, factor: 1 },
repulse: { distance: 200, duration: 0.4 },
},
},
background: { color: "#0f0f23" },
};
function App() {
return (
<ParticlesProvider
init={async (engine) => {
const { loadSlim } = await import("@tsparticles/slim");
await loadSlim(engine);
}}
>
<Particles id="tsparticles" options={options} />
</ParticlesProvider>
);
}
render(<App />, document.getElementById("app"));预设使用
@tsparticles/configs 包提供预构建的配置,你可以直接传递给 options 属性。在 ParticlesProvider 的 init 回调中使用相应的预设加载器。
import { render } from "inferno";
import { Particles, ParticlesProvider } from "@tsparticles/inferno";
import configs from "@tsparticles/configs";
function App() {
return (
<ParticlesProvider
init={async (engine) => {
const { loadFull } = await import("tsparticles");
await loadFull(engine);
}}
>
<Particles id="tsparticles" options={configs.confetti} />
</ParticlesProvider>
);
}
render(<App />, document.getElementById("app"));你可以将 configs.confetti 替换为任何可用的预设:configs.basic、configs.fireworks、configs.snow、configs.stars 等。
组件模式
对于较大的应用,将粒子逻辑组织到专用组件中,并使用 particlesLoaded 回调来访问 Container 实例。
import { render, Component } from "inferno";
import { Particles, ParticlesProvider } from "@tsparticles/inferno";
import type { Container, ISourceOptions } from "@tsparticles/engine";
const options: ISourceOptions = {
fpsLimit: 60,
particles: {
number: { value: 80 },
color: { value: "#00d4ff" },
shape: { type: "circle" },
opacity: { value: 0.6 },
size: { value: { min: 2, max: 5 } },
links: {
enable: true,
distance: 150,
color: "#00d4ff",
opacity: 0.3,
width: 1,
},
move: {
enable: true,
speed: 1.5,
outModes: { default: "bounce" },
},
},
interactivity: {
events: {
onHover: { enable: true, mode: "grab" },
onClick: { enable: true, mode: "push" },
},
modes: {
grab: { distance: 180, links: { opacity: 0.6 } },
push: { quantity: 3 },
},
},
background: { color: "#0a0a1a" },
};
class ParticlesBackground extends Component {
private container?: Container;
handleParticlesLoaded(container?: Container) {
this.container = container;
console.log("粒子已加载:", container?.id);
}
render() {
return (
<ParticlesProvider
init={async (engine) => {
const { loadSlim } = await import("@tsparticles/slim");
await loadSlim(engine);
}}
>
<Particles id="tsparticles" options={options} particlesLoaded={this.handleParticlesLoaded} />
</ParticlesProvider>
);
}
}
function App() {
return (
<div>
<h1 style={{ position: "relative", zIndex: 1, color: "#fff" }}>tsParticles + Inferno</h1>
<ParticlesBackground />
</div>
);
}
render(<App />, document.getElementById("app"));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.
TypeScript 示例
以下是一个完整的、带类型标注的 Inferno 应用,包含响应式粒子配置和全屏背景。
import { render } from "inferno";
import { Particles, ParticlesProvider } from "@tsparticles/inferno";
import type { Container, Engine, ISourceOptions } from "@tsparticles/engine";
const particlesOptions: ISourceOptions = {
fullScreen: { enable: true, zIndex: -1 },
fpsLimit: 60,
particles: {
number: { value: 80, density: { enable: true } },
color: { value: "#6366f1" },
shape: { type: "circle" },
opacity: { value: { min: 0.3, max: 0.7 } },
size: { value: { min: 2, max: 6 } },
links: {
enable: true,
distance: 160,
color: "#6366f1",
opacity: 0.25,
width: 1,
},
move: {
enable: true,
speed: 1.2,
direction: "none",
random: false,
straight: false,
outModes: { default: "bounce" },
},
},
interactivity: {
events: {
onHover: { enable: true, mode: "grab" },
onClick: { enable: true, mode: "push" },
},
modes: {
grab: { distance: 180, links: { opacity: 0.6 } },
push: { quantity: 3 },
},
},
background: { color: "#0a0a1a" },
};
function handleInit(engine: Engine): Promise<void> {
return import("@tsparticles/slim").then(({ loadSlim }) => loadSlim(engine));
}
function handleParticlesLoaded(container?: Container): void {
console.log("tsParticles 容器就绪:", container?.id);
}
function App() {
return (
<ParticlesProvider init={handleInit}>
<div style={{ position: "relative", zIndex: 1, color: "#fff", textAlign: "center", paddingTop: "2rem" }}>
<h1>tsParticles + Inferno</h1>
<p>完整 TypeScript 集成</p>
</div>
<Particles id="tsparticles" options={particlesOptions} particlesLoaded={handleParticlesLoaded} />
</ParticlesProvider>
);
}
render(<App />, document.getElementById("app"));你现在已拥有将 tsParticles 集成到 Inferno 应用所需的全部内容。每个示例都是独立的,可直接复制到你的项目中。
