chalk-animation

repository·master·Indexed 24 days ago

https://github.com/bokub/chalk-animation

A library for creating colorful, animated text output in terminal environments. It provides pre-defined animation styles including rainbow, pulse, glitch, radar, neon, and karaoke. Features include playback control via start() and stop(), text updates with replace(), and a CLI for running animations directly from the terminal. Version 2.0.3.

Tokens
1.4K
Snippets
7
Records
13
Agent score
80%

What's inside chalk-animation

  1. Automatic stopping of animations on console output

    master
    The chalk-animation package monkey-patches the standard console methods (log, info, warn, and error). If an animation is currently running, calling any of these console methods will automatically call .stop() on the active animation before printing the new message. This prevents animations from overwriting new log output in the terminal.
  2. Use chalk-animation in your code

    master

    Import chalkAnimation and call one of the animation methods (like rainbow, pulse, glitch, radar, neon, or karaoke) passing the text you want to animate. Animations start automatically upon creation.

    import chalkAnimation from 'chalk-animation';
    
    chalkAnimation.rainbow('Lorem ipsum dolor sit amet');
  3. Manually render frames with render() and frame()

    master

    If you want to control exactly when frames are displayed, call .stop() immediately after creation. You can then use .render() to display a frame or .frame() to retrieve the content of the next frame as a string.

    const rainbow = chalkAnimation.rainbow('Lorem ipsum').stop(); // Don't start the animation
    
    rainbow.render(); // Display the first frame
    
    const frame = rainbow.frame(); // Get the second frame
    console.log(frame);
  4. Update animated text with replace()

    master

    To change the text being animated without creating a new instance, use the .replace(newText) method.

    let str = 'Loading...';
    const rainbow = chalkAnimation.rainbow(str);
    
    // Add a new dot every second
    setInterval(() => {
    	rainbow.replace(str += '.');
    }, 1000);
  5. Control animation playback with start() and stop()

    master

    The animation instance returned by the animation method allows you to pause and resume playback using .stop() and .start().

    const rainbow = chalkAnimation.rainbow('Lorem ipsum'); // Animation starts
    
    setTimeout(() => {
        rainbow.stop(); // Animation stops
    }, 1000);
    
    setTimeout(() => {
        rainbow.start(); // Animation resumes
    }, 2000);
  6. Change animation speed

    master

    Pass a second parameter to the animation method to adjust the speed. The value should be a number greater than 0; the default is 1. A higher number results in a faster animation.

    chalkAnimation.rainbow('Lorem ipsum', 2); // Two times faster than default
  7. Use chalk-animation via CLI

    master

    If installed globally, you can run animations directly from your terminal using the chalk-animation command.

    Usage: $ chalk-animation <name> [options] [text...]

    Options:

    • --duration: Duration of the animation in ms (defaults to Infinity)
    • --speed: Animation speed as a number > 0 (defaults to 1)

    Example: $ chalk-animation rainbow Hello world!

    # Install package globally
    $ npm install --global chalk-animation
    
    # Run an animation
    $ chalk-animation rainbow Hello world!
  8. Control an active chalk-animation

    master

    When you call an animation method (like rainbow() or glitch()), it returns an animation object. You can use this object to manipulate the running animation:

    • .stop(): Stops the animation.
    • .start(): Restarts the animation.
    • .replace(str): Updates the text being animated with a new string.

    Note: Calling a new animation method will automatically stop the previously running animation to prevent terminal overlapping.

  9. Use chalk-animation to create text animations

    master

    The chalk-animation package provides several animated text effects that can be rendered to the terminal. Each animation method returns an animation object that allows you to control the playback.

    Available animation methods:

    • rainbow(str, speed): A shifting rainbow gradient.
    • pulse(str, speed): A red and white pulsing effect.
    • glitch(str, speed): A chaotic glitch/distortion effect.
    • radar(str, speed): A scanning shade effect.
    • neon(str, speed): A flashing neon color effect.
    • karaoke(str, speed): A color-fill effect that moves through the text.

    All methods accept a speed parameter (a number greater than 0) to control the playback rate.