Particlize

ParticleSystem

The ParticleSystem class is the core component of the Particlize library. It manages particles, their rendering, coordination, and provides the main interface for particle manipulation.

Constructor

import { ParticleSystem, ParticleSystemParams } from "particlize-js";
new ParticleSystem(params: ParticleSystemParams)

Parameters

ParameterTypeDescriptionDefault
canvasHTMLCanvasElementThe HTML canvas element where particles will be rendered-
backgroundColor[number, number, number, number]Background color as RGBA values (0-1)[0, 0, 0, 1]
fboHeightnumberHeight of Frame Buffer Objects512
fboWidthnumberWidth of Frame Buffer Objects512
pluginsParticlePlugin[]Array of plugins to initialize with the system[]

Regarding Plugins

If no plugins are provided, the system will not render or update particles. This is to provide a clean slate for developers to hook into the system and implement their own properties and constraints if desired. You can use the BasicConstraintsPlugin as a starting point.

Properties

PropertyTypeDescription
uuidstringUnique identifier for the particle system
canvasHTMLCanvasElementThe rendering canvas
sceneTHREE.SceneThree.js scene object
cameraTHREE.PerspectiveCameraThree.js camera
rendererTHREE.WebGLRendererThree.js WebGL renderer
clockTHREE.ClockThree.js clock for timing
managerPropertyManagerManages particle properties and FBOs
particleCountnumberCurrent number of active particles
maxParticlesnumberMaximum number of particles (fboWidth * fboHeight)
particlesTHREE.PointsThree.js Points object containing all particles
particleGeometryTHREE.BufferGeometryGeometry for particle rendering
particleMaterialTHREE.ShaderMaterialShader material for particles
pluginsParticlePlugin[]Array of active plugins

Methods

addParticles(frame: Frame): void

Adds particles from a Frame to the particle system.

linkProperty(propertyName: string): void

Links a property from the PropertyManager to the particle shader material. The texture for the property's FBO will be addded to the shader material as a uniform. The uniform name will be the textutreName of the associated FBO.

update(): void

Manually updates the particle system for one frame.

resize(): void

Handles canvas resize events. Called automatically on window resize.

dispose(): void

Cleans up all resources and disposes of the particle system.

Performance Considerations

  • FBO Size: The fboWidth * fboHeight determines the maximum number of particles and the memory usage. Larger sizes require more GPU memory.
  • Intersection Observer: The system automatically pauses updates when the canvas is not visible
  • Plugin Performance: Heavy computations in plugins can impact frame rate

Example Usage

import { ParticleSystem, Frame, Particle } from "particlize";

// Create particle system
const canvas = document.getElementById("canvas") as HTMLCanvasElement;
const system = new ParticleSystem({
  canvas,
  backgroundColor: [0, 0, 0, 1],
  fboWidth: 512,
  fboHeight: 512,
});

// Create particles
const particles = Array.from(
  { length: 1000 },
  (_, i) =>
    new Particle({
      position: new Float32Array([Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1]),
    })
);

// Add particles to system
const frame = new Frame({ particles });
system.addParticles(frame);

// Link properties and start
system.linkProperty("position");
system.start();