Node.js has the process object with stdout property which is a stream connected to stdout. Using the process.stdout property, we can access several useful properties and methods: columns, rows, cursorTo, and write. These are essentials to start "animate" something in the terminal.
Imagine we want to animate a falling "character" from top to bottom of the screen, which could be a small part of the "digital rain" animation from the Matrix movie.
To do this we will need several steps:
Write the character in a starting position.
Make a pause.
Remove the character.
Write the character into a new position.
Repeat...
const column = 0
const character = '$'
/* Pause for 1 second (1000 ms) */
const pause = () => new Promise((resolve) => setTimeout(resolve, 1000))
const main = async () => {
/* Clear screen */
console.clear()
/* Hide cursor */
process.stderr.write('\x1B[?25l')
/* The number of rows */
const rows = process.stdout.rows
/* Starting from the first row on the top of the screen.
* Going to the bottom of the screen `process.stdout.rows`.
*/
for (let row = 0; row < rows; row++) {
/* Set cursor into position `cursorTo(x, y)`
* and write a character */
process.stdout.cursorTo(column, row)
process.stdout.write(character)
/* Pause for 1 second */
await pause()
/* Set cursor into position and clear the character
* (write a space) */
process.stdout.cursorTo(column, row)
process.stdout.write(' ')
}
/* Show cursor */
process.stderr.write('\x1B[?25h')
}
main()
const column = 0
const character = '$'
/* Pause for 1 second (1000 ms) */
const pause = () => new Promise((resolve) => setTimeout(resolve, 1000))
const main = async () => {
/* Clear screen */
console.clear()
/* Hide cursor */
process.stderr.write('\x1B[?25l')
/* The number of rows */
const rows = process.stdout.rows
/* Starting from the first row on the top of the screen.
* Going to the bottom of the screen `process.stdout.rows`.
*/
for (let row = 0; row < rows; row++) {
/* Set cursor into position `cursorTo(x, y)`
* and write a character */
process.stdout.cursorTo(column, row)
process.stdout.write(character)
/* Pause for 1 second */
await pause()
/* Set cursor into position and clear the character