Skip to content

Primitives API

Low-level particle and trail classes used internally by the simulations. Import them directly to build fully custom effects.


Trail

A glowing line that travels from a start point to an end point. Sets isDone on arrival — no event system, no spark emission.

Constructor

typescript
new Trail(start
:
Point, end
:
Point, config ? : TrailConfig
)
ParameterTypeDescription
startPointLaunch position in pixels.
endPointTarget position in pixels.
configTrailConfigOptional appearance and physics settings.

Properties

PropertyTypeDescription
huenumberColor hue 0–360.
isDonebooleantrue when the end point has been reached.
positionPointCurrent pixel position (read-only copy).

Methods

tick(): void

Advances the trail by one frame. Sets isDone when the target is reached.

draw(ctx): void

Renders the glowing trail and head. Does nothing when isDone.


TrailConfig

typescript
interface TrailConfig {
    acceleration?: number;
    brightness?: number;
    glow?: number;
    hue?: number;
    length?: number;
    speed?: number;
    width?: number;
}
PropertyTypeDefaultDescription
accelerationnumber1.05Speed multiplier per tick. 1 = constant speed, > 1 = accelerates.
brightnessnumber65HSL lightness % for the trail color.
glownumber10Shadow blur for the glow effect.
huenumberrandomColor hue 0–360.
lengthnumber6Number of trail history positions. Longer = more visible tail.
speednumber1Starting speed in pixels/tick.
widthnumber2Stroke width at the head in pixels.

SparklerParticle

A glowing spark with a circular trail. The individual particle used by Sparklers.

Constructor

typescript
new SparklerParticle(
    position
:
Point,
    velocity
:
Point,
    color
:
[number, number, number],
    config ? : SparklerParticleConfig
)
ParameterTypeDescription
positionPointStarting position in pixels.
velocityPointInitial velocity { x: vx, y: vy } in pixels/tick.
color[number, number, number]RGB color tuple, values 0–255.
configSparklerParticleConfigOptional physics settings.

Properties

PropertyTypeDescription
isDeadbooleantrue when fully faded out.
positionPointCurrent pixel position.

Methods

tick(dt?: number): void

Advances physics by dt frames (default 1). Applies friction, gravity, and trail.

draw(ctx): void

Renders the trail circles and main spark circle. Use ctx.globalCompositeOperation = 'lighter' for additive glow.


SparklerParticleConfig

typescript
interface SparklerParticleConfig {
    decay?: number;
    friction?: number;
    gravity?: number;
    scale?: number;
    size?: number;
    trailLength?: number;
}
PropertyTypeDefaultDescription
decaynumberrandom 0.02–0.05Alpha decrease per tick.
frictionnumber0.96Velocity multiplier per tick.
gravitynumber0.8Pixels added to Y velocity per tick.
scalenumber1Size and gravity multiplier.
sizenumberrandom 1–3Base radius in pixels.
trailLengthnumber3Number of trail positions.

ConfettiParticle

A single confetti piece with physics (velocity, gravity, swing, rotation, flip) and one of 11 shapes.

Constructor

typescript
new ConfettiParticle(
    position
:
Point,
    direction
:
number,
    shape
:
ConfettiShape,
    color
:
string,
    config ? : ConfettiParticleConfig
)
ParameterTypeDescription
positionPointStarting position in pixels.
directionnumberLaunch angle in degrees. 90 = straight up, 0 = right.
shapeConfettiShapeVisual shape to render.
colorstringAny CSS color string ('#ff4466', 'rgb(255,0,0)', etc.).
configConfettiParticleConfigOptional physics settings.

Properties

PropertyTypeDescription
isDeadbooleantrue when the particle has lived its full ticks lifetime.
positionPointCurrent pixel position.

Methods

tick(dt?: number): void

Advances all physics by dt frames (default 1). Updates velocity, swing, rotation, and flip.

draw(ctx): void

Renders the shape at the current position with transform and alpha. Saves and restores context state.


ConfettiParticleConfig

typescript
interface ConfettiParticleConfig {
    decay?: number;
    gravity?: number;
    scale?: number;
    spread?: number;
    startVelocity?: number;
    ticks?: number;
}
PropertyTypeDefaultDescription
decaynumber0.9Velocity multiplier per tick. Lower = faster deceleration.
gravitynumber1Downward acceleration.
scalenumber1Multiplies startVelocity, gravity, and particle size.
spreadnumber45Random angular spread in degrees around the launch direction.
startVelocitynumber45Initial speed in pixels/tick.
ticksnumber200Lifetime in frames before isDead becomes true.

ConfettiShape

typescript
type ConfettiShape =
    | 'bowtie' | 'circle' | 'crescent' | 'diamond' | 'heart'
    | 'hexagon' | 'ribbon' | 'ring' | 'square' | 'star' | 'triangle';

