Introducing a new way to review your PRs.

Read the announcement

bas3line / ascii

Animated ascii art for web pages, in TypeScript: React, Next.js, Astro, or one HTML tag

View on GitHub

21 documents · Updated Oct 26, 2026

docs / kit-scene-composition

Kit scene composition

The kit turns a frame into a reusable piece. A piece has a fixed cell size, time-based drawing, display metadata, and an optional colour palette. Core drawing builds the scene state in a grid, while composition combines pieces into new pieces that can play in the same browser, terminal, or SVG output as any other piece. See the kit overview for the wider set of kit parts.

Piece contract

The piece contract is the boundary between scene creation and playback. A piece describes what it is through meta and produces a frame from a time value through default(options). This lets the kit and its consumers treat a hand-built drawing, a 3D scene, a particle system, and a composed layout in the same way.

References: src/types.ts• L26-71 src/…/core.ts• L963-1003

Piece metadata

Metadata defines the scene's identity and playback shape. It gives the piece a name, category, note, cell dimensions, frame rate, options, and any loop or colour information needed by renderers. The kit checks this contract when the piece is made, so invalid size, timing, palette, or option data fails before playback.

References: src/types.ts• L26-51 src/…/core.ts• L771-795 src/…/core.ts• L1041-1066

Frame drawing

The drawing function receives elapsed time, a Surface, and a small context that carries resolved options, page theme, colour mode, and size. Each frame normally depends on time and options only. The kit creates or clears the surface for a play, runs the drawing, and converts the cell state into the frame consumed by a renderer.

References: src/…/core.ts• L1024-1039 src/…/core.ts• L1069-1104

The piece() builder is the main entry point for custom scene state. It supports both direct frame drawing and setup work that runs once when playback starts. Use the setup form when a scene needs stable state for the duration of one play.

References: src/…/core.ts• L1078-1104

Surface state

Surface is the kit's scene grid. It stores one character and one colour index per cell, uses the top-left cell as coordinate origin, and clips writes outside its bounds. Drawing systems use it as the shared target, while composition uses it as the source and destination for layer operations.

References: src/…/core.ts• L537-570 src/…/core.ts• L585-607

Cell semantics

The grid distinguishes an empty cell from a visible space. Empty cells are transparent when a frame is placed over another frame. A visible space covers the cell below it. This distinction is the basis for readable text over a scene and for cards that intentionally hide their background.

References: src/…/core.ts• L539-542 src/…/core.ts• L609-620 src/…/core.ts• L680-699

The surface supports the basic state operations used by higher-level drawing APIs:

  • set() and put() write one cell.
  • write() writes text across cells.
  • fill() and clear() change a region or the full grid.
  • paste() places one surface over another with transparency and colour mapping.
  • load() imports text and optional cell colours.
  • frame() returns the final text and updates colour output for a themed renderer.

References: src/…/core.ts• L609-723 src/…/core.ts• L725-765

Colour state

A piece may draw in one ink or use a palette. A surface keeps palette indices with its characters, then maps those indices to the current light or dark page when it produces a frame. Composition merges child palettes into the parent palette and maps each child's colour indices into that shared space.

References: src/…/core.ts• L403-534 src/…/core.ts• L725-743 src/…/compose.ts• L198-215

Composition model

Composition treats a piece, a text block, or a surface as a source. A source can be made into a still piece, placed in a clip with its own options and time, then combined with other sources. The result is a normal chainable piece, so a composed scene can be composed again or sent to any player.

References: src/…/core.ts• L1106-1135 src/…/compose.ts• L59-82 src/…/core.ts• L864-867

Layer placement

layer() builds a stage from a base source and one or more overlays. Each overlay has an anchor, margin, optional offset, speed, colour, mask, and position. over() is the focused two-source form: it puts the first source over the second and centres the top source by default. Transparent cells reveal lower layers, while a null mask makes the overlay cover its full box.

References: src/…/compose.ts• L54-120 src/…/compose.ts• L675-703

The composition flow moves from independent sources to one shared frame. This diagram shows how each child frame is timed, positioned, colour-mapped, and pasted into the stage.

flowchart LR
    A[Piece, text, or surface] --> B[Clip settings]
    B --> C[Child frame at local time]
    C --> D[Anchor and position]
    D --> E[Mask and halo]
    E --> F[Palette mapping]
    F --> G[Shared Surface]
    G --> H[Composed piece frame]

Layer placement also handles moving overlays. A declared move gives the overlay a crossing period, while a position function lets the caller steer it over time. A moving or steered layer changes the parent playback requirements, so the composed piece receives a suitable frame rate and may lose a known loop when its motion cannot be wrapped safely.

References: src/…/compose.ts• L89-120 src/…/compose.ts• L620-671

Layout arrangement

row(), column(), and grid() arrange sources into fixed regions. They preserve each part's own playback and options while the parent decides the overall cell placement. Borders add a visual frame around a whole piece or around grid cells, with optional titles and padding.

