Skip to contentBlack Friday: Framescript for €79 instead of €99 with the code BF2026

Reference: framescript/code

What a scene imports from framescript/code and calls, in the order the module exports it. Each entry has its signature, what it does, and the examples that use it; the types its signatures name are at the end.

Explained in Code blocks.

code

TypeScript
function code(source: string, options?: CodeOptions): CodeBlock

Makes a block of code to pass to add: monospace text, coloured token by token. Give it a string, or the text of a video file with the lines to show, so that a scene shows its own source:

TypeScript
import { add } from "framescript"
import { code } from "framescript/code"
import source from "./release.ts?raw"

const block = yield* add(code(source, { lines: [12, 14], language: "ts" }))

Its layout is computed while add waits, which takes no scene time; after that lines, tokens, chars, width and height can be used.

  • source: The code.
  • options: The language, the lines to show, the font and colours.
  • Returns: The code block.

Examples: code/change, code/reading, code/typing

codeModel

TypeScript
function codeModel(source: string, selection?: CodeSelection): CodeModel

Reads a source into the lines and tokens a code block shows. The whole source is tokenized before the lines are picked, so a range that starts inside a comment or a template literal is still coloured as one.

  • source: The code: a string, or the text of a file.
  • selection: The language, the range of lines, the tab size and whether shared indentation is removed.
  • Returns: The content with its lines and tokens.

Examples: code/reading

TypeScript
function blink(
  caret: THREE.Object3D,
  options?: BlinkOptions
): Step<void>

Blinks a caret: shown for half a period, hidden for the other half, as a function of scene time. It ends shown.

  • caret: A code block's caret, or any object.
  • options: Duration (default 2), period (default 1) and delay.
  • Returns: A step that ends after the duration.

Examples: code/typing

change

TypeScript
function change(
  block: CodeTarget,
  token: CodePiece | string | undefined,
  to: string,
  options?: ChangeOptions
): Step<void>

Turns one literal of a code block into another, the way an edit in the studio rewrites a literal and nothing else. Only that token animates: a number counts to the new number, anything else settles out of scrambled letters. The rest of the line stays where it is when the new text is as long as the old one; otherwise what follows on the line moves over by exactly the difference, and nothing before the token moves at all.

  • block: A code block made with code().
  • token: One of the block's tokens, or the text of one, such as "0.6", which finds the first token with that text.
  • to: The text the token is to have.
  • options: Duration (default 0.6), easing, delay, steps and seed.
  • Returns: A step that ends when the token shows its new text.

Examples: code/change

changeContents

TypeScript
function changeContents(
  before: string,
  after: string,
  options?: Pick<ChangeOptions, "steps" | "seed">
): Array<string>

The texts a token shows while it turns from one literal into another: both numbers, it counts from the one to the other at their precision; otherwise the new text settles out of scrambled letters, between their quotes when both are strings in the same quotes. The first is always the old text and the last the new one.

  • before: The token's text now.
  • after: The text it is to have.
  • options: Steps and seed.
  • Returns: The texts, in order.

Examples: code/change

highlightLine

TypeScript
function highlightLine(
  block: CodeTarget,
  line: number,
  options?: HighlightOptions
): Step<void>

Puts the highlight band on a line. The first time, the band grows across the line from the left; after that it travels from the line it is on and takes the new line's width. Line 0 takes it away again. Only the band's position and scale are animated, so its opacity stays free to fade.

  • block: A code block made with code().
  • line: The line, counted from 1 at the top of the block; 0 for none.
  • options: Duration (default 0.4), easing, delay and fit.
  • Returns: A step that ends when the band is in place.

Examples: code/change

type

TypeScript
function type(block: CodeTarget, options?: TypeOptions): Step<void>

Types a code block, or a stretch of it, at a person's rhythm: each character is hidden until its turn and then shows at once, and the caret stands after the last one typed, and before the first until then. The rhythm comes from the seed, so every render types the same way. block.lineSpan(2) gives from and to for a line.

  • block: A code block made with code().
  • options: Seed, rate (default 14 characters a second) or duration, the stretch to type, and delay.
  • Returns: A step that ends when the last character has appeared.

Examples: code/typing

typingTimes

