Skip to content

Commit 528ef6a

Browse files
committed
/
0 parents  commit 528ef6a

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Horace Hoff
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Ascii-Intro
2+
> A very simple and lightweight JavaScript library (676 bytes minified) to animate ASCII art. Essentially, what it does is animate an "intro animation" of your ASCII text.
3+
4+
### Parameters
5+
| Param | Type | Description |
6+
|---------------------------|---------------------| ------------ |
7+
| preId | <code>string</code> | The id of the desired 'pre' tag. |
8+
| ascii | <code>string</code> | The ascii art.|
9+
| frameDelay (default = 22) | <code>number</code> | The time (in ms) between each animation update, more is slower, less is faster.|
10+
11+
### License
12+
[MIT License](LICENSE)\
13+
Copyright (c) 2024 Horace Hoff

ascii-intro.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Animates ASCII art.
3+
* @param {string} preId - The id of the desired 'pre' tag.
4+
* @param {string} ascii - The ascii art.
5+
* @param {number} frameDelay - The time (in ms) between each animation update, more is slower, less is faster.
6+
*/
7+
async function asciiIntro(preId, ascii, frameDelay = 22) {
8+
function replaceLetter(str, index, replacement) {
9+
return str.substring(0, index) + replacement + str.substring(index + replacement.length);
10+
}
11+
12+
13+
let final = ascii.trim().trim()
14+
document.getElementById(preId).innerText = ""
15+
16+
let lines = final.split("\n")
17+
let possible_chars = ["*","-","/","+","=","#"]
18+
19+
let lengths = []
20+
for (let i = 0; i < lines.length; i++) {
21+
lengths.push( Math.floor(Math.random() * 6))
22+
}
23+
24+
function displayRange(range) {
25+
let to_display = ""
26+
for (let i = 0; i < lines.length; i++) {
27+
let middle_index = Math.floor(lines[i].length / 2)
28+
29+
let chars = lines[i].substring(middle_index-range, middle_index+range)
30+
31+
if (range < (lines[i].length / 2 - 1)) {
32+
for (let x = 0; x < lengths[i]; x++) {
33+
const random_char = Math.floor(Math.random() * possible_chars.length);
34+
chars = replaceLetter(chars, x, possible_chars[random_char])
35+
}
36+
}
37+
38+
let range_0 = middle_index - range
39+
let range_1 = lines[i].length - (middle_index+range)
40+
41+
to_display += " ".repeat(range_0 < 0 ? 0 : range_0)+chars+" ".repeat(range_1 < 0 ? 0 : range_1)+"\n"
42+
}
43+
document.getElementById(preId).innerText = to_display
44+
}
45+
46+
for (let x = 0; x <= lines[0].length + 1; x++) {
47+
displayRange(x)
48+
await new Promise(res => setTimeout(res, frameDelay))
49+
}
50+
}

ascii-intro.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)