Skip to content

textmode.js / loadables / TextmodeImage

Class: TextmodeImage

Represents an image uploaded for textmode rendering via Textmodifier.loadImage.

It can be drawn to the canvas via Textmodifier.image.

An image uploaded currently runs through an adjustable brightness-converter that converts the original image into a textmode representation using characters. Those adjustable options are available via chainable methods on this class.

Example

javascript
const t = textmode.create({
    width: 800,
    height: 600,
});

let img;

t.setup(async () => {
    img = await t.loadImage('https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=800&q=80');
    img.characters(" .:-=+*#%@");
    // ... other adjustments like img.flipX(boolean), img.cellColorMode('sampled' | 'fixed'), etc.
    // (can also be chained or updated during runtime)
});

t.draw(() => {
    t.background(0);

    if (img) {
        // Draw the loaded image
        t.image(img);
    }
});

Extends

  • TextmodeSource

Accessors

height

Get Signature

ts
get height(): number;

Ideal height in grid cells.

Returns

number

Inherited from

ts
TextmodeSource.height

originalHeight

Get Signature

ts
get originalHeight(): number;

Original pixel height.

Returns

number

Inherited from

ts
TextmodeSource.originalHeight

originalWidth

Get Signature

ts
get originalWidth(): number;

Original pixel width.

Returns

number

Inherited from

ts
TextmodeSource.originalWidth

texture

Get Signature

ts
get texture(): WebGLTexture;

Return the WebGL texture currently backing this source.

Returns

WebGLTexture

Inherited from

ts
TextmodeSource.texture

width

Get Signature

ts
get width(): number;

Ideal width in grid cells.

Returns

number

Inherited from

ts
TextmodeSource.width

Methods

background()

ts
background(
   colorOrGray, 
   g?, 
   b?, 
   a?): this;

Defines the background color used for transparent pixels.

Parameters

ParameterTypeDescription
colorOrGray| string | number | TextmodeColorA grayscale value (0-255), hex string ('#RGB', '#RRGGBB', '#RRGGBBAA'), or TextmodeColor instance
g?numberGreen component (0-255) if using RGB format
b?numberBlue component (0-255) if using RGB format
a?numberAlpha component (0-255) if using RGBA format

Returns

this

This instance for chaining.

Inherited from

ts
TextmodeSource.background

cellColor()

ts
cellColor(
   colorOrGray, 
   g?, 
   b?, 
   a?): this;

Defines the cell color when cellColorMode is 'fixed'.

Parameters

ParameterTypeDescription
colorOrGray| string | number | TextmodeColorA grayscale value (0-255), hex string ('#RGB', '#RRGGBB', '#RRGGBBAA'), or TextmodeColor instance
g?numberGreen component (0-255) if using RGB format
b?numberBlue component (0-255) if using RGB format
a?numberAlpha component (0-255) if using RGBA format

Returns

this

This instance for chaining.

Inherited from

ts
TextmodeSource.cellColor

cellColorMode()

ts
cellColorMode(mode): this;

Set cell color mode: 'sampled' (from source) or 'fixed'.

Parameters

ParameterTypeDescription
mode"sampled" | "fixed"The cell color mode

Returns

this

This instance for chaining.

Inherited from

ts
TextmodeSource.cellColorMode

characters()

ts
characters(chars): this;

Define the characters to use for brightness mapping as a string. Maximum length is 255; excess characters are ignored.

Parameters

ParameterTypeDescription
charsstringString of characters to map

Returns

this

This instance for chaining.

Inherited from

ts
TextmodeSource.characters

charColor()

ts
charColor(
   colorOrGray, 
   g?, 
   b?, 
   a?): this;

Defines the character color when charColorMode is 'fixed'.

Parameters

ParameterTypeDescription
colorOrGray| string | number | TextmodeColorA grayscale value (0-255), hex string ('#RGB', '#RRGGBB', '#RRGGBBAA'), or TextmodeColor instance
g?numberGreen component (0-255) if using RGB format
b?numberBlue component (0-255) if using RGB format
a?numberAlpha component (0-255) if using RGBA format

Returns

this

This instance for chaining.

Inherited from

ts
TextmodeSource.charColor

charColorMode()

ts
charColorMode(mode): this;

Set character color mode: 'sampled' (from source) or 'fixed'.

Parameters

ParameterTypeDescription
mode"sampled" | "fixed"The character color mode

Returns

this

This instance for chaining.

Inherited from

ts
TextmodeSource.charColorMode

charRotation()

ts
charRotation(degrees): this;

Set the character rotation in degrees (0-360).

Parameters

ParameterTypeDescription
degreesnumberRotation in degrees

Returns

this

This instance for chaining.

Inherited from

ts
TextmodeSource.charRotation

colorFilter()

ts
colorFilter(palette?): this;

Applies an optional color filter palette before MRT conversion. When a palette is provided, all sampled pixels are quantized to the closest palette color prior to character/color analysis.

Parameters

ParameterTypeDescription
palette?| null | string[] | [number, number, number][] | [number, number, number, number][] | TextmodeColor[]A list of colors defined as TextmodeColor instances, hex strings, or RGBA tuples (0-255). Providing an empty array or null disables the filter.

Returns

this

Inherited from

ts
TextmodeSource.colorFilter

conversionMode()

ts
conversionMode(mode): this;

Select the conversion mode for this source.

textmode.js includes only a single built-in conversion strategy 'brightness'.

Additional conversion strategies may be provided via add-on libraries.

Parameters

ParameterTypeDescription
modeTextmodeConversionModeConversion mode to use.

Returns

this

Inherited from

ts
TextmodeSource.conversionMode

flipX()

ts
flipX(v): this;

Set horizontal flip indicator flag.

Parameters

ParameterTypeDefault valueDescription
vnumber | booleantrueFlip flag

Returns

this

This instance for chaining.

Inherited from

ts
TextmodeSource.flipX

flipY()

ts
flipY(v): this;

Set vertical flip indicator flag.

Parameters

ParameterTypeDefault valueDescription
vnumber | booleantrueFlip flag

Returns

this

This instance for chaining.

Inherited from

ts
TextmodeSource.flipY

invert()

ts
invert(v): this;

Set the invert flag, swapping character and cell colors when enabled.

Parameters

ParameterTypeDefault valueDescription
vnumber | booleantrueInvert flag

Returns

this

This instance for chaining.

Inherited from

ts
TextmodeSource.invert