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-drawing-and-fields

Kit drawing and fields

The drawing and field tools turn a kit piece into an ASCII visual. Drawing tools place known shapes and glyphs on a cell grid, while field tools turn a value for each cell into characters and colour. Both can build a complete piece or add a layer to a larger scene.

Drawing model

The drawing model treats a piece as a surface of cells. Each tool writes characters and optional palette colours into that surface, so later calls can frame, fill, label, or layer earlier work. The public exports for these tools are grouped in src/…/index.ts• L261-295.

Surface drawing

The surface is the shared target for ordinary drawing. Text and shapes use cell coordinates, clip work at the surface edge, and can leave existing cells visible when their spaces or empty areas are transparent. Regions returned by boxes and text let one drawing describe the space for the next drawing.

flowchart LR
    A[Piece surface] --> B[Text and labels]
    A --> C[Lines and boxes]
    A --> D[Curves and polygons]
    A --> E[Stamped art]
    B --> F[Layered ASCII frame]
    C --> F
    D --> F
    E --> F

The main drawing groups are:

GroupPurposeMain tools
TextPlace readable content in a region or around a pointtext(), label()
PathsConnect points with straight, sloped, or joined strokesline(), polyline(), ray()
ContainersBuild frames and provide an inside regionrect()
ShapesDraw round forms, arcs, and closed filled formscircle(), ellipse(), arc(), polygon()
StampsLay text art or another surface over the current onestamp()

References: src/…/core.ts• L1-13 src/…/draw.ts• L28-96 src/…/draw.ts• L453-525 src/…/draw.ts• L689-765 src/…/index.ts• L261-295

Glyph choices

Glyph choice lets a drawing express line direction, density, and colour without requiring a separate image format. A stroke can use one character or choose slope-aware characters; a box style can use joined border glyphs; and a field can map values through a character ramp.

The standard box styles are single, double, rounded, heavy, and ascii. Custom six-character borders are also supported. Drawing colours are palette indexes or #rrggbb values that resolve to the current surface palette.

The kit keeps a measured set of glyph shapes for image-style rendering. These glyphs describe ink across small parts of a cell, which lets image conversion choose a character that matches an edge or a shade instead of treating every cell as a solid block. The glyph data is generated from the project font data and is not a user-facing drawing API.

References: src/…/draw.ts• L142-223 src/…/draw.ts• L267-335 src/…/draw.ts• L650-765 src/…/core.ts• L79-119 src/…/glyphs.ts• L1-25 src/…/image.ts• L21-59

Fine drawing canvases

Fine canvases keep the same cell surface but draw at a higher internal resolution. They are useful when whole-cell characters cannot show a smooth curve or crisp pixel art. The canvas is prepared, drawn, and then written back to the surface with draw().

Braille canvas

The braille canvas gives each cell a two-by-four dot area. It supports lines, rays, circles, arcs, rectangles, and plots, so curves can keep more detail than a one-character stroke. A plot accepts either a function over an x range or a list of values, and can fill the area below the curve.

Use a region when the plot belongs inside a frame or beside other content. Empty braille cells do not replace the surface below them, which makes the canvas suitable for overlays.

References: src/…/draw.ts• L1103-1169 src/…/draw.ts• L1185-1332 src/…/draw.ts• L1346-1359

Pixel canvas

The pixel canvas gives each cell two square pixels, one above the other. It supports basic shapes and sprite art written as rows of characters. A sprite can use a colour for each art character, select frames from a sprite sheet, and wrap across the canvas edge.

The canvas writes lit pixels as upper-half, lower-half, or full-block glyphs. One cell has one stored colour, so two lit pixels with different colours resolve to the upper pixel's colour when they become a full block.

References: src/…/draw.ts• L1361-1425 src/…/draw.ts• L1438-1557 src/…/draw.ts• L1559-1574

Field-based drawing

A field defines the visual value of every cell with a function of position and time. The field system turns that value into a glyph, optional colour, and empty cell, which makes it suited to procedural scenes such as waves, rings, plasma, and light.

Cell field

field() creates a normal kit piece from a cell function. The function receives centered coordinates, time, and a cell record with normalized position, radius, angle, region size, loop phase, and per-cell glyph or colour overrides. A numeric result becomes shading; true is solid ink; false, null, and NaN leave the cell empty.

