---
title: "Shaders and Images"
description: "Terminal apps, written in TypeScript and compiled to native executables with scriptc."
canonical_url: "https://tui-sdk.dev/docs/guides/graphics"
---
# Shaders and Images

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

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

runApp({
  fps: 60,
  view: (frame) =>
    drawShader(frame, frame.rect, (x, y, time) =>
      rgb(0.5 + 0.5 * Math.sin(x / 9 + time), 0.5 + 0.5 * Math.sin(y / 7 - time), 0.8),
    ),
})
```


A shader's colors don't become styles: each cell keeps its two colors in its style id, at 32 levels a channel with an ordered dither. So a full screen of new colors every frame allocates nothing and adds no styles, and where colors repeat from one cell to the next, nothing is written for them.

## What a shader can draw

Each of these is a single file in the repository's `examples` directory, drawn with `drawShader` at 60 fps: a fluid simulation, a lit Earth, a raycast hall, and a raymarched torus.

The HTML page runs these examples here, in a terminal in the page:

- [Fluid: `examples/fluid.ts`](https://github.com/vercel-labs/tui/blob/main/examples/fluid.ts)
- [Earth: `examples/earth.ts`](https://github.com/vercel-labs/tui/blob/main/examples/earth.ts)
- [3D: `examples/maze.ts`](https://github.com/vercel-labs/tui/blob/main/examples/maze.ts)
- [Shader: `examples/shader.ts`](https://github.com/vercel-labs/tui/blob/main/examples/shader.ts)

## Images

`createImage(pixels, width, height)` takes an image's pixels, 4 bytes each for red, green, blue, and alpha, and `drawImage(frame, rect, image)` draws it as large as fits with its shape kept. In kitty and Ghostty, it's the image itself, through the kitty graphics protocol's Unicode placeholders. The image is sent once, and its cells are text, so whatever is drawn over them, such as a dialog, covers it. In other terminals, and inside tmux, it's drawn in half blocks. tui reads pixels rather than files, so it needs no image decoder: decode a PNG or JPEG with the library of your choice.
