Created
October 25, 2017 18:24
-
-
Save harrytallbelt/9c1ccea9465cfcb524af765591087759 to your computer and use it in GitHub Desktop.
A satisfying Matrix effect with Node.JS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const LINE_WIDTH = 80 // 150 | |
const CONTINUATION_PROBABILITY = 0.9 | |
const FRAMERATE = 100 | |
console.log('\x1b[32m') // green text | |
console.log('\x1b[40m') // black bg | |
let line = [] | |
for (let i = 0; i < LINE_WIDTH / 2; ++i) { | |
line.push(false) | |
} | |
while (true) { | |
render(line) | |
line = generateNewLine(line) | |
wait(FRAMERATE) | |
} | |
function render(line) { | |
console.log(line | |
.map(c => c ? randomChar() : ' ') | |
.join(' ')) | |
} | |
function randomChar() { | |
return String.fromCharCode(33 + Math.floor(Math.random() * 94)) | |
} | |
function generateNewLine(oldLine) { | |
return oldLine | |
.map(c => Math.random() < CONTINUATION_PROBABILITY ? c : !c) | |
} | |
function wait(ms) { | |
const time = new Date().getTime() | |
while (new Date().getTime() < time + ms) {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment