@teaui/core
Version:
A high-level terminal UI library for Node
49 lines (48 loc) • 2.08 kB
TypeScript
import type { Viewport } from '../Viewport.js';
import type { Props as ViewProps } from '../View.js';
import { View } from '../View.js';
import { Size } from '../geometry.js';
interface Props extends ViewProps {
/**
* Called during render after the pixel buffer has been sized. Use this to draw
* shapes that depend on the canvas dimensions (pixelWidth/pixelHeight).
* The canvas is cleared before each call.
*/
draw?: (canvas: Canvas) => void;
}
export declare class Canvas extends View {
#private;
constructor(props?: Props);
update(props: Props): void;
get pixelWidth(): number;
get pixelHeight(): number;
naturalSize(available: Size): Size;
/**
* Render the canvas into the viewport. If a `draw` callback is set, the pixel
* buffer is cleared and the callback is invoked after sizing. External callers
* (e.g. LineChart) can also use `withContext` to draw into the canvas at a
* specific size without needing a full render cycle.
*/
render(viewport: Viewport): void;
/**
* Size the pixel buffer, clear it, and invoke the callback. This is the safe way
* to draw into a Canvas programmatically — the buffer is guaranteed to be sized
* before any drawing occurs.
*
* @param cols Terminal columns (pixelWidth will be cols * 2)
* @param rows Terminal rows (pixelHeight will be rows * 4)
* @param fn Drawing function — call set/line/rect/circle/etc. on the canvas
*/
withContext(cols: number, rows: number, fn: (canvas: Canvas) => void): void;
set(px: number, py: number): void;
unset(px: number, py: number): void;
toggle(px: number, py: number): void;
isSet(px: number, py: number): boolean;
clear(): void;
line(x0: number, y0: number, x1: number, y1: number): void;
rect(x: number, y: number, w: number, h: number): void;
fillRect(x: number, y: number, w: number, h: number): void;
circle(cx: number, cy: number, r: number): void;
fillCircle(cx: number, cy: number, r: number): void;
}
export {};