textmode.js 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Rectangle geometry class for WebGL rendering
3
+ */
4
+ export declare class Rectangle {
5
+ /** The WebGL rendering context */
6
+ private gl;
7
+ /** The buffer containing the rectangle vertices */
8
+ private buffer;
9
+ /** The number of vertices in the rectangle */
10
+ private numVertices;
11
+ constructor(gl: WebGLRenderingContext, x: number, y: number, width: number, height: number);
12
+ /**
13
+ * Draw the quad using attribute locations from the current shader
14
+ */
15
+ draw(): void;
16
+ }
@@ -0,0 +1,16 @@
1
+ export declare class TextmodeCanvas {
2
+ private webglCanvas;
3
+ private captureCanvas;
4
+ constructor(captureCanvas: HTMLCanvasElement);
5
+ private generateUniqueCanvasId;
6
+ private createOverlayCanvas;
7
+ private positionOverlayCanvas;
8
+ resize(): void;
9
+ /**
10
+ * Get the WebGL context for the overlay canvas
11
+ */
12
+ getWebGLContext(): WebGL2RenderingContext | WebGLRenderingContext;
13
+ get canvas(): HTMLCanvasElement;
14
+ get width(): number;
15
+ get height(): number;
16
+ }
@@ -0,0 +1,111 @@
1
+ /**
2
+ * Manages the grid for the ASCII rendering pipeline of a Textmodifier instance.
3
+ */
4
+ export declare class TextmodeGrid {
5
+ /** The number of columns in the grid. */
6
+ private _cols;
7
+ /** The number of rows in the grid. */
8
+ private _rows;
9
+ /** The total width of the grid in pixels. */
10
+ private _width;
11
+ /** The total height of the grid in pixels. */
12
+ private _height;
13
+ /** The offset to the outer canvas on the x-axis when centering the grid. */
14
+ private _offsetX;
15
+ /** The offset to the outer canvas on the y-axis when centering the grid. */
16
+ private _offsetY;
17
+ /** Whether the grid dimensions are fixed, or responsive based on the canvas dimensions. */
18
+ private _fixedDimensions;
19
+ /** The canvas element used to determine the grid dimensions. */
20
+ private _canvas;
21
+ /** The width of each cell in the grid. */
22
+ private _cellWidth;
23
+ /** The height of each cell in the grid. */
24
+ private _cellHeight;
25
+ /**
26
+ * Create a new grid instance.
27
+ * @param canvas The canvas element used to determine the grid dimensions.
28
+ * @param cellWidth The width of each cell in the grid.
29
+ * @param cellHeight The height of each cell in the grid.
30
+ * @ignore
31
+ */
32
+ constructor(canvas: HTMLCanvasElement, cellWidth: number, cellHeight: number);
33
+ /**
34
+ * Reset the grid to the default number of columns and rows based on the current canvas dimensions, and the grid cell dimensions.
35
+ * @ignore
36
+ */
37
+ reset(): void;
38
+ /**
39
+ * Reset the total grid width & height, and the offset to the outer canvas.
40
+ */
41
+ private _resizeGrid;
42
+ /**
43
+ * Re-assign the grid cell dimensions and `reset()` the grid.
44
+ * @param newCellWidth The new cell width.
45
+ * @param newCellHeight The new cell height.
46
+ * @ignore
47
+ */
48
+ resizeCellPixelDimensions(newCellWidth: number, newCellHeight: number): void;
49
+ /**
50
+ * Re-assign the grid dimensions and resize the grid.
51
+ *
52
+ * Calling this method makes the grid dimensions fixed, meaning they will not automatically resize based on the canvas dimensions.
53
+ * @param newCols The new number of columns.
54
+ * @param newRows The new number of rows.
55
+ * @ignore
56
+ */
57
+ resizeGridDimensions(newCols: number, newRows: number): void;
58
+ /**
59
+ * Make the grid dimensions flexible again, and `reset()` the grid.
60
+ * @ignore
61
+ */
62
+ resetGridDimensions(): void;
63
+ /**
64
+ * Update the canvas used by the grid, and reset the grid dimensions.
65
+ * @param canvas The new canvas element to use for the grid.
66
+ * @ignore
67
+ */
68
+ updateCanvas(canvas: HTMLCanvasElement): void;
69
+ /**
70
+ * Returns the width of each cell in the grid.
71
+ */
72
+ get cellWidth(): number;
73
+ /**
74
+ * Returns the height of each cell in the grid.
75
+ */
76
+ get cellHeight(): number;
77
+ /**
78
+ * Returns the number of columns in the grid.
79
+ */
80
+ get cols(): number;
81
+ /**
82
+ * Returns the number of rows in the grid.
83
+ */
84
+ get rows(): number;
85
+ /**
86
+ * Returns the total width of the grid.
87
+ */
88
+ get width(): number;
89
+ /**
90
+ * Returns the total height of the grid.
91
+ */
92
+ get height(): number;
93
+ /**
94
+ * Returns the offset to the outer canvas borders on the x-axis when centering the grid.
95
+ */
96
+ get offsetX(): number;
97
+ /**
98
+ * Returns the offset to the outer canvas borders on the y-axis when centering the grid.
99
+ */
100
+ get offsetY(): number;
101
+ /**
102
+ * Returns `true` if the grid dimensions *(columns and rows)* are fixed, or `false` if they are responsive based on the canvas dimensions.
103
+ */
104
+ get fixedDimensions(): boolean;
105
+ /**
106
+ * Sets whether the grid dimensions *(columns and rows)* are fixed or responsive based on the canvas dimensions.
107
+ * @param value `true` to make the grid dimensions fixed, or `false` to make them responsive.
108
+ * @ignore
109
+ */
110
+ set fixedDimensions(value: boolean);
111
+ }
@@ -0,0 +1,37 @@
1
+ export interface TextModeOptions {
2
+ fontSize?: number;
3
+ }
4
+ export type TextmodifierInitializationState = 'pending' | 'initialized' | 'failed';
5
+ export declare class Textmodifier {
6
+ /** The canvas element to capture content from */
7
+ private captureCanvas;
8
+ /** Our WebGL overlay canvas manager */
9
+ private textmodeCanvas;
10
+ /** Core WebGL renderer */
11
+ private renderer;
12
+ private asciiShader;
13
+ private canvasFramebuffer;
14
+ private brightnessConverter;
15
+ private fontManager;
16
+ private grid;
17
+ private resizeObserver;
18
+ private resultFramebuffer;
19
+ private initialized;
20
+ constructor(canvas: HTMLCanvasElement, opts?: TextModeOptions);
21
+ /**
22
+ * Initialize the textmodifier. Must be called before using render().
23
+ */
24
+ initialize(): Promise<void>;
25
+ /**
26
+ * Check if the textmodifier is ready for use
27
+ */
28
+ isInitialized(): boolean;
29
+ /**
30
+ * Static factory method for creating and initializing a Textmodifier instance
31
+ */
32
+ static create(canvas: HTMLCanvasElement, opts?: TextModeOptions): Promise<Textmodifier>;
33
+ private setupEventListeners;
34
+ loadFont(fontUrl: string): Promise<void>;
35
+ render(): void;
36
+ private resize;
37
+ }
@@ -0,0 +1,50 @@
1
+ import { Renderer } from "../../renderer/Renderer";
2
+ import { Framebuffer } from "../../renderer/Framebuffer";
3
+ import { ColorPalette } from "../../ColorPalette";
4
+ import { FontManager } from "../../FontManager";
5
+ import { TextmodeGrid } from "../Grid";
6
+ import { Converter } from "./Converter";
7
+ export declare const BRIGHTNESS_DEFAULT_OPTIONS: {
8
+ /** Enable/disable the renderer */
9
+ enabled: boolean;
10
+ /** Characters used for brightness mapping (from darkest to brightest) */
11
+ characters: string;
12
+ /** Color of the ASCII characters. Only used when `characterColorMode` is set to `fixed` */
13
+ characterColor: number[];
14
+ /** Character color mode */
15
+ characterColorMode: string;
16
+ /** Cell background color. Only used when `characterColorMode` is set to `fixed` */
17
+ backgroundColor: number[];
18
+ /** Background color mode */
19
+ backgroundColorMode: string;
20
+ /** Swap the cells ASCII character colors with it's cell background colors */
21
+ invert: boolean;
22
+ /** Rotation angle of all characters in the grid in degrees */
23
+ rotation: number;
24
+ /** Flip the ASCII characters horizontally */
25
+ flipHorizontally: boolean;
26
+ /** Flip the ASCII characters vertically */
27
+ flipVertically: boolean;
28
+ /** Range of brightness values to map to ASCII characters */
29
+ brightnessRange: [number, number];
30
+ };
31
+ export declare class BrightnessConverter extends Converter {
32
+ private sampleShader;
33
+ private charMappingShader;
34
+ sampleFramebuffer: Framebuffer;
35
+ palette: ColorPalette;
36
+ private options;
37
+ constructor(renderer: Renderer, fontManager: FontManager, grid: TextmodeGrid);
38
+ convert(framebuffer: Framebuffer): void;
39
+ resize(): void;
40
+ characters(characters: string): void;
41
+ characterColor(r: number, g?: number, b?: number, a?: number): void;
42
+ characterColorMode(mode: "sampled" | "fixed"): void;
43
+ backgroundColor(r: number, g?: number, b?: number, a?: number): void;
44
+ backgroundColorMode(mode: "sampled" | "fixed"): void;
45
+ invert(invert: boolean): void;
46
+ rotation(angle: number): void;
47
+ flipHorizontally(flip: boolean): void;
48
+ flipVertically(flip: boolean): void;
49
+ brightnessRange(range: [number, number]): void;
50
+ }
@@ -0,0 +1,21 @@
1
+ import type { FontManager } from "../../FontManager";
2
+ import type { Framebuffer } from "../../renderer/Framebuffer";
3
+ import type { Renderer } from "../../renderer/Renderer";
4
+ import type { TextmodeGrid } from "../Grid";
5
+ export declare class Converter {
6
+ protected renderer: Renderer;
7
+ protected fontManager: FontManager;
8
+ protected grid: TextmodeGrid;
9
+ protected _characterFramebuffer: Framebuffer;
10
+ protected _primaryColorFramebuffer: Framebuffer;
11
+ protected _secondaryColorFramebuffer: Framebuffer;
12
+ protected _rotationFramebuffer: Framebuffer;
13
+ protected _transformFramebuffer: Framebuffer;
14
+ constructor(renderer: Renderer, fontManager: FontManager, grid: TextmodeGrid);
15
+ resize(): void;
16
+ get characterFramebuffer(): Framebuffer;
17
+ get primaryColorFramebuffer(): Framebuffer;
18
+ get secondaryColorFramebuffer(): Framebuffer;
19
+ get rotationFramebuffer(): Framebuffer;
20
+ get transformFramebuffer(): Framebuffer;
21
+ }
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "textmode.js",
3
+ "version": "0.0.1",
4
+ "description": "Apply real-time ASCII conversion to any HTML canvas.",
5
+ "type": "module",
6
+ "types": "./dist/types/index.d.ts",
7
+ "main": "./dist/textmode.umd.js",
8
+ "module": "./dist/textmode.esm.js",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/types/index.d.ts",
12
+ "import": "./dist/textmode.esm.js",
13
+ "require": "./dist/textmode.umd.js"
14
+ }
15
+ },
16
+ "scripts": {
17
+ "dev": "vite",
18
+ "build": "vite build && tsc",
19
+ "preview": "vite preview"
20
+ },
21
+ "devDependencies": {
22
+ "@types/node": "^24.0.13",
23
+ "@types/p5": "^1.7.6",
24
+ "@vitest/ui": "^3.1.2",
25
+ "tslib": "^2.8.1",
26
+ "typescript": "^5.8.3",
27
+ "vite": "^6.3.4",
28
+ "vite-plugin-glsl": "^1.5.1",
29
+ "vitest": "^3.1.2"
30
+ },
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "git+https://github.com/humanbydefinition/textmode.js.git"
34
+ },
35
+ "keywords": [
36
+ "ascii",
37
+ "petscii",
38
+ "textmode",
39
+ "canvas",
40
+ "webgl"
41
+ ],
42
+ "author": "humanbydefinition",
43
+ "license": "MIT",
44
+ "bugs": {
45
+ "url": "https://github.com/humanbydefinition/textmode.js/issues"
46
+ },
47
+ "homepage": "https://github.com/humanbydefinition/textmode.js",
48
+ "files": [
49
+ "dist"
50
+ ]
51
+ }