@flare-engine/particles

中文文档
0.4.12发布于 2026/11/13Apache-2.0TypeScriptESM + CJS构建来源已验证近 30 天下载 2236

Particle emitter, pool, immediate-mode Skia rendering

原始使用文档
文档目录5 节
  1. Install
  2. Usage
  3. Emitter shapes
  4. Drawing into a sprite batch
  5. License

@flare-engine/particles

Pre-allocated particle pool + emitter — particles are recycled rather than reallocated, so a steady-state emitter does not allocate per particle per frame. Target: 200–500 alive particles at 60 fps. This package owns particle data and lifecycle; rendering happens via the host's immediate-mode Skia layer (PictureRecorder).

Measured integrate cost per frame is published in benchmarks, with the capture date and run-to-run spread on every row. Allocation is instrumented, not assumed: the draw-path counter suite reads ParticlePool.created growth per frame and bun run check:perf fails the build if a steady-state emitter calls the factory even once. The exact integers are published in the same document.

Install

bun add @flare-engine/particles

Usage

import { ParticleEmitter } from '@flare-engine/particles';

const emitter = new ParticleEmitter({
  rate: 30, lifetime: 1.2,
  speed: 80, speedMax: 160,
  direction: -Math.PI / 2, spread: Math.PI / 4,
  startColor: { r: 1, g: 1, b: 1, a: 1 }, endColor: { r: 1, g: 1, b: 1, a: 1 },
  startAlpha: 1, endAlpha: 0, // opacity fades via start/endAlpha — Color.a is not read
  startSize: 4, endSize: 0,
  gravityY: 98,
});

// gravity is part of config; update takes only dt
emitter.update(1 / 60);

Emitter shapes

By default every particle spawns at the emitter's x/y. A shape samples a spawn offset instead — point (the default), circle, ring, rect, or cone:

import { ParticleEmitter } from '@flare-engine/particles';

// An explosion: spawn on a ring, travel outward from the centre.
const blast = new ParticleEmitter({
  rate: 0, lifetime: 0.6,
  speed: 120, speedMax: 200,
  direction: 0, spread: 0.15,
  shape: { kind: 'ring', radius: 24, radial: true },
});
blast.x = 160;
blast.y = 120;
blast.burst(48);

Shape controls position; direction and spread still control velocity. They compose rather than compete. A shape with a meaningful outward normal — circle, ring, cone — can hand velocity over to it with radial: true, which is what separates a ring of drifting particles from an explosion. Without it, a ring spawns on the ring and everything still flies along direction.

ShapePositionNotes
{ kind: 'point' }the emitter originDefault. Draws no random values, so an emitter without a shape produces the identical sequence to one before shapes existed.
{ kind: 'circle', radius, radial? }uniform inside the discUniform by area — the radial term is sqrt-weighted, so particles do not bunch at the centre.
{ kind: 'ring', radius, thickness?, radial? }on the ring, thickness widethickness defaults to 0 (a bare ring), spread uniformly across the band.
{ kind: 'rect', width, height }uniform in the rectCentred on the emitter, not anchored at a corner. No outward normal, so radial does not apply.
{ kind: 'cone', radius, angle }uniform in a wedge of half-angle angleOriented along direction. A wedge of spawn points — distinct from spread, which fans velocity out of a single point.

sampleEmitterShape(shape, rand, out, direction?) is exported if you want to place something other than particles. It writes into a caller-owned ShapeSample (createShapeSample()) and allocates nothing, so it is safe per-particle in a steady-state loop. Every shape draws a fixed number of values from rand, which is what keeps a seeded emitter replaying exactly.

Drawing into a sprite batch

drawParticles queues every live particle into a batch you own, so particles flush together with the rest of the scene rather than forcing a second pass:

import { drawParticles, ParticleEmitter } from '@flare-engine/particles';
import type { ParticleBatchLike, ParticleFrameLike } from '@flare-engine/particles';

declare const emitter: ParticleEmitter;
declare const batch: ParticleBatchLike; // a @flare-engine/render SpriteBatch satisfies this
declare const atlasImage: unknown;
declare const frame: ParticleFrameLike; // e.g. a SpriteFrame from @flare-engine/assets

// each frame, before the batch flushes
drawParticles(emitter.pool, batch, atlasImage, frame);

The batch and frame are typed structurally, so this package still depends on nothing but @flare-engine/core and @flare-engine/math and stays usable with no renderer. A real SpriteBatch satisfies ParticleBatchLike — asserted by a runtime test in @flare-engine/engine, not merely assumed.

The loop allocates nothing per particle, and skips any particle whose interpolated size or alpha has reached zero rather than paying for an invisible draw call.

Colour is not applied per particle. The pool interpolates startR/G/B → endR/G/B, but a batch retains an effect reference until it flushes, so a single reusable effect object would give every particle the last one's colour — and one object per particle per frame is exactly the allocation this package exists to avoid. Alpha is applied, because drawRaw takes it as a number. If you need per-particle colour today, write the loop yourself over pool.particles and accept the cost.

See API spec.

License

Apache-2.0

版本与发布记录

@flare-engine/particles 共发布 17 个版本,首次发布于 2026/06/10;过去 12 个月发布了 17 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.4.12latest2026/11/133 天前
0.4.112026/11/123 天前
0.4.102026/11/087 天前
0.4.82026/11/0610 天前
0.4.72026/11/0411 天前
0.4.62026/10/3115 天前

关于 @flare-engine/particles 的常见问题

@flare-engine/particles 是什么?

Particle emitter, pool, immediate-mode Skia rendering

如何安装 @flare-engine/particles?

在项目目录运行 npm install @flare-engine/particles,或使用 pnpm add / yarn add / bun add。

@flare-engine/particles 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@flare-engine/particles 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@flare-engine/particles 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@flare-engine/particles 可以商用吗?

@flare-engine/particles 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@flare-engine/particles 的下载量有多少?

根据 npm 官方下载统计,@flare-engine/particles 在 2026/10/15 – 2026/11/13 这 30 天内被下载 2,236 次,日均约 75 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@flare-engine/particles 还在维护吗?

最新版本 0.4.12 发布于 2026/11/13(3 天前);至今共发布 17 个版本,其中过去 12 个月发布了 17 个;首次发布于 2026/06/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。