ASCII text

Instead of drawing to a <canvas>, get the ASCII art back as a plain multi-line string you can use anywhere — copy to clipboard, print to a terminal, pipe to a file, or drop into SSR templates. Synchronous, and no output canvas needed.

Try it

Pick an image, tweak the charset and cell size — the output string updates live. Hit Copy text to grab exactly what is shown.

Size
asciiText() string
Rendering…
asciify() canvasSame options

Top: the plain string returned by asciiText(), set in Geist Mono with the cell aspect matched to the engine. Bottom: the same options drawn to a canvas with asciify().

API

asciiText(source, options?, targetWidth?, targetHeight?)Returns string

Convert an image, video frame, or canvas to a plain multi-line ASCII string. Rows are separated by newline characters. Synchronous — source must be already loaded.

asciiTextAnsi(source, options?, targetWidth?, targetHeight?)Returns string

Same as asciiText but wraps each character in ANSI truecolor escape codes (ESC[38;2;R;G;Bm). Prints coloured ASCII art in any terminal that supports ANSI color.

Basic usage

ascii-text.ts
import { asciiText, asciiTextAnsi } from 'asciify-engine';

const img = document.querySelector('img')!;

// Plain string
const text = asciiText(img, { fontSize: 8, charset: ' .:-=+*#%@' });
navigator.clipboard.writeText(text);

// ANSI coloured — pipe to a terminal
const ansi = asciiTextAnsi(img, { fontSize: 8, colorMode: 'fullcolor' });
process.stdout.write(ansi);

Server-side (Node.js)

ssr.ts
import { createCanvas, loadImage } from '@napi-rs/canvas';
import { asciiText } from 'asciify-engine';

async function imageToAscii(path: string): Promise<string> {
  const img    = await loadImage(path);
  const canvas = createCanvas(img.width, img.height);
  const ctx    = canvas.getContext('2d');
  ctx.drawImage(img, 0, 0);

  // @napi-rs/canvas shapes are compatible with HTMLCanvasElement
  return asciiText(canvas as unknown as HTMLCanvasElement, {
    fontSize: 10,
    charset: ' .:-=+*#%@',
  });
}

const art = await imageToAscii('./photo.jpg');
console.log(art);

Controlling output dimensions

Pass targetWidth and targetHeight in pixels to control grid size independently of the source image resolution. Columns are targetWidth / fontSize; rows are targetHeight / (fontSize / charAspect). Without them the grid is sized from the source's own pixel dimensions.

ts
// targetWidth / targetHeight are pixels, not columns and rows.
// A cell is fontSize wide and fontSize / charAspect tall, and the
// source is stretched to fill, so crop it to the grid's aspect first.
const fontSize = 8;
const charAspect = 0.55;
const cols = 80, rows = 40;

const text = asciiText(
  img,
  { fontSize, charAspect },
  cols * fontSize,
  rows * fontSize / charAspect,
); // 40 lines of 80 characters

With art style presets

with-preset.ts
import { asciiText, CHARACTER_SETS, DEFAULT_OPTIONS } from 'asciify-engine';

const options = {
  ...DEFAULT_OPTIONS,
  charset: CHARACTER_SETS.technical.chars,
  fontSize: 8,
};

const text = asciiText(img, options);