SHAPE_PATHS

All 11 confetti shapes as normalized Path2D objects. Coordinates span roughly [-1, 1] — apply setTransform to scale, rotate, and position them.

typescript
const SHAPE_PATHS: Record<ConfettiShape, Path2D>
typescript
import { SHAPE_PATHS } from '@basmilius/sparkle';

ctx.save();
ctx.setTransform(size, 0, 0, size, x, y);
ctx.fillStyle = color;
ctx.fill(SHAPE_PATHS['star']);
ctx.restore();

BalloonParticle

A floating balloon with gradient body, gloss highlight, knot, and swaying string. Rises upward until isDone.

Constructor

typescript
new BalloonParticle(position
:
Point, color
:
[number, number, number], config ? : BalloonParticleConfig
)
ParameterTypeDescription
positionPointStarting position in pixels.
color[number, number, number]RGB color tuple, values 0–255.
configBalloonParticleConfigOptional appearance and physics settings.

Properties

PropertyTypeDescription
isDonebooleantrue when the balloon has risen above the canvas.
positionPointCurrent pixel position.

Methods

tick(dt?: number): void

Advances physics: rises, drifts, and rotates.

draw(ctx): void

Renders the full balloon — gradient body, gloss, knot, and animated string.


BalloonParticleConfig

typescript
interface BalloonParticleConfig {
    driftAmp?: number;
    driftFreq?: number;
    driftPhase?: number;
    radiusX?: number;
    radiusY?: number;
    riseSpeed?: number;
    rotationSpeed?: number;
    scale?: number;
    stringLength?: number;
}
PropertyTypeDefaultDescription
driftAmpnumberrandom 0.3–1Max horizontal drift in pixels/tick.
driftFreqnumberrandom 0.5–1.5Drift oscillation frequency.
driftPhasenumberrandomStarting phase of the drift sine.
radiusXnumberrandom 25–45 × scaleHorizontal radius in pixels.
radiusYnumberradiusX / 0.85Vertical radius in pixels.
riseSpeednumberrandom 0.5–1.3Pixels per tick upward.
rotationSpeednumberrandom 0.5–2Rotation oscillation speed.
scalenumber1Multiplies all size values.
stringLengthnumberrandom 30–70 × scaleString length in pixels.

RaindropParticle

A line-rendered raindrop that moves along its velocity vector. isDead when it reaches groundY.

Constructor

typescript
new RaindropParticle(position
:
Point, velocity
:
Point, color
:
[number, number, number], config ? : RaindropParticleConfig
)
ParameterTypeDescription
positionPointStarting position in pixels.
velocityPointVelocity { x: vx, y: vy } in pixels/tick.
color[number, number, number]RGB color tuple, values 0–255.
configRaindropParticleConfigOptional physics settings.

Properties

PropertyTypeDescription
isDeadbooleantrue when position.y >= groundY.
positionPointCurrent pixel position.

Methods

tick(dt?: number): void

Moves the drop along its velocity.

draw(ctx): void

Renders a short line along the velocity direction with depth-based opacity and width.


RaindropParticleConfig

typescript
interface RaindropParticleConfig {
    depth?: number;
    groundY?: number;
    length?: number;
    scale?: number;
}
PropertyTypeDefaultDescription
depthnumberrandom 0.3–1Affects opacity and line width. 1 = full, 0.3 = dim/thin.
groundYnumberInfinityY position (pixels) at which isDead becomes true.
lengthnumberrandom 8–23 × scaleTail length in pixels.
scalenumber1Size multiplier.

SplashParticle

A small circular particle that arcs upward and fades out. Spawned when a raindrop lands.

Constructor

typescript
new SplashParticle(position
:
Point, velocity
:
Point, color
:
[number, number, number], config ? : SplashParticleConfig
)

Static methods

SplashParticle.burst(position, color, config?): SplashParticle[]

Creates 2–4 splash particles at the given position with randomized upward velocities.

Properties

PropertyTypeDescription
isDeadbooleantrue when fully faded out.
positionPointCurrent pixel position.

Methods

tick(dt?: number): void

Applies gravity and moves the particle. Decrements alpha.

draw(ctx): void

Renders a small circle.


SplashParticleConfig

typescript
interface SplashParticleConfig {
    gravity?: number;
    scale?: number;
    size?: number;
}
PropertyTypeDefaultDescription
gravitynumber0.15Downward acceleration per tick.
scalenumber1Size multiplier.
sizenumberrandom 1–3Radius in pixels.

FireflyParticle

A softly pulsing glow dot with organic Lissajous-pattern drift. Wraps around canvas edges. Requires a pre-created sprite from createFireflySprite().

Constructor