TypeScript
function typingTimes(
  text: string,
  options?: RhythmOptions
): Array<number>

When each character of a text appears if a person types it: an uneven beat around the rate, a breath after a word and after punctuation, a longer one before a new line, a quick repeat of a doubled letter, and now and then a moment of thought. The indentation of a new line arrives with the line break, as an editor puts it there. Everything comes from the seed; nothing differs between runs.

  • text: The text to type.
  • options: The seed and the rate.
  • Returns: For each UTF-16 unit of the text, the seconds from the start at which it has appeared; the first appears at 0. The last entry is how long the typing takes.

Examples: code/typing

darkTheme

TypeScript
const darkTheme: CodeTheme

The colours a code block has when it is given none: for a dark ground.

Examples: code/reading

tokenize

TypeScript
function tokenize(source: string, language?: CodeLanguage): Array<Token>

Splits source code into tokens for colouring. Nothing is dropped: the tokens' texts, joined, are the source again. It reads one token at a time and never fails; code it does not understand comes out as "plain".

  • source: The code.
  • language: The language it is written in. Defaults to "text", which splits into words and colours nothing.
  • Returns: The tokens in order, white space included.

Examples: code/reading

Types

BlinkOptions

TypeScript
interface BlinkOptions {
  /** How long to blink for, in seconds. Defaults to 2. */
  readonly duration?: number
  /** The seconds one blink takes: half shown, half hidden. Defaults to 1. */
  readonly period?: number
  /** Seconds to wait before starting. */
  readonly delay?: number
}

ChangeOptions

TypeScript
interface ChangeOptions extends MotionOptions {
  /**
   * How many texts the token passes through. Defaults to every value at the
   * literals' precision for numbers, at most 61, and to 8 for anything else.
   */
  readonly steps?: number
  /** The seed of the letters a text scrambles through. Defaults to 1. */
  readonly seed?: number
}

CodeBlock

TypeScript
class CodeBlock extends Group {
  /** The shown text with its lines and tokens, known from the start. */
  readonly model: CodeModel
  /** The colours in use. */
  readonly theme: CodeTheme
  /** The text object that draws the code. */
  readonly text: TextObject
  /**
   * The caret: a bar one character box high whose origin is its top left
   * corner. It stands after the last character until something moves it;
   * `caretAt` gives the position for any other place.
   */
  readonly caret: Mesh<PlaneGeometry, MeshBasicMaterial>
  /**
   * The line highlight: a band whose origin is its top left corner and
   * whose scale is its size. It has no width until `highlightLine` gives it
   * one.
   */
  readonly highlight: Mesh<PlaneGeometry, MeshBasicMaterial>
  /** One part per line of the block, from the top; an empty line's is empty. */
  lines: Array<TextPart>
  /** One part per visible token, in reading order, as in `model.tokens`. */
  tokens: Array<TextPart>
  /** Every visible character, in reading order. White space has none. */
  chars: Array<TextPart>
  /** Whether the block is laid out, which `add` waits for. */
  ready: boolean
  /** The grid the characters sit on. Measured once the block is ready. */
  grid: CodeGrid
  private readonly style
  private readonly infos
  /**
   * @param source - The code: a string, or the text of a file.
   * @param options - The language, the lines to show, the font and colours.
   */
  constructor(source: string, options?: CodeOptions)
  /** The shown text: the block's lines, joined by line breaks. */
  get content(): string
  /**
   * The tokens under the name a text has for its words, for the text presets.
   */
  get words(): Array<TextPart>
  /** The width of one character. */
  get advance(): number
  /** The width of the block: its longest line. */
  get width(): number
  private edge
  /** The height of the block: its lines at their line height. */
  get height(): number
  /**
   * Lays the code out, which `add` waits for.
   *
   * @returns Nothing when the block is ready now; otherwise a promise that
   *   resolves when it is, or when calling again will make it so.
   */
  framescriptReady(): Promise<void> | undefined
  private build
  private place
  /**
   * Where the caret stands when it is before a character.
   *
   * @param index - A position in `content`, from 0 to its length; the
   *   length is the place after the last character.
   * @returns The position for `caret.position`.
   */
  caretAt(index: number): {
    x: number
    y: number
  }
  /**
   * The positions in `content` that a line spans, for `type`.
   *
   * @param line - The line, counted from 1 at the top of the block.
   * @returns Where the line starts and where it ends, its line break not
   *   included.
   * @throws When the block has no such line.
   */
  lineSpan(line: number): {
    from: number
    to: number
  }
  /**
   * The band a line occupies, for the highlight.
   *
   * @param line - The line, counted from 1 at the top of the block.
   * @param fit - `"block"` for the width of the block, `"line"` for the
   *   width of the line's own text. Defaults to `"block"`.
   * @returns The band's top left corner and its size, in the block's space.
   * @throws When the block has no such line.
   */
  lineBox(
    line: number,
    fit?: "block" | "line"
  ): {
    x: number
    y: number
    width: number
    height: number
  }
  private lineInfo
  /**
   * Finds a token by its text.
   *
   * @param content - The token's text, such as `"0.6"`.
   * @param nth - Which of the tokens with that text, counted from 0.
   *   Defaults to the first.
   * @returns The token's part, or `undefined` when there is none or the
   *   block is not ready.
   */
  token(content: string, nth?: number): TextPart | undefined
  /**
   * What a token part is: its kind, its text and its cell.
   *
   * @param part - One of `tokens`.
   * @returns The token's entry in `model.tokens`, or `undefined` for
   *   anything else.
   */
  infoOf(part: object): CodeTokenInfo | undefined
  /**
   * Makes the texts that stand in for a token while it changes: a flipbook
   * set like the block, placed on a cell. `change` adds it under the block.
   *
   * @param contents - The texts to step through, in order.
   * @param cell - The line and column, counted from 0, of the first
   *   character, and the kind of token whose colour to use.
   * @returns The flipbook, not yet added.
   */
  replacement(
    contents: ReadonlyArray<string>,
    cell: {
      line: number
      column: number
      kind: TokenKind
    }
  ): Flipbook
}

