Kit particles and simulation
The particle system turns simple birth, motion, age, and appearance rules into dynamic ASCII scenes. It supports ready-made scenes such as snow, rain, stars, fireworks, and matrix streams, while also letting a scene define its own moving population. This page is a child of Creative coding kit and connects with scene composition, drawing and fields, effects and materials, geometry, math, and images, and recipes.
Particle system model
Particle systems
The particle system is a rule set for a population of small visual elements. Each system defines where particles start, how often they appear, how they move, and how their glyph and color change during their life. A scene can use one system or several layered systems.
The public model is centered on src/…/particles.ts. A system is declarative: ranges give each particle its own value, seeds make those choices repeatable, and the particle view exposes age, position, velocity, birth data, and a stable random value to custom glyph or color functions.
References: src/…/particles.ts• L74-170 src/…/particles.ts• L172-287 examples/…/particles-embers.ts• L9-17
Time-based frames
The system evaluates a frame from the requested time rather than from the last frame. Motion uses the particle's birth time and its rules for throw, gravity, wind, drag, and sway. This lets a renderer seek to any time, draw frames out of order, and support exact loops or a still frame without keeping live simulation state.
When a period is valid, the same particle choices and timed changes recur at that period. A moving emitter, changing wind, or delayed start can prevent a loop unless its time behavior also repeats.
The frame flow moves from time to particle age, then to position and boundary state, and finally to glyph and color output:
flowchart LR
T[Requested time] --> B[Particle births]
B --> A[Age and life]
A --> M[Motion and boundaries]
M --> V[Glyph, color, and trail]
V --> F[ASCII frame]
References: src/…/particles.ts• L1-15 src/…/particles.ts• L270-284 src/…/particles.ts• L1406-1498 src/…/particles.ts• L1798-1807
Particle sources and motion
Emitters
An emitter defines the source area for births. It can be a named edge or place, a point, line, area, moving point, text picture, or marked cell in scene art. The source determines the initial position; direction and spread then determine how particles leave it.
Text emitters connect particles to existing ASCII content. A rate sends particles from the text's non-space characters. A burst can place one particle on each character, so the original word or picture can hold and then break apart.
References: src/…/particles.ts• L88-114 src/…/particles.ts• L116-125 src/…/particles.ts• L1173-1205 examples/…/particles-sand.ts• L10-14 examples/…/particles-banner.ts• L10-14
Motion and boundaries
Motion combines an initial throw with gravity, wind, drag, and optional side-to-side sway. A custom path can replace this standard motion when the scene needs an orbit, spiral, swarm, or another position rule. The path still receives the particle's time and identity so its result stays tied to the same frame model.
Boundary rules decide what happens when a particle leaves its region. Particles can disappear, wrap to the opposite side, bounce, or continue outside and return later. A floor adds landing behavior, including bounce, rest, and a short splash.
References: src/…/particles.ts• L119-125 src/…/particles.ts• L204-228 src/…/particles.ts• L246-276 src/…/particles.test.ts• L158-197 src/…/particles.test.ts• L747-764
Appearance over time
Appearance follows the particle's life. Glyph strings and color fades express a change from birth to death, while glyph and color functions can use any exposed particle value for more specific behavior. Twinkle hides particles for short time slots. Trails draw earlier positions behind the current head and can use their own glyphs and colors.
The streak helper maps velocity to a screen direction glyph. This is useful for rain, shooting stars, and other particles where the mark should show travel direction instead of age.
References: src/…/particles.ts• L229-245 src/…/particles.ts• L263-276 src/…/particles.ts• L888-892 src/…/particles.ts• L1357-1385 examples/…/particles-matrix.ts• L1-8
Scene integration
Scenery layers
Particle scenes can include static or animated art behind or in front of the particles. Text, a piece, or a surface can provide the art. Placement gives that art a meaningful position in the scene, while color and character paint preserve a visual relationship between the setting and the moving elements.
This layering makes the same particle system work as weather over a cabin, fireworks over a skyline, or sparks around a banner. Solid art can hide particles inside its covered spaces, or let them show through when the scene needs an open structure.
References: src/…/particles.ts• L300-334 src/…/particles.ts• L1609-1720 examples/…/particles-snow.ts• L10-20 examples/…/particles-fireworks.ts• L10-19
Render targets
particles() creates a normal kit piece, so the result can use the kit's usual terminal, SVG, and sampling paths. drawParticles() instead draws systems into an existing surface, which lets a larger scene combine particles with direct drawing or other kit layers.
The palette helper keeps particle fades and other scene colors in one palette. A surface without a color palette, or a monochrome surface, falls back to the normal text ink.
References: src/…/particles.ts• L1527-1551 src/…/particles.ts• L1599-1607 src/…/particles.ts• L1746-1826 examples/…/particles-comet.ts• L8-21 src/…/particles.test.ts• L484-516
Layer order
When several systems share a surface, they draw in list order. Within a system, trails appear before particle heads, and newer heads appear over older heads. This gives a scene a simple depth order without requiring a separate scene graph.
An existing piece can therefore use particles as one layer in a wider composition. The comet example places a moving particle tail with a star field, then draws the planet over both layers.
References: src/…/particles.ts• L1527-1537 src/…/particles.ts• L1495-1498 examples/…/particles-comet.ts• L16-21
Preset families
Preset catalog
Presets provide complete systems for common emergent scenes. They scale their rates and lifetimes to the requested piece size, so the same concept remains legible across grids without hand-tuning every scene.
| Preset | Scene behavior | Main controls |
|---|---|---|
snow | Layered flakes fall, sway, drift, and wrap across the sky. | Wind, gusts, density, layers, colors |
rain | Drops cross the scene at two depths and can splash on landing. | Wind, density, splash, colors |
stars | Stars appear and fade with optional shooting stars. | Density, shooting, colors |
sparks | A fountain rises, arcs, and cools as it falls. | Location, height, density, colors |
fireworks | Shells rise, burst in rings, and fade through per-shell colors. | Interval, count, rise, colors |
fireflies | Small lights drift near the ground and cycle through a glow. | Density, colors |
bubbles | Bubbles rise, sway, speed up, and grow. | Density, colors |
matrix | Falling streams use bright heads and fading trails. | Density, speed, colors |
Presets return systems as well as pieces. This means a preset can stand alone, be changed before drawing, or be combined with a custom system such as a comet tail.
References: src/…/particles.ts• L1829-1922 src/…/particles.ts• L1956-2241 site/…/kit-particles.md• L40-55 examples/…/particles-matrix.ts• L6-8
Simulation constraints
Deterministic variation
Each particle derives its personal variation from the system seed and its identity. The same seed gives the same births, ranges, and visual choices at the same time. A different seed changes the population without changing the scene rules.
This gives authors controlled variation instead of a frame-to-frame random walk. It also makes visual output stable for SVG generation, seeking, and repeat checks.
References: src/…/particles.ts• L281-284 src/…/particles.ts• L1254-1287 src/…/particles.test.ts• L441-450
Work limits
The system checks its estimated live population before drawing. A rate, burst count, or lifetime that would keep too many particles alive is rejected so a frame remains practical to render.
The check is part of scene authoring, not a runtime tuning step. Reduce the birth rate, burst count, or lifetime when a scene exceeds the limit.
References: src/…/particles.ts• L350-361 src/…/particles.ts• L800-813 src/…/particles.test.ts• L545-599
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to emitter coordinates or the grid aspect must preserve the relationship between columns, rows, and motion units.
- Changes to preset timing must keep preset motion, burst intervals, and palette changes aligned with the piece period.
- Changes to
Particlefields must preserve the inputs used by customglyph,color, andpathfunctions. - Changes to
front,back, ormarkbehavior must keep particle birth cells aligned with the displayed art. - Changes to particle colors must keep
particlesPalette()and any surrounding piece palette within the palette size limit. - Changes to
drawParticles()layering must preserve the expected order between scenery, trails, heads, and later systems.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A system has an invalid emitter, range, direction, or appearance rule. | Piece creation fails with the field that needs correction. | Keep system values within the public particle model and use system() when declaring systems separately. |
| A rate, burst count, or life keeps more than the supported live population. | The system is rejected before it can create an impractical frame. | Lower the rate, count, or life. |
| A requested period conflicts with a start time, moving emitter, or non-repeating wind. | The piece cannot promise the requested loop. | Make the time input periodic or set period: 0. |
| A color function is used without a piece palette. | Particle color selection has no palette to resolve against. | Give particles() a palette or use a color fade. |
A marked emitter is used with drawParticles() instead of particles(). | The mark cannot be found because no front or back art is available. | Put the art and marked system under particles(), or use a point emitter for an existing surface. |
Key files
| File | Why to read it |
|---|---|
| src/…/particles.ts | Defines the particle model, standalone piece creation, surface drawing, palette support, scenery, and presets. |
| site/…/kit-particles.md | Shows the public authoring model and the intended progression from presets to custom systems. |
| src/…/particles.test.ts | States the guarantees for time independence, motion, boundaries, layering, palettes, loops, and performance limits. |
| examples/…/particles-embers.ts | Shows a custom multi-system scene with source lines, motion, sway, and color aging. |
| examples/…/particles-sand.ts | Shows text breaking into particles, delayed release, landing, and settling. |
| examples/…/particles-comet.ts | Shows particles mixed with direct drawing through an existing surface. |
| examples/…/particles-fireworks.ts | Shows a preset combined with foreground scenery and painted characters. |