-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
68 lines (52 loc) · 1.24 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env node
const characters = ' -=oO0@';
const emptyLine = ' '.repeat(16 * 2 + 1);
code = process.argv[2] || 'sin(t-sqrt((x-7.5)**2+(y-6)**2))';
console.clear();
let output = '';
const callback = new Function('t', 'i', 'x', 'y', `
with (Math) {
return ${code.replace(/\\/g, ';')};
}
`);
let startTime;
function render() {
output = '\n\x1b[40m ' + emptyLine;
output += '\n ';
let time = 0;
if (startTime) {
time = (new Date() - startTime) / 1000;
} else {
startTime = new Date();
}
let index = 0;
for (let y = 0; y < 16; y++) {
for (let x = 0; x < 16; x++) {
let value = Number(callback(time, index, x, y));
let color = '\x1b[37m';
if (value < 0) {
value = -value;
color = '\x1b[31m';
}
if (value > 1) {
value = 1;
}
const character = characters[
Math.floor(value * characters.length)
] || ' ';
output += color + character + ' ';
index++;
}
output += ' \n ';
}
output += emptyLine;
output += '\x1b[0m\n';
output += '\n (t,i,x,y)=>';
output += '\n ' + code;
output += '\n';
output += '\n';
process.stdout.cursorTo(0, 0);
process.stdout.write(output);
setTimeout(render, 1);
}
render();