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

`createSpring` and `createTransition` move a number toward a target, for panels that slide, highlights that glide, and numbers that count up. Set where it goes with `to`, and call `update(frame)` in the view for where it is in this frame. A spring keeps its speed when its target changes on the way, so it never jumps; a transition takes a set time on a curve: `easeOut` by default, or `easeIn`, `easeInOut`, `linear`, or a function of your own.

```ts
import { createSpring, fill, runApp } from "tui"

const panel = createSpring(0)

runApp({
  view: (frame) => {
    const width = Math.round(panel.update(frame) * 30)
    fill(frame.cells, { ...frame.rect, columns: width }, frame.styles.style({ bg: "#1f2a44" }))
  },
  keys: () => {
    panel.to(panel.target ? 0 : 1)
    return true
  },
})
```


An app needs no `fps` for this. While something moves, `update` calls `frame.animate()`, which draws the next frame a sixtieth of a second later, and once everything is at rest, the app draws only when something changes again. A view can call `frame.animate()` itself for motion of its own. In a test app, `advance(ms)` draws the frames a spring asks for. [`glide.ts`](https://github.com/vercel-labs/tui/blob/main/examples/glide.ts) has a highlight that glides, a panel that slides in, numbers that count up, and rows that move to their new order.

The HTML page runs [`examples/glide.ts`](https://github.com/vercel-labs/tui/blob/main/examples/glide.ts) here, in a terminal in the page.

## A spinner

A spinner in a view is a frame of characters chosen by `frame.time`. Call `frame.animate()` while the work runs, so the view keeps drawing.

```ts
import { runApp, text } from "tui"

const FRAMES = "⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"

runApp({
  view: (frame) => {
    const busy = frame.time < 2000
    if (busy) {
      frame.animate()
    }
    const index = Math.floor(frame.time / 80) % FRAMES.length
    text(frame, 0, 0, `${FRAMES[index] ?? ""} Working`)
  },
})
```

`FRAMES` holds ten frames, and `frame.time` moves to the next one every 80 ms. Here `busy` is true for the first two seconds; a real app sets it from its work. While it is true, `frame.animate()` asks for the next frame a sixtieth of a second later.

## Stream text

Store the whole reply and draw the part that `frame.time` says is visible. Call `frame.animate()` until the last character shows.

```ts
import { runApp, text } from "tui"

const reply = "The build finished. Three tests passed."
let started = -1

runApp({
  view: (frame) => {
    if (started < 0) {
      started = frame.time
    }
    const visible = reply.slice(0, Math.floor((frame.time - started) / 24))
    const complete = visible.length === reply.length
    if (!complete) {
      frame.animate()
    }
    text(frame, 0, 0, complete ? visible : `${visible}▍`)
  },
})
```

`visible` adds a character every 24 ms, and the `▍` cursor shows while more remains. When the reply is complete, the view stops asking for frames. Use `createMarkdown` and `drawLines` to draw the visible part when the reply is Markdown.

## Update from a timer

`runApp` returns the app immediately. A timer, a socket, or other code that changes state calls `app.redraw()` to draw the change. Without `fps`, nothing draws until a key, a resize, `frame.animate()`, or `app.redraw()` asks for it.

```ts
import { runApp, text } from "tui"

let count = 0

const app = runApp({
  view: (frame) => {
    text(frame, 0, 0, `Count: ${count}`)
  },
})

setInterval(() => {
  count++
  app.redraw()
}, 1000)
```

For something that changes every frame, use `fps` and `frame.delta` instead; the next section covers both.

## Animation and games

Without `fps`, an app draws only when something changes. With it, the app draws that many frames a second, and each frame says when it's drawn: `frame.time` is the milliseconds since the app started, `frame.delta` the milliseconds since the frame before, and `frame.fps` how many frames the terminal really got in the last second.

```ts
import { createCanvas, drawCanvas, runApp } from "tui"

const canvas = createCanvas()

runApp({
  fps: 60,
  view: (frame) => {
    canvas.resize(frame.rect.columns, frame.rect.rows)
    const x = (frame.time / 20) % canvas.width
    canvas.circle(x, canvas.height / 2, 4, 0x3291ff)
    drawCanvas(frame, frame.rect, canvas)
  },
})
```


Frames are drawn on time: one that's late doesn't make the next come sooner. In a test app, `advance(ms)` moves the clock on and draws the frame the timer would.

A game also needs to know which keys are down. With `heldKeys`, `frame.held("w")`, `frame.held("space")`, or `frame.held("shift")` says so. Terminals with the kitty keyboard protocol, such as kitty, Ghostty, WezTerm, foot, and recent iTerm2 and Alacritty, say when each key is let go, so a key is held from its press to its release, and nothing is held once the window loses the focus. Other terminals never say a key is let go, so there a key counts as held until a moment after its last press or repeat.
