-
Notifications
You must be signed in to change notification settings - Fork 72
/
index.js
194 lines (172 loc) · 5.05 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import chalk from 'chalk';
import gradient from 'gradient-string';
const log = console.log;
let currentAnimation = null;
const consoleFunctions = {
log: log.bind(console),
info: console.info.bind(console),
warn: console.warn.bind(console),
error: console.error.bind(console)
};
// eslint-disable-next-line guard-for-in
for (const k in consoleFunctions) {
console[k] = function () {
stopLastAnimation();
consoleFunctions[k].apply(console, arguments);
};
}
const glitchChars = 'x*0987654321[]0-~@#(____!!!!\\|?????....0000\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t';
const longHsv = {interpolation: 'hsv', hsvSpin: 'long'};
const effects = {
rainbow(str, frame) {
const hue = 5 * frame;
const leftColor = {h: hue % 360, s: 1, v: 1};
const rightColor = {h: (hue + 1) % 360, s: 1, v: 1};
return gradient(leftColor, rightColor)(str, longHsv);
},
pulse(str, frame) {
frame = (frame % 120) + 1;
const transition = 6;
const duration = 10;
const on = '#ff1010';
const off = '#e6e6e6';
if (frame >= (2 * transition) + duration) {
return chalk.hex(off)(str); // All white
}
if (frame >= transition && frame <= transition + duration) {
return chalk.hex(on)(str); // All red
}
frame = frame >= transition + duration ? (2 * transition) + duration - frame : frame; // Revert animation
const g = frame <= transition / 2 ?
gradient([
{color: off, pos: 0.5 - (frame / transition)},
{color: on, pos: 0.5},
{color: off, pos: 0.5 + (frame / transition)}
]) :
gradient([
{color: off, pos: 0},
{color: on, pos: 1 - (frame / transition)},
{color: on, pos: frame / transition},
{color: off, pos: 1}
]);
return g(str);
},
glitch(str, frame) {
if ((frame % 2) + (frame % 3) + (frame % 11) + (frame % 29) + (frame % 37) > 52) {
return str.replace(/[^\r\n]/g, ' ');
}
const chunkSize = Math.max(3, Math.round(str.length * 0.02));
const chunks = [];
for (let i = 0, length = str.length; i < length; i++) {
const skip = Math.round(Math.max(0, (Math.random() - 0.8) * chunkSize));
chunks.push(str.substring(i, i + skip).replace(/[^\r\n]/g, ' '));
i += skip;
if (str[i]) {
if (str[i] !== '\n' && str[i] !== '\r' && Math.random() > 0.995) {
chunks.push(glitchChars[Math.floor(Math.random() * glitchChars.length)]);
} else if (Math.random() > 0.005) {
chunks.push(str[i]);
}
}
}
let result = chunks.join('');
if (Math.random() > 0.99) {
result = result.toUpperCase();
} else if (Math.random() < 0.01) {
result = result.toLowerCase();
}
return result;
},
radar(str, frame) {
const depth = Math.floor(Math.min(str.length, str.length * 0.2));
const step = Math.floor(255 / depth);
const globalPos = frame % (str.length + depth);
const chars = [];
for (let i = 0, length = str.length; i < length; i++) {
const pos = -(i - globalPos);
if (pos > 0 && pos <= depth - 1) {
const shade = (depth - pos) * step;
chars.push(chalk.rgb(shade, shade, shade)(str[i]));
} else {
chars.push(' ');
}
}
return chars.join('');
},
neon(str, frame) {
const color = (frame % 2 === 0) ? chalk.dim.rgb(88, 80, 85) : chalk.bold.rgb(213, 70, 242);
return color(str);
},
karaoke(str, frame) {
const chars = (frame % (str.length + 20)) - 10;
if (chars < 0) {
return chalk.white(str);
}
return chalk.rgb(255, 187, 0).bold(str.substr(0, chars)) + chalk.white(str.substr(chars));
}
};
function animateString(str, effect, delay, speed) {
stopLastAnimation();
speed = speed === undefined ? 1 : parseFloat(speed);
if (!speed || speed <= 0) {
throw new Error('Expected `speed` to be an number greater than 0');
}
currentAnimation = {
text: str.split(/\r\n|\r|\n/),
lines: str.split(/\r\n|\r|\n/).length,
stopped: false,
init: false,
f: 0,
render() {
const self = this;
if (!this.init) {
log('\n'.repeat(this.lines - 1));
this.init = true;
}
log(this.frame());
setTimeout(() => {
if (!self.stopped) {
self.render();
}
}, delay / speed);
},
frame() {
this.f++;
return '\u001B[' + this.lines + 'F\u001B[G\u001B[2K' + this.text.map(str => effect(str, this.f)).join('\n');
},
replace(str) {
this.text = str.split(/\r\n|\r|\n/);
this.lines = str.split(/\r\n|\r|\n/).length;
return this;
},
stop() {
this.stopped = true;
return this;
},
start() {
this.stopped = false;
this.render();
return this;
}
};
setTimeout(() => {
if (!currentAnimation.stopped) {
currentAnimation.start();
}
}, delay / speed);
return currentAnimation;
}
function stopLastAnimation() {
if (currentAnimation) {
currentAnimation.stop();
}
}
const chalkAnimation = {
rainbow: (str, speed) => animateString(str, effects.rainbow, 15, speed),
pulse: (str, speed) => animateString(str, effects.pulse, 16, speed),
glitch: (str, speed) => animateString(str, effects.glitch, 55, speed),
radar: (str, speed) => animateString(str, effects.radar, 50, speed),
neon: (str, speed) => animateString(str, effects.neon, 500, speed),
karaoke: (str, speed) => animateString(str, effects.karaoke, 50, speed)
};
export default chalkAnimation;