References: src/…/compose.ts• L705-815 src/…/compose.ts• L817-839

These layouts serve different scene relationships:

CompositionScene relationship
row()Independent pieces share one horizontal line.
column()Independent pieces share one vertical stack.
grid()Pieces occupy a repeated cell arrangement.
border()One source gains an enclosing frame and optional title.

References: src/…/compose.ts• L705-815 src/…/index.ts• L399-427

Sequence playback

sequence() treats sources as steps in one timed presentation. Each step owns a duration, and the parent changes from one step to the next with a dissolve, fade, wipe, or cut. The sequence can loop back to its first step, which makes it useful for a rotating set of scenes rather than a simultaneous layout.

References: src/…/compose.ts• L120-132 src/…/index.ts• L399-427

Shape and time transforms

Composition can change a source without changing its subject. Cropping selects a region, padding adds space, scaling repeats cells, and flipping mirrors the frame. Delay, speed, repeat, and freeze change when the source appears or whether it remains still. These operations return new pieces, so each transform can feed the next composition step.

References: src/…/compose.ts• L120-138 src/…/core.ts• L805-831

Scene playback

The parent piece coordinates child time, frame size, colour maps, and loop information. A child may receive its own options, time offset, speed, or tint through a clip. The parent then renders each child into the shared surface using the child's local playback state.

References: src/…/compose.ts• L59-82 src/…/compose.ts• L641-671

Loop coordination

Composition combines child periods when they can meet within the kit's loop limit. An explicit parent loop can provide a boundary for parts that do not naturally meet. A negative offset delays a child before its local time begins, and a position function prevents the kit from claiming a fixed loop unless the caller wraps it.

References: src/…/compose.ts• L69-75 src/…/compose.ts• L631-655

Size and cell shape

The parent stage has a fixed cell size. When a character-cell source is placed on a square-cell scene, or the reverse, the composition system adjusts its row fit so the source keeps its visual shape. A still source may therefore occupy a different number of rows in the parent than in its own piece metadata.

References: src/…/compose.ts• L206-214 src/…/compose.ts• L675-683

3D scene integration

The 3D scene API uses the same surface and piece contract as flat drawings. render3d() draws the nearest visible shape into an existing surface, so text or other drawing can share the frame. scene() wraps that work as a piece, while scenePalette() supplies the colours needed when a custom piece combines 3D forms with other layers.

This makes a 3D scene another composition source rather than a separate output type. For related 3D, field, image, and drawing features, see drawing and fields and geometry, math, and images.

References: src/…/shapes3d.ts• L2103-2122 src/…/shapes3d.ts• L2161-2187 src/…/shapes3d.ts• L2190-2249

Chainable composition

Kit pieces expose composition and playback changes as chain methods. Each method returns a new piece and leaves the source unchanged, which lets a scene read as a left-to-right pipeline. Library pieces can join this model through chained(), and text or surfaces can enter it through pieceOf() or asPiece().

References: src/…/core.ts• L797-867 src/…/core.ts• L951-960 src/…/core.ts• L1111-1135

The chain is the common link between composition and the other kit systems. A composed scene can receive an effect, a particle layer, or a material-driven picture without changing how the final piece is played. See effects and materials, particles and simulation, and recipes.

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the Piece contract aligned with KitPiece, because players accept the plain meta and default shape while chain methods extend it.
  • When changing Surface cell or colour semantics, check paste(), load(), frame(), and every composition path that maps child palettes.
  • When changing clip timing or layer movement, recheck parent frame rate, loop discovery, and the behavior of explicit loop options together.
  • When changing cell aspect rules, verify both character-cell overlays on 3D scenes and square-cell scenes placed beside text.
  • When changing composition exports, keep the public names in src/…/index.ts aligned with the compose API and its chain methods.

Important failure modes

TriggerConsequenceGuard
A composed palette exceeds the piece colour limit.The parent piece cannot represent all child colours.Reduce child colours or use fewer shades before composition.
A still text source is larger than its stage.Its edges would be cut without warning from a normal crop.Make the source smaller, crop it intentionally, or use a larger stage.
A position function controls a moving layer.The parent has no known loop for SVG playback or other loop-based output.Wrap the source with repeat() or provide a deliberate parent loop.
A child uses a negative time offset.The parent no longer has a simple repeating period for that child.Use offsets only when the altered start time is part of the intended scene.
A character outside the supported cell range is written to a surface.The frame cannot hold the character as one cell.Use one printable Basic Multilingual Plane character per cell.

Key files

FileWhy to read it
src/types.tsDefines the plain piece contract shared by all players.
src/…/core.tsDefines Surface, piece(), metadata, palettes, and chainable pieces.
src/…/compose.tsDefines layers, layouts, borders, sequences, transforms, timing, and palette merging.
src/…/shapes3d.tsShows how 3D rendering writes into the same surface and piece model.
src/…/index.tsDefines the public kit exports for core and composition APIs.
examples/…/compose-banner-scene.tsShows a compact banner-over-scene composition using the public API.