最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I write ASCII art for text(using any language)? - Stack Overflow

programmeradmin2浏览0评论

I want to write a program to print following pattern for text "NUOSPIN":

* * *  *   *  * * *  * * *  * * *  *  * * *
*   *  *   *  *   *  *      * * *  *  *   *
*   *  *   *  *   *  * * *  *      *  *   *
*   *  * * *  * * *  * * *  *      *  *   *

I am working on Javascript(NodeJS). I have to output it on console. Implementation in any language will work for me including java, C, javascript. I know I can do this by using prebuilt npm packages(like asciify), but I want to do this raw. What should be my approach for this? Do I have to write code for each character's pattern separately? And then print them by running logic of each character's pattern one by one?

I want to write a program to print following pattern for text "NUOSPIN":

* * *  *   *  * * *  * * *  * * *  *  * * *
*   *  *   *  *   *  *      * * *  *  *   *
*   *  *   *  *   *  * * *  *      *  *   *
*   *  * * *  * * *  * * *  *      *  *   *

I am working on Javascript(NodeJS). I have to output it on console. Implementation in any language will work for me including java, C, javascript. I know I can do this by using prebuilt npm packages(like asciify), but I want to do this raw. What should be my approach for this? Do I have to write code for each character's pattern separately? And then print them by running logic of each character's pattern one by one?

Share Improve this question asked Oct 11, 2016 at 17:58 Rahul RajputRahul Rajput 2841 gold badge3 silver badges15 bronze badges 2
  • "I want to do this raw" - 1) Why, when there are pre-existing libraries that do that? 2) Look at the source code of those libraries to get an idea how to do this. – Tomalak Commented Oct 11, 2016 at 18:01
  • @Tomalak well, copying from the source code doesn't teach you anything, it just shows you the plain answer, and 2. because npm sometimes just isn't great. – user17585064 Commented Nov 18, 2022 at 2:50
Add a ment  | 

3 Answers 3

Reset to default 3

You could use a bitmap and assemble the dots to the ASCII style, you want.

function getWord(s) {
    var ascii = [],
        font = {
            N: [7, 5, 5, 5],
            U: [5, 5, 5, 7],
            O: [7, 5, 5, 7],
            S: [7, 4, 7, 7],
            P: [7, 7, 4, 4],
            I: [1, 1, 1, 1]
        };

    s.split('').forEach(function (c) {
        var size =( font[c].reduce(function (r, a) {
            return r | a;
        }, 0)).toString(2).length;

        font[c].forEach(function (a, i) {
            var temp = a.toString(2).split('').map(function (c) {
                return +c ? '*' : ' ';
            });
            while (temp.length < size) {
                temp.unshift(' ');
            }
            ascii[i] = ascii[i] || [];
            ascii[i].push(temp.join(''));
        });
    });
    return ascii.map(function (a) {
        return a.join(' ');
    }).join('\n')
}

document.getElementById('tt').innerHTML = getWord('NUOSPIN');
<pre id="tt"></pre>

Usually you have a large map somewhere that maps a character to its appearance, especially for such custom-made fonts. You'd then go over the input string, look up the picture for a character, put it in a buffer, and print that buffer once you're done.

You can also do it by printing directly to the screen by doing this line by line, look up each character's first line, print that, look up each character's second line, etc.

If you want to get really creative you could also draw an image in memory of the text with a specific font, and then draw that image with asterisks and spaces to the console.

Each line of text would have to be written several times. First the upper rows of the ASCII art glyphs.

#!/usr/bin/python

import sys

# Each item in `font` is a list of `font_height` strings.
# The glyphs are not limited to asterisks and spaces.
font_height = 7
margin_left = 1
margin_right = 1
font = {
    'N': [
        "*   *",
        "*   *",
        "**  *",
        "* * *",
        "*  **",
        "*   *",
        "*   *",
    ],
    'U': [
        "*   *",
        "*   *",
        "*   *",
        "*   *",
        "*   *",
        "*   *",
        " *** ",
    ],
    'O': [
        " *** ",
        "*   *",
        "*   *",
        "*   *",
        "*   *",
        "*   *",
        " *** ",
    ],
    'S': [
        " *** ",
        "*   *",
        "*    ",
        " *** ",
        "    *",
        "*   *",
        " *** ",
    ],
    'P': [
        "**** ",
        "*   *",
        "*   *",
        "**** ",
        "*    ",
        "*    ",
        "*    ",
    ],
    'I': [
        "  *  ",
        "  *  ",
        "  *  ",
        "  *  ",
        "  *  ",
        "  *  ",
        "  *  ",
    ],
}

def printline(text):
    for row in range(font_height):
        for ch in text:
            sys.stdout.write(' ' * margin_left)
            sys.stdout.write(font[ch][row])
            sys.stdout.write(' ' * margin_right)
        sys.stdout.write('\n')

printline('NUOSPIN')

Gives me

sshoskar@oskog97:~$ ./test.py 
 *   *  *   *   ***    ***   ****     *    *   * 
 *   *  *   *  *   *  *   *  *   *    *    *   * 
 **  *  *   *  *   *  *      *   *    *    **  * 
 * * *  *   *  *   *   ***   ****     *    * * * 
 *  **  *   *  *   *      *  *        *    *  ** 
 *   *  *   *  *   *  *   *  *        *    *   * 
 *   *   ***    ***    ***   *        *    *   * 
发布评论

评论列表(0)

  1. 暂无评论