A Node.js library for ansi codes, figlet fonts, ascii art and other ASCII graphics
A Node.js library for ansi codes, figlet fonts, ascii art and other ASCII graphics
(_)(_) | |
__ _ ___ ___ _ _ ______ __ _ _ __ | |_
/ _` |/ __| / __|| || ||______| / _` || '__|| __|
| (_| |\__ \| (__ | || | | (_| || | | |_
\__,_||___/ \___||_||_| \__,_||_| \__|Images, fonts, tables, ansi styles and compositing in Node.js & the browser. 100% JS. (What is ASCII-Art?)
In the beginning there was colors.js but in the fine tradition of vendors calling out a problem they have the solution to, chalk was introduced. In that same vein, I offer ascii-art as an update, expansion and generalization of MooAsciiArt and at the same time it can replace your existing ansi colors library.
It features support for Images, Styles, Tables, Graphs and Figlet Fonts as well as handling multi-line joining and compositing automatically.
Why would I use this?
[click to expand/close]
String.prototype usage. No __proto__ usage. No BS.† - This allows us plug into averaging, distance and other logic dynamically, in powerful ways (In node this renders in cairo, via a Canvas shim)
‡ - while both have fluent apis we use a grid and are asynchronous, which enables large image processing and complex compositing scenarios, whereas the chalk API focuses on immediate string mutations
ascii-art supports many usage styles, but the different use cases require slightly different installations. If you have any questions, please ask.
In Code
npm install --save ascii-artCLI
npm install -g ascii-artor (Beta):
npm install -g ascii-art-clWeb
npm install --save ascii-art-webcomponentsAll chains in ascii-art can be called in one of 3 ways. In this example we change "Some Text" to be formatted using the doom.flf font.
Callback
art.font("Some Text", 'doom', (err, rendered)=>{
//if err, err is the error that occured
//if !err rendered is the ascii
});Promise
art.font("Some Text", 'doom')
.then((rendered)=>{
//rendered is the ascii
})).catch((err)=>{
//err is an error
}));Await
try{
let rendered = await art.font("Some Text", 'doom').completed()
//rendered is the ascii
}catch(err){
//err is an error
}Add ANSI styles to a string and return the result.
Change "Some Text" to be formatted with ansi codes for green (then have the colors reset at the end of the string)
JS
art.style("Some Text", 'green', true) //returns StringCL
ascii-art text -s green "Some Text"Web
Some TextStyles are: italic, bold, underline, |framed|, |encircled|, overline, blink and inverse . And available colors are:
Colors
Color defaults to 8 bit (most compatible), to enable other modes set booleans in the options:
is256 : compute the color using 256colors, as defined by ansi256. Note that full color output may need to tune the difference method to obtain optimal results.isTrueColor : do not constrain color and directly output RGB to the console.Color Tables may be found in the style documentation
Render a string using a figlet font and add that to the buffer. There is a batch version of this function which does not chain and takes an array( .strings()).
Change "Some Text" to be formatted using the doom.flf font, which it will load from the predefined location (defaults to /Fonts)
JS
art.font("Some Text", 'doom', true) //returns StringCL
ascii-art font -F doom "Some Text"Web
Some TextOutputs
______ _
| _ \ | |
| | | | ___ _ __ ___ ___ | |
| | | | / _ \| '_ ` _ \ / _ \ | |
| |/ / | __/| | | | | || (_) ||_|
|___/ \___||_| |_| |_| \___/ (_)If you use UTF fonts(Which are part of your system fonts) your output looks more like:
or by using u: where the font names are: default, script, script+bold, gothic, gothic+bold, serif+bold+italic, serif+bold, serif+italic, monospace, sansserif, sansserif+bold+italic, sansserif+bold, sansserif+italic, doublestrike
Check out the documentation for more examples!
Create an image from the passed image and append that to the buffer
This takes myImage.jpg, converts the colors to ansi backgrounds and then stipples the details at a threshold of 40 (of 255) using a higher resolution image and then renders that into a braille overlay in black(#000000) on top of the colors.
JS
art.image({
src: "myImage.jpg",
rows:80,
cols:80,
stipple:"#000000",
posterize: true,
threshold:40
}, cb)CL
ascii-art image posterized stippled
--rows=80 --cols=80 --stipple="#000000"
--threshold=40 myImage.jpgWeb
There are some options that are available which are not in the image core, they are:
lineart : A boolean option which outputs lineart using block characters (which may be colored with stroke and customized with threshold(0-255)). Example
stipple : A boolean option which outputs lineart using braille characters (which may be colored with stroke and customized with threshold(0-255)). Example
posterize : Use stipple on top of colored backgrounds to retain as much detail as possible. Example
blended : Use posterize with both lineart and braille at relative thresholds to each other. Example
Because of the resolution downsampling from the original dimensions to the destination width, some finer details may be lost. Plan accordingly. Here's an example of some sequential art in 256 color (primarily greyscale):
Example
JS
var art = require('ascii-art');
var Color = require('ascii-art-ansi/colors');
Color.is256 = true;
art.image({
src: "node_modules/ascii-art/Images/grendel.jpg",
alphabet:"blocks"
}, cb);CL
ascii-art image
-B 8
-C rankedChannel
-a blocks
node_modules/ascii-art/Images/grendel.jpgWeb
Multiple output modes are available including 4bit, 8bit, 32bit (equivalent to 24bit). 16bit color was intentionally excluded, as there is no direct terminal support and the cost of including color definitions for such a large set would not be justified. It would be feasible to implement as an optional import, should the need exist.
The following example takes zero-cool.jpg, converts the colors to ansi foreground color in solid block characters. Note that you will need to tune your distance function in 8bit mode, as what looks good varies by the type of imagery that goes through it.
Anecdotally, the default distance function only covers ~1/3 of the samples we've done, but we support many methods(euclideanDistance, classic, ratioDistance, classicByValue, CIE76Difference, closestByIntensity, rankedChannel, simple, minDeviation, luminosity, saturation, hue, original). But helpfully, you can merge algorithms by asking for algorithm1+algorithm2 (it uses equal weighting), or make your own.
img 4
8
32
JS 4
var art = require('ascii-art');
art.image({
src: "node_modules/ascii-art/Images/zero-cool.jpg",
alphabet:"solid"
}, cb);8
var art = require('ascii-art');
var Color = require('ascii-art-ansi/colors');
Color.is256 = true;
art.image({
src: "node_modules/ascii-art/Images/zero-cool.jpg",
alphabet:"solid"
}, cb);32
var art = require('ascii-art');
var Color = require('ascii-art-ansi/colors');
Color.isTrueColor = true;
art.image({
src: "node_modules/ascii-art/Images/zero-cool.jpg",
alphabet:"solid"
}, cb);CL 4
ascii-art image
-B 4
-a solid
node_modules/ascii-art/Images/zero-cool.jpg8
ascii-art image
-B 8
-C closestByIntensity
-a solid
node_modules/ascii-art/Images/zero-cool.jpg32
ascii-art image
-B 32
-a solid
node_modules/ascii-art/Images/zero-cool.jpgWeb 4
8
32
Check out the documentation for more examples!
Generate a table from the passed data, with support for many styles and append that to the buffer
Render a table for with the provided data given the provided options
JS
art.table({}, cb) //returns StringCL
# N/AWeb
Check out the documentation for more examples!
Render a graph for with the provided data given the provided options
JS
art.graph({}, cb) //returns StringCL
# N/AWeb
Check out the documentation for more examples!
Fetch a graphic from a remote source and append it to the current buffer, which is not enabled by default. You must add a request compatible library by either setting the ENV variable ASCII_ART_REQUEST_MODULE or by sett
No open issues yet, or sync has not completed.