textmode.js 0.2.1-beta.6 → 0.2.1-beta.8

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.
@@ -1,16 +1,38 @@
1
1
  import type { GLRenderer } from '../rendering/webgl/Renderer';
2
2
  /**
3
- * TextmodeImage represents an image source uploaded to a WebGL texture.
4
- * It can be drawn via the textmode image() API, which resizes into the MRT pipeline.
3
+ * Consolidated render data for TextmodeImage
4
+ */
5
+ export interface TextmodeImageRenderData {
6
+ texture: WebGLTexture;
7
+ invert: number;
8
+ flipX: number;
9
+ flipY: number;
10
+ charRotation: [number, number];
11
+ charColorFixed: boolean;
12
+ charColor: [number, number, number];
13
+ cellColorFixed: boolean;
14
+ cellColor: [number, number, number];
15
+ backgroundColor: [number, number, number, number];
16
+ charCount: number;
17
+ charList: number[][];
18
+ }
19
+ /**
20
+ * Represents an image uploaded for textmode rendering via {@link Textmodifier.loadImage}.
21
+ *
22
+ * It can be drawn to the canvas via {@link Textmodifier.image}.
23
+ *
24
+ * An image uploaded currently runs through an adjustable brightness-converter that converts
25
+ * the original image into a textmode representation using characters.
26
+ * Those adjustable options are available via chainable methods on this class.
5
27
  */
