---
title: "Drawing"
description: "Terminal apps, written in TypeScript and compiled to native executables with scriptc."
canonical_url: "https://tui-sdk.dev/docs/reference/drawing"
---
# Drawing

## drawBox

`drawBox(cells, rect, options?)` draws a border around `rect` and returns the rect inside it. Options are `border` (`"rounded"`, `"single"`, `"double"`, or `"heavy"`; rounded by default), `style`, `title`, and `titleStyle`.

## createCanvas

`createCanvas(mode?)` returns a canvas of pixels. `mode` is `"blocks"` (the default: each cell holds two pixels of any color) or `"braille"` (each cell holds 2 by 4 dots). Coordinates are in pixels from the top left; fractions round down, and what falls outside is left out.

| Method                             | Description                                           |
| ---------------------------------- | ----------------------------------------------------- |
| `resize(columns, rows)`            | Sizes the canvas to that many cells and empties it.   |
| `clear(color?)`                    | Sets every pixel to a color, or empties them all.     |
| `get(x, y)`                        | A pixel's color, or -1 when it is empty or outside.   |
| `set(x, y, color)`                 | Sets a pixel to a color, or empties it with -1.       |
| `line(x0, y0, x1, y1, color)`      | A line from one pixel to another, both ends included. |
| `rect(x, y, width, height, color)` | A filled rectangle.                                   |
| `circle(x, y, radius, color)`      | A filled circle around a point.                       |
| `width`, `height`                  | The size in pixels.                                   |

`drawCanvas(frame, rect, canvas)` draws the canvas in a rect.

```ts
import {
  createCanvas,
  createChart,
  createTestApp,
  drawCanvas,
  drawMeter,
  drawSparkline,
  split,
} from "tui"

const canvas = createCanvas()
const chart = createChart()

const app = createTestApp({
  columns: 40,
  rows: 11,
  view: (frame) => {
    const [top, middle, bottom, graph] = split(frame.rect, "rows", [3, 2, 3, -1])
    canvas.resize(top.columns, top.rows)
    canvas.line(0, 0, canvas.width - 1, canvas.height - 1, 0x3291ff)
    drawCanvas(frame, top, canvas)
    drawMeter(frame, middle, 0.6)
    drawSparkline(frame, bottom, [1, 4, 2, 8, 5, 9])
    chart.draw(frame, graph, { series: [{ values: [1, 4, 2, 8, 5, 9, 3] }] })
  },
})

console.log(app.screen().join("\n"))
```

## drawShader

`drawShader(frame, rect, shade)` calls `shade(x, y, time)` for every pixel in `rect`, two to a cell, and draws the color it returns as `0xrrggbb`. `time` is in seconds. `rgb(red, green, blue)` makes a color from channels of 0 to 1.

```ts
import { createTestApp, drawShader, rgb } from "tui"

const app = createTestApp({
  columns: 12,
  rows: 4,
  view: (frame) => {
    drawShader(frame, frame.rect, (x, y, time) => rgb(x / 12, y / 8, time))
  },
})

app.advance(16)
console.log(app.screen().join("\n"))
```

## createChart

`createChart()` returns `{ draw(frame, rect, options) }`. The options are `series` (`{ values, style? }[]`), `min`, `max`, and `format(value)`. Values are spread evenly across the chart; a value that is not a number is a gap. The chart is drawn in braille dots, with the axis on the left labeled with the top and bottom values.

## drawMeter and drawSparkline

- `drawMeter(frame, rect, value, options?)` fills every row of `rect` from the left to `value` (0 to 1) in eighths of a cell. Options are `style` and `track`.
- `drawSparkline(frame, rect, values, options?)` draws a number's recent history as bars, one value per column with the newest at the right. Options are `min` (0 by default), `max`, and `style`.

## createImage

`createImage(pixels, width, height)` takes an image's pixels, 4 bytes each for red, green, blue, and alpha, row by row from the top left. `drawImage(frame, rect, image)` draws it as large as fits with its shape kept.

In kitty and Ghostty the image itself is sent through the kitty graphics protocol's Unicode placeholders. The image is sent once, and its cells are text, so whatever is drawn over them covers it. Other terminals, and tmux, get half blocks. tui reads pixels rather than files, so it needs no image decoder.

```ts
import { createImage, createTestApp, drawImage } from "tui"

const pixels = new Uint8Array([255, 0, 0, 255, 0, 255, 0, 255, 0, 0, 255, 255, 255, 255, 255, 255])
const image = createImage(pixels, 2, 2)

const app = createTestApp({
  columns: 8,
  rows: 4,
  view: (frame) => {
    drawImage(frame, frame.rect, image)
  },
})

console.log(app.screen().join("\n"))
```

## Filtering

`createFilter()` finds the lines of a list that match a query, a few milliseconds a frame. Call `update(lines, query, budget?)` every frame and draw the matches found so far. `count` is how many match, `at(index)` is the index in the list of the `index`th match, and `scanned` and `done` say how much of the list has been tested.

The list is read as one that only grows, like a log's: lines added since the last update are all that is tested, and a query that adds to the last one tests only the lines that matched it. A query without a capital letter ignores case; one with a capital matches it. `findMatch(value, query, from?)` returns where the query first matches, or -1.

`drawMatches(frame, x, y, value, query, style?, clip?, match?)` draws a line with each match in the match style.

```ts
import assert from "node:assert/strict"
import { createFilter, createTestApp, drawMatches } from "tui"

const lines = ["apple", "apricot", "banana", "kiwi"]
const filter = createFilter()

const app = createTestApp({
  columns: 20,
  rows: 4,
  view: (frame) => {
    filter.update(lines, "ap")
    for (let row = 0; row < filter.count; row++) {
      drawMatches(frame, 0, row, lines[filter.at(row)] ?? "", "ap")
    }
  },
})

assert.equal(filter.count, 2)
console.log(app.screen().join("\n"))
```

## Markdown

`createMarkdown(styles, theme?)` lays out Markdown as rows of styled text that fit a width. It reads paragraphs, headings, lists and task lists, quotes, code blocks, tables, rules, and inline code, strong, emphasis, strikethrough, and links. Each source is laid out once for each width it is drawn at.

`markdown.lines(source, columns)` returns the rows for a source at a width. `drawLines(frame, rect, lines, offset?)` draws them, from row `offset`.

```ts
import { createMarkdown, createTestApp, drawLines } from "tui"

let markdown: ReturnType<typeof createMarkdown> | null = null

const app = createTestApp({
  columns: 40,
  rows: 6,
  view: (frame) => {
    markdown ??= createMarkdown(frame.styles, frame.theme)
    const lines = markdown.lines("# tui\n\n**Typed** terminal interfaces.", frame.rect.columns)
    drawLines(frame, frame.rect, lines)
  },
})

console.log(app.screen().join("\n"))
```