CodeFrames

TypeScript
type CodeFrames = THREE.Object3D & {
  readonly frames: ReadonlyArray<THREE.Object3D>
}

CodeLanguage

TypeScript
type CodeLanguage = "ts" | "js" | "json" | "shell" | "text"

CodeLineInfo

TypeScript
interface CodeLineInfo {
  /** The line's text, without its line break. */
  readonly text: string
  /** Where the line starts in the block's content. */
  readonly start: number
  /** The line's visible tokens, from the left. */
  readonly tokens: ReadonlyArray<CodeTokenInfo>
}

CodeModel

TypeScript
interface CodeModel {
  /** The shown text: the lines, joined by line breaks. */
  readonly content: string
  /**
   * The number, counted from 1, that the first shown line has in the source.
   */
  readonly firstLine: number
  /** The lines, from the top. An empty line has no tokens. */
  readonly lines: ReadonlyArray<CodeLineInfo>
  /** Every visible token, in reading order. White space has none. */
  readonly tokens: ReadonlyArray<CodeTokenInfo>
}

CodeOptions

TypeScript
interface CodeOptions extends CodeSelection {
  /**
   * The URL of a monospace `.ttf`, `.otf` or `.woff` font file. Defaults to
   * the JetBrains Mono that ships with Framescript, which covers Latin only.
   */
  readonly font?: string
  /**
   * Which bundled typeface to set the code in when no `font` is given:
   * `"mono"`, the default, or `"sans"`, the typeface of `text()`. The block
   * measures a proportional typeface glyph by glyph.
   */
  readonly family?: "mono" | "sans"
  /** The font size in world units. Defaults to 32. */
  readonly fontSize?: number
  /** Picks regular or bold (600 and above) of the default font. */
  readonly fontWeight?: number | "normal" | "bold"
  /** Line height as a multiple of the font size. Defaults to 1.5. */
  readonly lineHeight?: number
  /** Colours to use in place of the dark theme's, kind by kind. */
  readonly theme?: Partial<CodeTheme>
  /** Whether the caret is drawn. Defaults to true. */
  readonly caret?: boolean
  /** The resolution of each glyph in the atlas. Defaults to 128. */
  readonly sdfGlyphSize?: number
}

CodePiece

TypeScript
type CodePiece = THREE.Object3D & {
  opacity: number
}