6
28
  export declare class TextmodeImage {
7
29
  /**
8
30
  * Underlying WebGL texture handle.
9
31
  */
10
- readonly texture: WebGLTexture;
32
+ private _texture;
11
33
  /** Original pixel dimensions of the source image. */
12
- readonly width: number;
13
- readonly height: number;
34
+ private _width;
35
+ private _height;
14
36
  private _gl;
15
37
  private _invert;
16
38
  private _flipX;
@@ -23,70 +45,117 @@ export declare class TextmodeImage {
23
45
  private _backgroundColor;
24
46
  private _glyphColors;
25
47
  private _glyphColorResolver;
48
+ /**
49
+ * Create a new TextmodeImage instance.
50
+ * @param gl WebGL2 rendering context
51
+ * @param texture WebGL texture containing the image
52
+ * @param width Original image width in pixels
53
+ * @param height Original image height in pixels
54
+ *
55
+ * @ignore
56
+ */
26
57
  constructor(gl: WebGL2RenderingContext, texture: WebGLTexture, width: number, height: number);
27
- /** Dispose of GPU resources. */
58
+ /**
59
+ * Dispose of GPU resources.
60
+ * @ignore
61
+ */
28
62
  $dispose(): void;
29
63
  /** Normalize boolean | number to 0/1 */
30
64
  private _to01;
31
65
  /**
32
- * Invert the image colors indicator flag to pass into MRT transform (stored in o_transform.r).
33
- * Does not modify the source texture pixels; downstream stages can respond accordingly.
66
+ * Set the invert flag.
67
+ *
68
+ * Setting this flag to `true` will swap the character and cell colors when rendering the image.
69
+ *
70
+ * @param v Flag value
71
+ * @returns This instance for chaining.
34
72
  */
35
73
  invert(v?: boolean | number): this;
36
74
  /**
37
- * Set horizontal flip indicator flag (stored in o_transform.g).
75
+ * Set horizontal flip indicator flag.
76
+ * @param v Flag value
77
+ * @returns This instance for chaining.
38
78
  */
39
79
  flipX(v?: boolean | number): this;
40
80
  /**
41
- * Set vertical flip indicator flag (stored in o_transform.b).
81
+ * Set vertical flip indicator flag.
82
+ * @param v Flag value
83
+ * @returns This instance for chaining.
42
84
  */
43
85
  flipY(v?: boolean | number): this;
44
86
  /**
45
- * Set character rotation in degrees, encoded into two channels as in RenderState.
87
+ * Set the character rotation in degrees.
88
+ * @param degrees Rotation in degrees *(0-360)*
89
+ * @returns This instance for chaining.
46
90
  */
47
91
  charRotation(degrees: number): this;
48
- /** Snapshot of flags for renderer uniform binding */
49
- get flags(): Readonly<{
50
- invert: number;
51
- flipX: number;
52
- flipY: number;
53
- }>;
54
- /** Encoded (r,g) rotation channels */
55
- get charRotationRG(): Readonly<[number, number]>;
56
- /** Set character color mode: 'sampled' (from image) or 'fixed' (use charColor) */
92
+ /**
93
+ * Get all render data needed by the GLRenderer.
94
+ * @ignore
95
+ */
96
+ $getRenderData(): TextmodeImageRenderData;
97
+ /**
98
+ * Set character color mode: `'sampled'` *(from image)* or `'fixed'` *(use {@link charColor})*.
99
+ * @param mode
100
+ * @returns
101
+ */
57
102
  charColorMode(mode: 'sampled' | 'fixed'): this;
58
- /** Set cell color mode: 'sampled' (from image) or 'fixed' (use cellColor) */
103
+ /**
104
+ * Set the cell color mode: `'sampled'` *(from image)* or `'fixed'` *(use {@link cellColor})*.
105
+ * @param mode
106
+ * @returns
107
+ */
59
108
  cellColorMode(mode: 'sampled' | 'fixed'): this;
60
- /** Set fixed character color; numbers are 0..255 like other APIs */
109
+ /**
110
+ * Defines the character color used when {@link charColorMode} is `'fixed'`.
111
+ * @param r Red channel (0-255)
112
+ * @param g Green channel (0-255)
113
+ * @param b Blue channel (0-255)
114
+ * @returns This instance for chaining.
115
+ */
61
116
  charColor(r: number, g?: number, b?: number): this;
62
- /** Set fixed cell color; numbers are 0..255 like other APIs */
117
+ /**
118
+ * Defines the cell color used when {@link cellColorMode} is `'fixed'`.
119
+ * @param r Red channel (0-255)
120
+ * @param g Green channel (0-255)
121
+ * @param b Blue channel (0-255)
122
+ * @returns This instance for chaining.
123
+ */
63
124
  cellColor(r: number, g?: number, b?: number): this;
64
- /** Set background color for transparent pixels; numbers are 0..255 like other APIs */
125
+ /**
126
+ * Set background color for transparent pixels.
127
+ * @param r Red channel (0-255)
128
+ * @param g Green channel (0-255)
129
+ * @param b Blue channel (0-255)
130
+ * @param a Alpha channel (0-255)
131
+ * @returns This instance for chaining.
132
+ */
65
133
  background(r: number, g?: number, b?: number, a?: number): this;
66
134
  /**
67
- * Define the characters to use for brightness mapping, as a string.
68
- * The string will be translated to glyph shader colors externally and stored here (max 64).
69
- * Note: In this core class we only accept the precomputed colors via a helper; the public method is wired through RenderingMixin.
135
+ * Define the characters to use for brightness mapping as a string.
136
+ *
137
+ * The maximum number of characters for brightness mapping currently is `64`; excess characters are ignored.
138
+ *
139
+ * @param chars
140
+ * @returns
70
141
  */
71
- charactersFromColors(colors: ([number, number, number] | null)[]): this;
72
- /** Convert a string to glyph colors using the resolver (from Textmodifier font) and store. */
73
142
  characters(chars: string): this;
74
- /** Internal: uniforms for character list */
75
- get characterListUniforms(): Readonly<{
76
- count: number;
77
- list: number[][];
78
- }>;
79
- /** Internal: snapshot of color uniforms for renderer */
80
- get colorUniforms(): Readonly<{
81
- charColorFixed: boolean;
82
- _charColor: [number, number, number];
83
- cellColorFixed: boolean;
84
- cellColor: [number, number, number];
85
- backgroundColor: [number, number, number, number];
86
- }>;
87
143
  /**
88
144
  * Create a TextmodeImage from an HTML image/video/canvas element.
89
145
  * Texture parameters use NEAREST and CLAMP to align with grid sampling.
146
+ * @ignore
147
+ */
148
+ static $fromSource(renderer: GLRenderer, source: HTMLImageElement | HTMLCanvasElement | HTMLVideoElement, glyphResolver: (s: string) => ([number, number, number])[]): TextmodeImage;
149
+ /**
150
+ * WebGL texture handle containing the original source image.
151
+ */
152
+ get texture(): WebGLTexture;
153
+ /**
154
+ * Original pixel width of the source image.
155
+ */
156
+ get width(): number;
157
+ /**
158
+ * Original pixel height of the source image.
90
159
  */
91
- static fromSource(renderer: GLRenderer, source: HTMLImageElement | HTMLCanvasElement | HTMLVideoElement, glyphResolver: (s: string) => ([number, number, number])[]): TextmodeImage;
160
+ get height(): number;
92
161
  }
@@ -18,7 +18,13 @@ import type { GLFramebuffer, Shader } from '../rendering';
18
18
  * Options for creating a {@link Textmodifier} instance.
19
19
  */
20
20
  export type TextmodeOptions = {
21
- /** An existing [HTMLCanvasElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement) to use instead of creating a new one. */
21
+ /**
22
+ * An existing [HTMLCanvasElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement) to use instead of creating a new one.
23
+ *
24
+ * **Note:**
25
+ * If using `overlay` mode, this should be the target canvas or video element to overlay on.
26
+ * `textmode.js` will create its own canvas applied on top of the target element, always matching its size and position.
27
+ */
22
28
  canvas?: HTMLCanvasElement;
23
29
  /** The font size to use for text rendering. Defaults to 16. */
24
30
  fontSize?: number;
@@ -38,13 +44,16 @@ export type TextmodeOptions = {
38
44
  fontSource?: string;
39
45
  /**
40
46
  * Use `textmode.js` in overlay mode,
41
- * which sets up the textmode <canvas> on top of an existing HTMLCanvasElement or HTMLVideoElement,
47
+ * which sets up the textmode `<canvas>` on top of an existing HTMLCanvasElement or HTMLVideoElement,
42
48
  * automatically resizing and positioning it to match the target element.
43
49
  *
44
50
  * In this mode `textmode.js` fetches the content of the target element and applies it with adjustable textmode conversion
45
51
  * as a first layer to the textmode canvas.
46
52
  *
47
53
  * Useful for applying textmode conversion to p5.js sketches, YouTube videos, and sooo much more.
54
+ *
55
+ * All functionality of `textmode.js` remains available, including drawing additional content on top of the converted source.
56
+ *
48
57
  */
49
58
  overlay?: boolean;
50
59
  };
@@ -233,6 +242,10 @@ export declare class Textmodifier extends Textmodifier_base {
233
242
  get canvas(): HTMLCanvasElement;
234
243
  /** Check if the instance has been disposed/destroyed. */
235
244
  get isDisposed(): boolean;
245
+ /**
246
+ * If in overlay mode, returns the {@link TextmodeImage} instance capturing the target canvas/video content,
247
+ * allowing further configuration of the conversion parameters.
248
+ */
236
249
  get overlay(): TextmodeImage | undefined;
237
250
  }
238
251
  export interface Textmodifier extends RenderingCapabilities, ExportCapabilities, FontCapabilities, AnimationCapabilities, MouseCapabilities, KeyboardCapabilities {
@@ -19,9 +19,6 @@ export interface ExportCapabilities {
19
19
  * // Create a Textmodifier instance
20
20
  * const textmodifier = textmode.create(canvas, {renderMode: 'manual'});
21
21
  *
22
- * // Render a single frame
23
- * textmodifier.render();
24
- *
25
22
  * // Get the current rendering as a text string
26
23
  * const textString = textmodifier.toString({
27
24
  * preserveTrailingSpaces: false,
@@ -45,9 +42,6 @@ export interface ExportCapabilities {
45
42
  * // Create a Textmodifier instance
46
43
  * const textmodifier = textmode.create(canvas, {renderMode: 'manual'});
47
44
  *
48
- * // Render a single frame
49
- * textmodifier.render();
50
- *
51
45
  * // Export the current rendering to a TXT file
52
46
  * textmodifier.saveStrings({
53
47
  * filename: 'my_textmode_rendering',
@@ -69,9 +63,6 @@ export interface ExportCapabilities {
69
63
  * // Create a Textmodifier instance
70
64
  * const textmodifier = textmode.create(canvas, {renderMode: 'manual'});
71
65
  *
72
- * // Render a single frame
73
- * textmodifier.render();
74
- *
75
66
  * // Get the current rendering as an SVG string
76
67
  * const svgString = textmodifier.toSVG({
77
68
  * includeBackgroundRectangles: true,
@@ -95,9 +86,6 @@ export interface ExportCapabilities {
95
86
  * // Create a Textmodifier instance
96
87
  * const textmodifier = textmode.create(canvas, {renderMode: 'manual'});
97
88
  *
98
- * // Render a single frame
99
- * textmodifier.render();
100
- *
101
89
  * // Export the current rendering to an SVG file
102
90
  * textmodifier.saveSVG({
103
91
  * filename: 'my_textmode_rendering',
@@ -117,14 +105,13 @@ export interface ExportCapabilities {
117
105
  * // Create a Textmodifier instance
118
106
  * const textmodifier = textmode.create(canvas, {renderMode: 'manual'});
119
107
  *
120
- * // Render a single frame
121
- * textmodifier.render();
122
- *
123
- * // Export the current rendering to a PNG file
124
- * textmodifier.saveCanvas('my_textmode_rendering', 'png');
108
+ * // Export the current rendering to a PNG file *(default)*
109
+ * textmodifier.saveCanvas();
125
110
  *
126
111
  * // Export with custom options
127
- * textmodifier.saveCanvas('my_textmode_rendering', 'jpg', {
112
+ * textmodifier.saveCanvas({
113
+ * filename: 'my_textmode_rendering',
114
+ * format: 'jpg',
128
115
  * quality: 0.8,
129
116
  * scale: 2.0,
130
117
  * backgroundColor: 'white'
@@ -21,7 +21,6 @@ type UniformValue = number | boolean | number[] | Float32Array | Int32Array | We
21
21
  export interface RenderingCapabilities {
22
22
  /**
23
23
  * Set a custom shader for subsequent rendering operations.
24
- * Pass null to return to the default shader.
25
24
  * @param shader The custom shader to use
26
25
  *
27
26
  * @example
@@ -43,9 +42,6 @@ export interface RenderingCapabilities {
43
42
  * t.shader(customShader);
44
43
  * t.setUniform('u_frameCount', t.frameCount);
45
44
  * t.rect(0, 0, t.grid.cols, t.grid.rows);
46
- *
47
- * // Return to default shader
48
- * t.shader(null);
49
45
  * });
50
46
  * ```
51
47
  */
@@ -89,18 +85,13 @@ export interface RenderingCapabilities {
89
85
  */
90
86
  createFramebuffer(options: TextmodeFramebufferOptions): GLFramebuffer;
91
87
  /**
92
- * Render a framebuffer to the current render target.
93
- *
94
- * This method takes the 5 MRT attachments from the source framebuffer
95
- * and renders them at the specified position.
96
- * The framebuffer content will be processed through the same rendering
97
- * pipeline as normal drawing operations.
98
- *
99
- * @param framebuffer The framebuffer to render
100
- * @param x X position on the grid where to place the framebuffer content *(top-left corner)*
101
- * @param y Y position on the grid where to place the framebuffer content *(top-left corner)*
102
- * @param width Optional width to scale the framebuffer content *(defaults to framebuffer width)*
103
- * @param height Optional height to scale the framebuffer content *(defaults to framebuffer height)*
88
+ * Draw a TextmodeFramebuffer or TextmodeImage to the current render target.
89
+ *
90
+ * @param source The TextmodeFramebuffer or TextmodeImage to render
91
+ * @param x X position on the grid where to place the content *(top-left corner)*
92
+ * @param y Y position on the grid where to place the content *(top-left corner)*
93
+ * @param width Width to scale the content
94
+ * @param height Height to scale the content
104
95
  *
105
96
  * @example
106
97
  * ```javascript
@@ -42,6 +42,10 @@ export interface TextmodifierContext {
42
42
  readonly _textmodeDrawFramebuffer: GLFramebuffer;
43
43
  /** Shader used for converting pixels to textmode grid format @ignore */
44
44
  readonly _textmodeConversionShader: Shader;
45
+ /** Framebuffer used for textmode conversion @ignore */
46
+ readonly _asciiColorFramebuffer: GLFramebuffer;
47
+ /** Shader used for final presentation to the screen @ignore */
48
+ readonly _presentShader: Shader;
45
49
  /** Main render method @ignore */
46
50
  $render(): void;
47
51
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "textmode.js",
3
- "version": "0.2.1-beta.6",
3
+ "version": "0.2.1-beta.8",
4
4
  "description": "textmode.js is a lightweight creative coding library for creating real-time ASCII art on the web.",
5
5
  "type": "module",
6
6
  "types": "./dist/types/index.d.ts",
@@ -1,14 +0,0 @@
1
- export interface ShaderData {
2
- sourceCode: string;
3
- originalCode?: string;
4
- uniforms: Record<string, {
5
- variableName: string;
6
- variableType: string;
7
- }>;
8
- consts: Record<string, {
9
- variableName: string;
10
- variableType: string;
11
- }>;
12
- }
13
- declare const shaderData: ShaderData;
14
- export default shaderData;
@@ -1,14 +0,0 @@
1
- export interface ShaderData {
2
- sourceCode: string;
3
- originalCode?: string;
4
- uniforms: Record<string, {
5
- variableName: string;
6
- variableType: string;
7
- }>;
8
- consts: Record<string, {
9
- variableName: string;
10
- variableType: string;
11
- }>;
12
- }
13
- declare const shaderData: ShaderData;
14
- export default shaderData;
@@ -1,14 +0,0 @@
1
- export interface ShaderData {
2
- sourceCode: string;
3
- originalCode?: string;
4
- uniforms: Record<string, {
5
- variableName: string;
6
- variableType: string;
7
- }>;
8
- consts: Record<string, {
9
- variableName: string;
10
- variableType: string;
11
- }>;
12
- }
13
- declare const shaderData: ShaderData;
14
- export default shaderData;
@@ -1,14 +0,0 @@
1
- export interface ShaderData {
2
- sourceCode: string;
3
- originalCode?: string;
4
- uniforms: Record<string, {
5
- variableName: string;
6
- variableType: string;
7
- }>;
8
- consts: Record<string, {
9
- variableName: string;
10
- variableType: string;
11
- }>;
12
- }
13
- declare const shaderData: ShaderData;
14
- export default shaderData;
@@ -1,14 +0,0 @@
1
- export interface ShaderData {
2
- sourceCode: string;
3
- originalCode?: string;
4
- uniforms: Record<string, {
5
- variableName: string;
6
- variableType: string;
7
- }>;
8
- consts: Record<string, {
9
- variableName: string;
10
- variableType: string;
11
- }>;
12
- }
13
- declare const shaderData: ShaderData;
14
- export default shaderData;
@@ -1,7 +0,0 @@
1
- export { default as frag_ascii } from './frag/ascii.ts';
2
- export { default as frag_copy_mrt } from './frag/copy-mrt.ts';
3
- export { default as frag_image_to_mrt } from './frag/image-to-mrt.ts';
4
- export { default as frag_instanced_ascii_mrt } from './frag/instanced-ascii-mrt.ts';
5
- export { default as frag_present } from './frag/present.ts';
6
- export { default as vert_instanced_ascii_mrt } from './vert/instanced-ascii-mrt.ts';
7
- export { default as vert_shader } from './vert/shader.ts';
@@ -1,14 +0,0 @@
1
- export interface ShaderData {
2
- sourceCode: string;
3
- originalCode?: string;
4
- uniforms: Record<string, {
5
- variableName: string;
6
- variableType: string;
7
- }>;
8
- consts: Record<string, {
9
- variableName: string;
10
- variableType: string;
11
- }>;
12
- }
13
- declare const shaderData: ShaderData;
14
- export default shaderData;
@@ -1,14 +0,0 @@
1
- export interface ShaderData {
2
- sourceCode: string;
3
- originalCode?: string;
4
- uniforms: Record<string, {
5
- variableName: string;
6
- variableType: string;
7
- }>;
8
- consts: Record<string, {
9
- variableName: string;
10
- variableType: string;
11
- }>;
12
- }
13
- declare const shaderData: ShaderData;
14
- export default shaderData;