
The tree and its ornaments are rendered using ANSI escape codes, which allows you to see colors and special symbols in the console. The randomly assigned ornament colors provide a unique tree each time it’s generated.
How to use it:
1. Download the package and import ctree into your project.
import ctree from './src/ctree.js';
2. You can log the Christmas tree directly to your console using this simple method:
console.log(ctree());
How ctree.js Works:
Color Class: The script defines a Color class that represents an RGB color. Each Color instance can be converted into an ANSI escape code to apply the color to the console output.
- The
toAnsi()method converts RGB values into an ANSI escape code. - The
reset()method resets the color formatting back to default once the output is complete. - The
format()method applies the color to a given character, ensuring that it is displayed with the correct color in the console.
Predefined Colors: Three colors are set up—yellow, green, and brown—representing the tree’s ornaments, branches, and trunk. These colors are then used throughout the tree rendering process
applyColors Function: This function takes a string input (representing the tree) and applies colors to specific characters. For example, stars, ornaments, and branches are assigned colors like yellow for the ornaments and green for the branches.
- The
applyColorToChars()function is called for each part of the tree, modifying the input string by adding ANSI color codes to the right symbols.
Tree Structure: The tree is represented as a string, where characters like slashes, stars, and ornaments are used to depict the tree’s shape. These characters are then colored based on their role (e.g., yellow stars, green branches).
Here’s a portion of the tree structure before the colors are applied:
y|
y-y+y-
yA
g/g=gThe script identifies specific characters like |, +, and /, and replaces them with their colored counterparts. As the applyColors function processes the string, it swaps out these symbols with their colorful representations, giving you a Christmas tree full of bright ornaments and branches when logged in the console.
Changelog:
v1.1.0 (04/16/2026)
- Bugfixes
The post Add A Colorful Christmas Tree to Your Console – ctree.js appeared first on CSS Script.
Discover more from RSS Feeds Cloud
Subscribe to get the latest posts sent to your email.
