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:
| Group | Purpose | Main tools |
|---|---|---|
| Text | Place readable content in a region or around a point | text(), label() |
| Paths | Connect points with straight, sloped, or joined strokes | line(), polyline(), ray() |
| Containers | Build frames and provide an inside region | rect() |
| Shapes | Draw round forms, arcs, and closed filled forms | circle(), ellipse(), arc(), polygon() |
| Stamps | Lay text art or another surface over the current one | stamp() |
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.
| Need | Preferred tool | Result |
|---|---|---|
| Frame, label, wire, or geometric form | Surface drawing | Direct control of cells and glyphs |
| Smooth curve or chart | Braille canvas | Two-by-four dot detail per cell |
| Crisp sprite or pixel icon | Pixel canvas | Two square pixels per cell |
| Procedural shading or animated texture | field() | A complete generated piece |
| Procedural layer in a composed piece | drawField() | 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 forfield(). - 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
| Trigger | Consequence | Guard |
|---|---|---|
A field function returns undefined or another unsupported value | Frame generation stops because the cell has no valid meaning | Return a number, true, false, null, or NaN on every path |
A field uses at.color or a colour function without a palette | The field cannot resolve its chosen colours | Supply palette or value-based colors |
| A drawing uses both a stroke character and a box style | The stroke has two conflicting glyph rules | Choose one of char or style |
| A field formula assumes square cells with default aspect handling | Round forms look stretched | Keep the default aspect model or set aspect: false deliberately |
| A sprite or stamp uses a mask that does not match the source art | Intended background cells cover or fail to cover lower layers | Use the source's empty or mask character consistently |
Key files
| File | Why to read it |
|---|---|
| src/…/index.ts | Public entry point for drawing, canvases, and field exports |
| src/…/draw.ts | Core surface drawing model, shape tools, stamps, braille, and pixels |
| src/…/field.ts | Field coordinate model, shading, colour mapping, looping, and composition |
| src/…/core.ts | Shared surface, ramp, palette, and piece concepts |
| src/…/glyphs.ts | Measured glyph data used by image-style character selection |
| src/…/image.ts | Image-to-glyph and image-to-pixel drawing choices |
| src/…/draw.test.ts | Behavioral contract for surface drawing and fine canvases |
| src/…/field.test.ts | Behavioral contract for coordinates, shading, colour, regions, and loops |