Skip to content

Particles ​

Particle generator optimized to display thousands of instances.

Particles example

Examples ​

Manual generation ​

js
import { Particles, Star, Math as M } from "pencil.js";

const count = 5000;
const position = [100, 200];
const base = new Star(undefined, 5, 20);
// Additional parameters after the first one come from the generate method call
const generator = (index, any, additional, params) => {
    // Particles data
    return {
        position: scene.getRandomPosition(),
        rotation: M.random(),
        scale: M.random(0.5, 1.5),
        ttl: 100,
        // And whatever value you want to define for each individual particle, for example
        speed: new Position(1, 0).rotate(M.random()),
    };
};
const updater = (data, index) => {
    data.position.add(data.speed);
};

const particles = new Particles(position, base, generator, updater);
// Any parameters after the first one are passed to the generator function
particles.generate(count, any, additional, params);

Automatic generation ​

js
import { Particles, Star, Math as M } from "pencil.js";

const count = 5000;
const position = [100, 200];
const base = new Star(undefined, 5, 20);
// Additional parameters after the first one come from the generate method call
const generator = (index, any, additional, params) => {
    // Particles data
    return {
        position: scene.cursorPosition,
        rotation: M.random(),
        scale: M.random(0.5, 1.5),
        ttl: 100,
        // And whatever value you want to define for each individual particle, for example
        speed: new Position(1, 0).rotate(M.random()),
    };
};
const updater = (data, index) => {
    data.position.add(data.speed);
};

const particles = new Particles(position, base, generator, updater, {
    emit: [10, 20], // Between 10 and 20 particles
    frequency: 0.5, // about every 2 frames
    args: [any, additional, params], // parameters for the generator function
});

ParticlesOptions ​

Inherit from ComponentOptions.

NameTypeDefaultComment
frequencyNumber1Frequency of emission per frame (randomized, 1 mean every frame, 0.1 mean about 1 each 10 frames)
emitNumber or Array<Number>Number or range of number of particles emitted
argsArrayArguments passed to the generator and updater functions

ParticlesData ​

ParticlesData stores individuals information about each particle. It supports a limited number of properties.

NameTypeDefaultComment
positionPositionnew Position()Position of the particle
rotationNumber0Rotation applied to the particle
scaleNumber or Array1Scaling ratio or a pair of value for horizontal and vertical scaling
ttlNumberundefinedTime to live, number of frames the particle is displayed. This number will be decremented and the data removed when it reach 0. Any falsy value will prevent this behavior

The data object can also be used to store any other individual values for each particle.

Released under the MIT License.