admin管理员组文章数量:1356017
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
3 Answers
Reset to default 3You 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
* * * * *** *** **** * * *
* * * * * * * * * * * * *
** * * * * * * * * * ** *
* * * * * * * *** **** * * * *
* ** * * * * * * * * **
* * * * * * * * * * * *
* * *** *** *** * * * *
本文标签: javascriptHow can I write ASCII art for text(using any language)Stack Overflow
版权声明:本文标题:javascript - How can I write ASCII art for text(using any language)? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743948881a2566916.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论