digital-matrix-rain
Version:
A cool matrix-style digital rain animation for terminal/console applications
221 lines (192 loc) • 5.19 kB
JavaScript
/**
* Matrix Animation Package
* Creates a cool matrix-style digital rain animation for terminal/console
*/
/**
* Matrix Rain Animation Class
*/
class MatrixRain {
constructor(options = {}) {
this.width = options.width || process.stdout.columns || 80;
this.height = options.height || process.stdout.rows || 24;
this.speed = options.speed || 100;
this.density = options.density || 0.1;
this.color = options.color || 'green';
this.chars = options.chars || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789日ハミヒーウシナモニサワツオリアホテマケメエカキムユラセネスタヌヘ';
this.drops = [];
this.isRunning = false;
this.interval = null;
// Initialize drops
this.initDrops();
}
/**
* Initialize the falling drops
*/
initDrops() {
this.drops = [];
for (let x = 0; x < this.width; x++) {
this.drops[x] = Math.random() < this.density ? 1 : 0;
}
}
/**
* Get a random character from the character set
*/
getRandomChar() {
return this.chars.charAt(Math.floor(Math.random() * this.chars.length));
}
/**
* Get ANSI color code
*/
getColorCode() {
const colors = {
green: '\x1b[92m', // Bright green (classic Matrix green)
red: '\x1b[31m',
blue: '\x1b[34m',
yellow: '\x1b[33m',
magenta: '\x1b[35m',
cyan: '\x1b[36m',
white: '\x1b[37m'
};
return colors[this.color] || colors.green;
}
/**
* Get bright green color for the leading character
*/
getBrightGreenCode() {
return '\x1b[1m\x1b[92m'; // Bold bright green
}
/**
* Clear the screen
*/
clearScreen() {
process.stdout.write('\x1b[2J\x1b[H');
}
/**
* Draw a single frame
*/
drawFrame() {
let output = '';
for (let y = 0; y < this.height; y++) {
let line = '';
for (let x = 0; x < this.width; x++) {
if (this.drops[x] > y && this.drops[x] - y < 15) {
const distanceFromHead = this.drops[x] - y;
if (distanceFromHead === 1) {
// Bright white/green character at the very front
line += '\x1b[1m\x1b[97m' + this.getRandomChar() + '\x1b[0m';
} else if (distanceFromHead <= 3) {
// Bright green for the next few characters
line += '\x1b[1m\x1b[92m' + this.getRandomChar() + '\x1b[0m';
} else if (distanceFromHead <= 8) {
// Normal green
line += '\x1b[92m' + this.getRandomChar() + '\x1b[0m';
} else {
// Dim green for the tail
line += '\x1b[32m' + this.getRandomChar() + '\x1b[0m';
}
} else {
line += ' ';
}
}
output += line + '\n';
}
return output;
}
/**
* Update drops positions
*/
updateDrops() {
for (let x = 0; x < this.width; x++) {
// If drop has reached bottom or random reset
if (this.drops[x] * this.speed > this.height && Math.random() > 0.975) {
this.drops[x] = 0;
}
// Move drop down
this.drops[x]++;
// Random new drop
if (this.drops[x] === 0 && Math.random() < this.density / 10) {
this.drops[x] = 1;
}
}
}
/**
* Start the animation
*/
start() {
if (this.isRunning) return;
this.isRunning = true;
this.clearScreen();
// Hide cursor
process.stdout.write('\x1b[?25l');
this.interval = setInterval(() => {
this.clearScreen();
process.stdout.write(this.drawFrame());
this.updateDrops();
}, this.speed);
// Handle Ctrl+C
process.on('SIGINT', () => {
this.stop();
process.exit(0);
});
}
/**
* Stop the animation
*/
stop() {
if (!this.isRunning) return;
this.isRunning = false;
if (this.interval) {
clearInterval(this.interval);
this.interval = null;
}
// Show cursor
process.stdout.write('\x1b[?25h');
this.clearScreen();
}
/**
* Run animation for a specific duration
*/
run(duration = 10000) {
this.start();
setTimeout(() => {
this.stop();
}, duration);
}
}
/**
* Simple matrix animation function
*/
function createMatrixAnimation(options = {}) {
return new MatrixRain(options);
}
/**
* Quick start function
*/
function matrix(options = {}) {
const animation = new MatrixRain(options);
animation.start();
return animation;
}
/**
* Demo function
*/
function demo() {
console.log('🚀 Starting Matrix Animation Demo...');
console.log('🌿 Classic Green Matrix Style');
console.log('Press Ctrl+C to stop\n');
setTimeout(() => {
const animation = new MatrixRain({
color: 'green',
speed: 75,
density: 0.12
});
animation.start();
}, 2000);
}
// Export functions and class
module.exports = {
MatrixRain,
createMatrixAnimation,
matrix,
demo
};