Add A Colorful Christmas Tree to Your Console – ctree.js
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.
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());
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.
toAnsi() method converts RGB values into an ANSI escape code.reset() method resets the color formatting back to default once the output is complete.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.
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=g The 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.
v1.1.0 (04/16/2026)
The post Add A Colorful Christmas Tree to Your Console – ctree.js appeared first on CSS Script.
Border Beam Vanilla is a Vanilla JavaScript library that decorates DOM elements with animated traveling…
A comprehensive review of browser privacy in 2026 reveals that Google Chrome remains highly vulnerable…
DETROIT, MI (WOWO) A competitive shift is underway on the Detroit River as the operator…
The European Commission’s newly launched Digital Age Verification App, unveiled on April 14, 2026, to…
MUNCIE, IND. (WOWO) A Muncie man is facing felony charges after police say he assaulted…
MUNCIE, IND. (WOWO) A Muncie man is facing felony charges after police say he assaulted…
This website uses cookies.