CodeSelection

TypeScript
interface CodeSelection {
  /** The language to colour it as. Defaults to `"text"`. */
  readonly language?: CodeLanguage
  /**
   * The first and last line to show, counted from 1, both included. Without
   * it the whole source is shown.
   */
  readonly lines?: readonly [first: number, last: number]
  /** How many spaces a tab becomes. Defaults to 2. */
  readonly tabSize?: number
  /**
   * Whether the indentation the shown lines share is removed, so that a
   * range from deep inside a file starts at the left edge. Defaults to true.
   */
  readonly dedent?: boolean
}

CodeTarget

TypeScript
interface CodeTarget {
  /** The shown text. */
  readonly content: string
  /** The visible characters, each with its position in the content. */
  readonly chars: ReadonlyArray<
    CodePiece & {
      readonly index: number
    }
  >
  /** The visible tokens, in reading order. */
  readonly tokens: ReadonlyArray<CodePiece>
  /** The caret, whose origin is its top left corner. */
  readonly caret: THREE.Object3D
  /** The highlight band, whose origin is its top left corner. */
  readonly highlight: THREE.Object3D
  /** The width of one character. */
  readonly advance: number
  /** The caret's position before a character of the content. */
  caretAt(index: number): {
    x: number
    y: number
  }
  /** The band of a line, counted from 1. */
  lineBox(
    line: number,
    fit?: "block" | "line"
  ): {
    x: number
    y: number
    width: number
    height: number
  }
  /** The token with a text, or `undefined`. */
  token(content: string, nth?: number): CodePiece | undefined
  /** A token's kind, text and cell. */
  infoOf(part: object): CodeTokenInfo | undefined
  /** The texts that stand in for a token, placed on a cell. */
  replacement(
    contents: ReadonlyArray<string>,
    cell: {
      line: number
      column: number
      kind: TokenKind
    }
  ): CodeFrames
}

CodeTheme

TypeScript
interface CodeTheme extends Readonly<Record<TokenKind, string>> {
  /** The colour of the caret. */
  readonly caret: string
  /** The colour of the line highlight, which is drawn at a low opacity. */
  readonly highlight: string
}

CodeTokenInfo

TypeScript
interface CodeTokenInfo {
  /** What the token is. */
  readonly kind: TokenKind
  /** The token's text. A token that spans lines is one of these per line. */
  readonly text: string
  /** The token's line within the block, counted from 0. */
  readonly line: number
  /** The column of its first character, counted from 0. */
  readonly column: number
  /** Where it starts in the block's content, in UTF-16 units. */
  readonly start: number
  /** Where it ends in the block's content, exclusive. */
  readonly end: number
}

HighlightOptions

TypeScript
interface HighlightOptions extends MotionOptions {
  /**
   * How wide the band is: `"block"` for the width of the block, `"line"`
   * for the line's own text. Defaults to `"block"`.
   */
  readonly fit?: "block" | "line"
}

RhythmOptions

TypeScript
interface RhythmOptions {
  /**
   * The seed of the rhythm: the same seed types the same way. Defaults to 1.
   */
  readonly seed?: number
  /** Characters per second, on average. Defaults to 14. */
  readonly rate?: number
}

Token

TypeScript
interface Token {
  /** What the run is. White space is `"plain"`. */
  readonly kind: TokenKind
  /** The run's text, exactly as it is in the source. */
  readonly text: string
  /** Where the run starts in the source, in UTF-16 units. */
  readonly start: number
  /** Where the run ends in the source, exclusive. */
  readonly end: number
}

TokenKind

TypeScript
type TokenKind =
  | "keyword"
  | "string"
  | "number"
  | "comment"
  | "punctuation"
  | "identifier"
  | "function"
  | "property"
  | "operator"
  | "plain"

TypeOptions

TypeScript
interface TypeOptions extends RhythmOptions {
  /**
   * How long the typing takes in all. With it, the rhythm is kept and the
   * rate follows from the duration.
   */
  readonly duration?: number
  /** The position in the content to start at. Defaults to 0. */
  readonly from?: number
  /** The position to stop before. Defaults to the content's end. */
  readonly to?: number
  /** Seconds to wait before starting. */
  readonly delay?: number
}