Skip to documentation content

ASCII

Render an image or a procedural field as a grid of text glyphs drawn from a configurable character ramp.

ascii-demo

Overview

ASCII redraws its source as characters. The canvas is divided into monospace cells; each cell samples the source once and picks a glyph from the characters ramp by brightness, so dense glyphs carry the ink and sparse ones let the background through. The glyphs come from a small atlas drawn at runtime with your font, so any characters work: classic ramps, block elements, Braille, or a word repeated.

Glyphs ink the bright areas when the text is lighter than the background (the terminal look) and the shadows when the text is darker (the printed look). colorMode="source" tints each glyph with the color it replaces.

Without src, ASCII renders a slow procedural field, useful as a terminal-style background.

The playground uses free Unsplash photographs by Marc Wieland and Martin Péchy.

Installation

npm install honestui

Usage

Procedural background

import { Ascii } from "honestui/shaders"

<Ascii className="h-80 rounded-xl" cellSize={14} color="#9ae6b4" backgroundColor="#06120b" />

Image

<Ascii
  alt="An alpine lake reflecting an orange sunset between dark mountain ridges."
  className="h-80 rounded-xl"
  colorMode="source"
  characters=" .,:;i1tfLCG08@"
  src="https://images.unsplash.com/photo-1776348568867-24146d18b736?auto=format&fit=crop&w=1600&q=85"
/>

Order characters from the lightest to the densest glyph; the first is usually a space. Remote image hosts must allow cross-origin use so WebGL can read the pixels. Smaller cells show more of the picture but read less as text; around 10 to 16 pixels keeps both.

Accessibility

The glyphs are pixels on a canvas, not text: screen readers, search, and copy and paste do not see them. Never use ASCII to show words a reader needs; put real text in a separate layer. In image mode, write alt text for the image's purpose, or an empty string when it is decorative. The original image shows until the canvas is ready and remains in the accessibility tree afterwards.

The procedural field moves slowly and stops when the user prefers reduced motion, while the element is offscreen, and while the page is hidden.

API Reference

PropTypeDefaultDescription
srcstring–Image to render as text. Omit it to render the procedural field.
altstring–Alternative text for the image. Required with src; use an empty string for decorative images.
charactersstring" .:-=+*#%@"Glyph ramp from lightest to densest.
cellSizenumber12Height of one character cell in CSS pixels. Width follows the font.
fontFamilystringui-monospace stackFont used to draw the glyphs.
fontWeightnumber | string500Font weight used to draw the glyphs.
colorModemono|source"mono"One glyph color, or each glyph in the source color.
colorstring"#e8ecf4"Glyph color in mono mode. Any CSS color.
backgroundColorstring"#0b0d12"Background color. Any CSS color.
contrastnumber1.35Tone contrast applied before choosing glyphs.
brightnessnumber0Brightness offset from -1 to 1 applied before choosing glyphs.
animatedbooleantrueAnimates the procedural field. Images are always still.
speednumber0.6Speed of the procedural field.
sourceColors[string, string, string]–Three colors of the procedural field.
pausedbooleanfalseRenders one still frame.
classNamestring–Class for sizing and positioning the container.
childrenReactNode–Content layered above the effect.