The default coordinates account for the tall shape of normal terminal cells, so a mathematical circle stays round on screen. Set aspect: false when the formula should use independent -1 to 1 axes instead.

flowchart LR
    A[Cell position and time] --> B[Field function]
    B --> C{Cell result}
    C -->|number| D[Normalize and shade]
    C -->|true| E[Solid glyph]
    C -->|false or null| F[Leave empty]
    D --> G[Character ramp and colour]
    E --> G
    G --> H[Piece frame]

References: src/…/field.ts• L51-110 src/…/field.ts• L112-178 src/…/field.ts• L470-523

Field shading

Field shading maps a result range to a ramp from light to dense glyphs. Named ramps provide common styles, and custom ramps allow a piece to choose its own visual language. range defines the input limits, gamma changes the middle values, and dither mixes neighboring steps for smoother bands.

Colour can follow field value through colors, use a custom color function, or be set for one cell with at.color. invert: "auto" keeps bright values visually bright on both dark and light pages; true and false make that choice fixed.

References: src/…/core.ts• L79-119 src/…/field.ts• L112-171 src/…/field.ts• L350-417

Field composition

drawField() applies the same field model inside a piece that already contains other drawings. Its region option lets a field sit behind text, inside a frame, or beside another field. Empty field cells preserve the content below them, so the order of drawing becomes the layer order.

When a field has a period, its cell record exposes a repeating phase and field() uses that period as the piece loop. A formula based on whole turns of that phase produces a frame that returns to its start without a visible jump.

References: src/…/field.ts• L174-178 src/…/field.ts• L270-333 src/…/field.ts• L439-455

Drawing choices

The right tool depends on whether the visual is known in advance, needs smooth sub-cell detail, or comes from a value at every cell.

NeedPreferred toolResult
Frame, label, wire, or geometric formSurface drawingDirect control of cells and glyphs
Smooth curve or chartBraille canvasTwo-by-four dot detail per cell
Crisp sprite or pixel iconPixel canvasTwo square pixels per cell
Procedural shading or animated texturefield()A complete generated piece
Procedural layer in a composed piecedrawField()A field that shares the current surface

For higher-level scene layout, see Kit scene composition. For material and object drawing, see Kit effects and materials. For geometry helpers that supply positions or values, see Kit geometry, math, and images. For ready-made examples, see Kit recipes.

Change checklist

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

  • Changes to the surface colour contract must stay aligned with ordinary drawing, stamps, fine canvases, and field colour resolution.
  • Changes to cell aspect or turn coordinates must be checked across surface shapes, braille dots, pixel sprites, and field coordinates.
  • Changes to field empty-cell behavior must preserve layering for drawField() and frame independence for field().
  • Changes to ramps or glyph data must preserve both light-page inversion and image edge or shade selection.
  • Changes to region handling must keep returned box regions usable by text, braille, pixels, and field layers.

Important failure modes

TriggerConsequenceGuard
A field function returns undefined or another unsupported valueFrame generation stops because the cell has no valid meaningReturn a number, true, false, null, or NaN on every path
A field uses at.color or a colour function without a paletteThe field cannot resolve its chosen coloursSupply palette or value-based colors
A drawing uses both a stroke character and a box styleThe stroke has two conflicting glyph rulesChoose one of char or style
A field formula assumes square cells with default aspect handlingRound forms look stretchedKeep the default aspect model or set aspect: false deliberately
A sprite or stamp uses a mask that does not match the source artIntended background cells cover or fail to cover lower layersUse the source's empty or mask character consistently

Key files

FileWhy to read it
src/…/index.tsPublic entry point for drawing, canvases, and field exports
src/…/draw.tsCore surface drawing model, shape tools, stamps, braille, and pixels
src/…/field.tsField coordinate model, shading, colour mapping, looping, and composition
src/…/core.tsShared surface, ramp, palette, and piece concepts
src/…/glyphs.tsMeasured glyph data used by image-style character selection
src/…/image.tsImage-to-glyph and image-to-pixel drawing choices
src/…/draw.test.tsBehavioral contract for surface drawing and fine canvases
src/…/field.test.tsBehavioral contract for coordinates, shading, colour, regions, and loops