OptimizedBuffer
The OptimizedBuffer class (also known as FrameBuffer) provides low-level access to terminal rendering. It manages a grid of cells, each containing a character, foreground color, background color, and text attributes.
Creating Buffers
Create a new framebufferstatic create(
width: number,
height: number,
widthMethod: WidthMethod,
options?: { respectAlpha?: boolean; id?: string }
): OptimizedBuffer
widthMethod
'unicode' | 'wcwidth' | 'no_zwj'
required
Character width calculation method
Whether to respect alpha channel when compositing. Default: false
Optional identifier for debugging
Properties
Character width calculation method used by this buffer
Whether alpha blending is enabled
Native pointer to the underlying buffer (for advanced use)
Direct access to raw buffer data{
char: Uint32Array // Unicode codepoints
fg: Float32Array // Foreground colors (RGBA, 4 floats per cell)
bg: Float32Array // Background colors (RGBA, 4 floats per cell)
attributes: Uint32Array // Text attributes (bold, italic, etc.)
}
Drawing Methods
Clear the entire buffer with a background colorBackground color. Default: RGBA.fromValues(0, 0, 0, 1)
Set a single cell with no alpha blendingsetCell(
x: number,
y: number,
char: string,
fg: RGBA,
bg: RGBA,
attributes?: number
): void
Character to display (first codepoint used)
Text attributes (bold, italic, underline, etc.). Default: 0
Set a single cell with alpha blendingsetCellWithAlphaBlending(
x: number,
y: number,
char: string,
fg: RGBA,
bg: RGBA,
attributes?: number
): void
Same parameters as setCell, but blends colors using alpha channel.
Draw text at a position with optional selectiondrawText(
text: string,
x: number,
y: number,
fg: RGBA,
bg?: RGBA,
attributes?: number,
selection?: {
start: number
end: number
bgColor?: RGBA
fgColor?: RGBA
}
): void
Text attributes. Default: 0
Optional selection range to highlight
Draw a single character by codepointdrawChar(
char: number,
x: number,
y: number,
fg: RGBA,
bg: RGBA,
attributes?: number
): void
Fill a rectangular area with a background colorfillRect(
x: number,
y: number,
width: number,
height: number,
bg: RGBA
): void
Draw a bordered box with optional titledrawBox(options: {
x: number
y: number
width: number
height: number
borderStyle?: BorderStyle
customBorderChars?: Uint32Array
border: boolean | BorderSides[]
borderColor: RGBA
backgroundColor: RGBA
shouldFill?: boolean
title?: string
titleAlignment?: "left" | "center" | "right"
}): void
borderStyle
'single' | 'double' | 'rounded' | 'thick' | 'dashed'
Border style preset
border
boolean | BorderSides[]
required
Which sides to draw: true for all sides, or array like ["top", "left"]
titleAlignment
'left' | 'center' | 'right'
Title text alignment. Default: "left"
Draw a grid with custom column/row offsetsdrawGrid(options: {
borderChars: Uint32Array
borderFg: RGBA
borderBg: RGBA
columnOffsets: Int32Array
rowOffsets: Int32Array
drawInner: boolean
drawOuter: boolean
}): void
Compositing
Composite another framebuffer onto this onedrawFrameBuffer(
destX: number,
destY: number,
frameBuffer: OptimizedBuffer,
sourceX?: number,
sourceY?: number,
sourceWidth?: number,
sourceHeight?: number
): void
Source buffer to composite
Draw a TextBufferViewdrawTextBuffer(textBufferView: TextBufferView, x: number, y: number): void
Draw an EditorViewdrawEditorView(editorView: EditorView, x: number, y: number): void
Image Rendering
Draw pixel data with supersamplingdrawSuperSampleBuffer(
x: number,
y: number,
pixelDataPtr: Pointer,
pixelDataLength: number,
format: "bgra8unorm" | "rgba8unorm",
alignedBytesPerRow: number
): void
Draw grayscale intensity datadrawGrayscaleBuffer(
posX: number,
posY: number,
intensities: Float32Array,
srcWidth: number,
srcHeight: number,
fg?: RGBA | null,
bg?: RGBA | null
): void
drawGrayscaleBufferSupersampled
Draw grayscale data with supersampling for smoother renderingdrawGrayscaleBufferSupersampled(
posX: number,
posY: number,
intensities: Float32Array,
srcWidth: number,
srcHeight: number,
fg?: RGBA | null,
bg?: RGBA | null
): void
Clipping and Opacity
Push a clipping rectangle onto the stackpushScissorRect(x: number, y: number, width: number, height: number): void
Pop the top clipping rectangle from the stack
Remove all clipping rectanglesclearScissorRects(): void
Push an opacity value onto the stack (multiplies with existing opacity)pushOpacity(opacity: number): void
Opacity value between 0.0 and 1.0
Pop the top opacity value from the stack
Get the current effective opacitygetCurrentOpacity(): number
Utilities
Resize the buffer (clears content)resize(width: number, height: number): void
Enable or disable alpha blendingsetRespectAlpha(respectAlpha: boolean): void
Get the buffer’s native identifier
Get the resolved character data as UTF-8 bytesgetRealCharBytes(addLineBreaks?: boolean): Uint8Array
Whether to add newlines between rows. Default: false
Extract buffer content as styled text spansgetSpanLines(): CapturedLine[]
Returns an array of lines, each containing spans with text, colors, and attributes.
Destroy the buffer and free native resources
Example
import { OptimizedBuffer, RGBA } from "@opentui/core"
// Create a 80x24 buffer
const buffer = OptimizedBuffer.create(80, 24, "unicode", {
respectAlpha: true,
id: "main-buffer"
})
// Clear with dark background
buffer.clear(RGBA.fromValues(0.1, 0.1, 0.1, 1))
// Draw text
buffer.drawText(
"Hello, World!",
5, 10,
RGBA.fromHex("#00FF00"),
RGBA.fromValues(0, 0, 0, 0)
)
// Draw a box
buffer.drawBox({
x: 10,
y: 5,
width: 40,
height: 10,
border: true,
borderStyle: "rounded",
borderColor: RGBA.fromHex("#FFFFFF"),
backgroundColor: RGBA.fromValues(0.2, 0.2, 0.3, 1),
title: "My Box",
titleAlignment: "center"
})
// Use clipping
buffer.pushScissorRect(0, 0, 40, 12)
buffer.drawText("Clipped text", 0, 0, RGBA.fromHex("#FF0000"))
buffer.popScissorRect()
// Clean up when done
buffer.destroy()