Logo

dev-resources.site

for different kinds of informations.

Terminal Animations with Node.js

Published at
12/26/2024
Categories
node
javascript
terminal
animation
Author
vladimirvovk
Author
12 person written this
vladimirvovk
open
Terminal Animations with Node.js

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.

Digital Rain Animation

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()
Enter fullscreen mode Exit fullscreen mode

Falling Character Animation

You can play with this code here.

Next, it will be nice to add some colors to our animation. We can use the great Ansis library for that.

Let's output something in green color:

/* import Ansis library */
import { green } from 'ansis'

const character = '$'

/* Apply green color to our character */
const coloredCharacter = green`${character}`

/* Write colored character to the terminal */
process.stdout.write(coloredCharacter)
Enter fullscreen mode Exit fullscreen mode

Or even better animate the color from dark to light green:

import { rgb } from 'ansis'

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++) {
    /* Use the `rgb` function to change color
     * from dark to light green */
    const g = Math.floor((255 / rows) * row)
    const coloredCharacter = rgb(0, g, 0)`${character}`

    /* Set cursor into position `cursorTo(x, y)`
     * and write a character */
    process.stdout.cursorTo(column, row)
    process.stdout.write(coloredCharacter)
    /* 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()
Enter fullscreen mode Exit fullscreen mode

Green Color Animation

We can create something interesting and cool-looking using these simple ideas and animation techniques. ๐ŸŒŸ

See the Matrix digital rain in action with the npx jmatrix command and check out the source code on Github.

Credits

Photo by Lukas on Unsplash


Please post your thoughts, questions, and ideas in the comments, press the ๐Ÿ’– button, and happy hacking! ๐Ÿ™Œ๐Ÿป

animation Article's
30 articles in total
Favicon
[Boost]
Favicon
What is the scope of multimedia and animation?
Favicon
[Translations] Implementing Animations with Throttle in JavaScript
Favicon
Top Aviation Courses in Bangalore: Best Aviation Training Institutes for Career Growth in 2025
Favicon
Using Forced Reflows, the Event Loop, and the Repaint Cycle to Slide Open a Box
Favicon
Getting Started with CSS Animations: A Beginner's Guide ๐ŸŽจโœจ
Favicon
Houdini animations USED IN APPS
Favicon
Some platforms to post your animations on the web
Favicon
Sci-fi Text Animation
Favicon
Counter - A React library for animating numeric transitions
Favicon
Terminal Animations with Node.js
Favicon
Enjoy editing your videos with fun animations, premium effects, and pro features unlocked by downloading the Alight Motion Mod APK 2024 for free. https://alightmotionproapks.com/
Favicon
Learn how to create an animated avatar stack widget with Tailwind CSS
Favicon
Framer Motion Animated Hero with Reactjs
Favicon
Learn how to create an animated expanding search bar with Tailwind CSS
Favicon
ใ€ŒMac็•…็Žฉ้ธฟ่’™ไธŽ็กฌไปถ19ใ€้ธฟ่’™UI็ป„ไปถ็ฏ‡9 - ่‡ชๅฎšไน‰ๅŠจ็”ปๅฎž็Žฐ
Favicon
[Boost]
Favicon
Learn how to create an animated profile card with Tailwind CSS
Favicon
Learn how to create a cool animated envelope with Tailwind CSS
Favicon
How to Create a Flipping Card Animation Using Framer Motion
Favicon
Learn how to create an animated input field with Tailwind CSS
Favicon
Dynamic Box Shadow on Mouse Move
Favicon
Interactive Snowfall Cursor Effect with CSS and JavaScript
Favicon
How to Create Scroll Animation for Text in Your Website
Favicon
Add Crazy Cursor Click Effect on your website..
Favicon
Create Eye-Catching Button Effect with Rotating Glow Animation
Favicon
Discover the Ultimate Resource: Dog Breed Hub
Favicon
A Cozy Thanksgiving Feast with Hello Kitty and the Turkey ๐Ÿฆƒ๐Ÿ‚
Favicon
Build A Stunning Animated Travel Image Slider Using Html, Css, And JavaScript!
Favicon
testimonials with slide effects

Featured ones: