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 / public-rendering-api

Public rendering API

The public rendering API turns an ASCII piece into a still or moving picture for a browser, terminal, document, or framework. Callers choose a piece, apply its options, and select the output surface that fits the host environment.

Rendering model

Piece contract

A piece is the common input to every rendering target. It provides metadata for its size, motion, color support, and accessibility name, plus a function that returns one frame of ASCII text at a given time (src/types.ts• L26-71). This shared contract lets the same artwork move between a page, a terminal, an SVG, and framework adapters without changing the artwork itself.

References: src/types.ts• L26-71 src/banner.ts• L319-327

Rendering targets

The package keeps the picture model separate from the place that displays it. Choose a target by the host and by whether the result must remain live, become a string, or stay in terminal output.

TargetPrimary surfaceResult
Browser elementmount(element, piece, options?)A live <pre> or <canvas>
ReactAscii or Banner from ascii.rest/reactA managed browser element
Custom elementascii.rest/element<ascii-art> or <ascii-banner>
Astroascii.rest/astroAn Astro component with an initial server frame
Terminalplay or banner from ascii.rest/terminalLive terminal output or scrollback text
SVG or filesvg or bannerSvg from ascii.rest/svgAn SVG string

See Terminal and output rendering for output-specific behavior and Framework rendering adapters for framework use.

References: package.json• L18-60 README.md• L98-145 README.md• L203-255

Public package surface

Core package exports

The root package, ascii.rest, is the low-level browser-facing API. It exports mount for playback, the piece library helpers for discovery and loading, and the types that describe the shared piece contract (src/index.ts• L4-11). Use this surface when the caller owns the element and its lifecycle.

The library supports both direct imports and named loading. A direct piece import bundles that artwork with the caller. A name passed to load can be fetched on demand, while names, canvas, and isPiece help callers build safe selection and target logic (src/library.ts• L5-6, src/library.ts• L224-235).

References: src/index.ts• L4-11 src/library.ts• L5-6 src/library.ts• L224-235

Specialized entry points

The package exposes separate entry points so each host receives only the interface it needs. ascii.rest/pieces supplies artwork, ascii.rest/banner creates text as a piece, and the terminal, SVG, React, element, and Astro entries adapt that same piece model to their targets (package.json• L22-60).

The banner API is part of the same model rather than a separate kind of artwork. banner(text, options?) returns a piece, so callers can mount it in a browser, play it in a terminal, or pass it to an SVG renderer (src/banner.ts• L395-400, src/banner.ts• L558-577).

References: package.json• L22-60 src/banner.ts• L395-400 src/banner.ts• L558-577

Mounting lifecycle

Mount operation

mount starts a piece in a caller-owned HTML element and returns a stop function. Text output uses a <pre>; a piece with a palette uses a <canvas> when the caller selects color output. A colored piece can also use the text path when the caller wants one ink (src/mount.ts• L44-52).

Mount options combine the piece defaults with caller overrides. The caller can set a frame rate and can explicitly allow motion when reduced-motion preferences would otherwise hold the piece still (src/mount.ts• L23-27, src/mount.ts• L47-63).

References: src/mount.ts• L23-27 src/mount.ts• L44-63

Playback lifecycle

The mount lifecycle draws an initial frame, starts motion only when the element is visible and the document is active, and stops all work when the returned cleanup function runs. Reduced-motion settings select the piece’s still moment unless the caller enables motion for an explicit control (src/mount.ts• L60-70, src/mount.ts• L184-221).

The diagram shows how a caller-owned element moves from setup to managed playback and cleanup.

flowchart LR
    A[Choose piece and element] --> B[mount]
    B --> C[Draw initial frame]
    C --> D{Visible and motion allowed?}
    D -->|Yes| E[Advance and redraw]
    D -->|No| F[Hold current frame]
    E --> D
    F --> D
    D -->|Stop function or element cleanup| G[Disconnect observers and stop playback]

References: src/mount.ts• L60-70 src/mount.ts• L184-221

Target selection

Bundled and named pieces

Callers choose a bundled piece when startup control and static imports matter. They choose a name when the host can load artwork after the page or component mounts. Named loading is limited to names recognized by the library, while direct modules must follow the piece contract (src/library.ts• L5-6, src/library.ts• L226-235).

Framework adapters use this same choice. A React Ascii receives either a piece module or a piece name; a custom element accepts a library name or a module URL. In both cases, the visible output waits for the selected piece to be ready (src/react.tsx• L25-69, src/ascii.ts• L71-98).

References: src/library.ts• L5-6 src/library.ts• L226-235 src/react.tsx• L25-69 src/ascii.ts• L71-98

Text and color targets

Use text output when the host should control the ink and font, or when it does not support a drawing surface. Use color output when the piece supplies a palette and the target can display cell colors. The public APIs preserve this choice across browser elements, framework components, and terminal playback (src/mount.ts• L49-59, src/terminal.ts• L31-46).

For a target that cannot run the live lifecycle, use an SVG or a still frame instead. These paths return data that the caller can write, serve, or place in a document without a running animation loop (src/terminal.ts• L238-249, src/svg.ts• L305-322).

References: src/mount.ts• L49-59 src/terminal.ts• L31-46 src/terminal.ts• L238-249 src/svg.ts• L305-322

Change checklist

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

  • Keep the root exports, package export map, and public type declarations aligned when a rendering entry point changes.
  • Keep named-piece loading and the canvas classification aligned when adding or changing artwork names.
  • Preserve the Piece contract when changing banner output or kit-created pieces so every target can still consume them.
  • Preserve mount cleanup and reduced-motion behavior when changing frame scheduling or framework lifecycle code.
  • Keep the target choice consistent between direct imports, named loading, mono, and accessibility labels.

Important failure modes

TriggerConsequenceGuard
An unknown piece name reaches a named loaderThe caller cannot load or mount the artworkCheck isPiece before loading, or use a direct piece module
A module does not export a valid pieceCustom-element loading cannot startExport a piece as the module default or as meta plus a default frame function
A mount is not stopped after its element leaves useObservers and animation work can outlive the host elementRetain and call the stop function during host cleanup
A colored piece is sent to a text-only target without mono handlingThe target loses the intended color treatmentChoose a canvas-capable target or request one-ink text output
A live target ignores reduced-motion stateUsers who request reduced motion still see playbackUse the default mount policy or enable motion only from an explicit user control

Key files

FileWhy to read it
src/index.tsDefines the root browser API and its public exports.
src/types.tsDefines the piece contract shared by all rendering targets.
src/mount.tsShows the browser mount, playback, accessibility, and cleanup lifecycle.
src/library.tsDefines named piece loading, discovery, and color-target classification.
package.jsonDefines the package entry points that callers import.
src/banner.tsShows how caller text becomes a reusable piece.
src/react.tsxShows how a framework adapter chooses and mounts its target.
src/ascii.tsShows the custom-element entry point and attribute-driven lifecycle.