typescript
new FireflyParticle(x
:
number, y
:
number, bounds
:
{
    width: number;
    height: number
}
,
sprite: HTMLCanvasElement, config ? : FireflyParticleConfig
)
ParameterTypeDescription
xnumberStarting X position in pixels.
ynumberStarting Y position in pixels.
bounds{ width, height }Canvas dimensions for wrap-around.
spriteHTMLCanvasElementGlow sprite from createFireflySprite().
configFireflyParticleConfigOptional settings.

Properties

PropertyTypeDescription
position{ x, y }Current pixel position.

Methods

tick(dt?: number): void

Advances the Lissajous drift and wraps around bounds.

draw(ctx): void

Draws the pulsing glow sprite. Does not set composite operation — set ctx.globalCompositeOperation = 'lighter' before calling.


FireflyParticleConfig

typescript
interface FireflyParticleConfig {
    glowSpeed?: number;
    scale?: number;
    size?: number;
    speed?: number;
}
PropertyTypeDefaultDescription
glowSpeednumberrandom 0.5–2Pulsing speed multiplier.
scalenumber1Multiplied into size.
sizenumber6 × scaleDisplay radius in pixels.
speednumber1Movement speed multiplier.

createFireflySprite

Creates a radial-gradient glow canvas for use with FireflyParticle. Create once per color and reuse across many particles.

typescript
function createFireflySprite(color: string, size?: number): HTMLCanvasElement
ParameterTypeDefaultDescription
colorstringAny CSS color string ('#b4ff6a', 'rgb(...)', etc.).
sizenumber64Canvas size in pixels.

ShootingStarSystem

A self-contained system that spawns and animates shooting stars at random intervals. Already standalone — no extraction needed.

typescript
import { ShootingStarSystem } from '@basmilius/sparkle';

const system = new ShootingStarSystem(
    {interval: [60, 180], color: [200, 230, 255]},
    Math.random  // or any () => number RNG
);

// In your loop:
system.tick(dt, canvas.width, canvas.height);
system.draw(ctx);

See ShootingStarSystemConfig for the full list of options.


Explosion

A single firework burst particle. See Fireworks API for FireworkVariant details.

Constructor

typescript
new Explosion(
    position
:
Point,
    hue
:
number,
    lineWidth
:
number,
    type
:
ExplosionType,
    scale ? : number,
    angle ? : number,
    speed ? : number,
    vz ? : number
)
ParameterTypeDefaultDescription
positionPointStarting position in pixels.
huenumberBase hue 0–360. Varied by hueVariation from config.
lineWidthnumberStroke width before config scale is applied.
typeExplosionTypeDetermines physics and visual config.
scalenumber1Multiplies speed and vz.
anglenumberrandomLaunch angle in radians.
speednumberrandomInitial speed in pixels/tick.
vznumberrandom if spread3dDepth velocity for 3D perspective.

Properties

PropertyTypeDescription
anglenumberLaunch angle in radians.
huenumberActual hue after variation.
isDeadbooleantrue when fully faded out.
positionPointCurrent position (mutable, updated each tick).
typeExplosionTypeThe type this particle was created with.

Methods

tick(): void

Advances physics by one frame.

draw(ctx): void

Renders the trail and head shape.

checkSplit(): boolean

Returns true once when a crossette particle's alpha drops below 0.5.

checkCrackle(): boolean

Returns true once when a crackle particle is nearly dead.


Firework

A rising projectile. Travels from start to target, dispatches 'remove' on arrival.

Constructor

typescript
new Firework(start
:
Point, target
:
Point, hue
:
number, tailWidth
:
number, baseSize
:
number
)

Properties

PropertyTypeDescription
huenumberTrail hue.
positionPointCurrent pixel position.

Methods

tick(): void

Advances movement. Dispatches 'remove' at target.

draw(ctx): void

Renders trail and glowing head.

collectSparks(): Spark[]

Returns and clears sparks emitted this frame.

Events

EventWhen
'remove'Target reached.

Spark

A tiny physics particle used for trail sparks and crackle effects.

Constructor

typescript
new Spark(position
:
Point, hue
:
number, velocityX ? : number, velocityY ? : number
)

Properties

PropertyTypeDescription
isDeadbooleantrue when fully faded out.
positionPointCurrent pixel position.

Methods

tick(): void

Applies friction, gravity, moves spark, decrements alpha.

draw(ctx): void

Renders a small circle.


EXPLOSION_CONFIGS

typescript
const EXPLOSION_CONFIGS: Record<ExplosionType, ExplosionConfig>

Config map for all 14 base explosion types. Use to read particle counts and speed ranges when spawning manually.


ExplosionType

typescript
type ExplosionType =
    | 'peony' | 'chrysanthemum' | 'willow' | 'ring'
    | 'palm' | 'crackle' | 'crossette' | 'dahlia'
    | 'brocade' | 'horsetail' | 'strobe' | 'heart'
    | 'spiral' | 'flower';