dev-resources.site
for different kinds of informations.
Day 24: Paint by Pixels π¨
Published at
12/24/2024
Categories
webdev
beginners
deno
webadvent
Author
valeriavg
Author
9 person written this
valeriavg
open
We did almost everything in a terminal except for maybe... Drawing!
And that's exactly what we're going to be doing today with the help of terminal-canvas.
Let's install it with e.g. deno add npm:terminal-canvas
and create a script, e.g. main.ts
:
import { Canvas } from "terminal-canvas";
const canvas = Canvas.create().reset();
for (let i = 8; i < 9; i++) {
canvas.moveTo(i, 1);
canvas.background("green").write(" ");
}
for (let i = 6; i < 11; i++) {
canvas.moveTo(i, 2);
canvas.background("green").write(" ");
}
for (let i = 4; i < 13; i++) {
canvas.moveTo(i, 3);
canvas.background("green").write(" ");
}
for (let i = 2; i < 15; i++) {
canvas.moveTo(i, 4);
canvas.background("green").write(" ");
}
for (let i = 6; i < 11; i++) {
canvas.moveTo(i, 5);
canvas.background("green").write(" ");
}
for (let i = 4; i < 13; i++) {
canvas.moveTo(i, 6);
canvas.background("green").write(" ");
}
for (let i = 2; i < 15; i++) {
canvas.moveTo(i, 7);
canvas.background("green").write(" ");
}
for (let i = 0; i < 17; i++) {
canvas.moveTo(i, 8);
canvas.background("green").write(" ");
}
for (let i = 6; i < 11; i++) {
canvas.moveTo(i, 9);
canvas.background("brown").write(" ");
}
canvas.flush()
console.log("");
console.log("");
Run with e.g. deno run -A main.ts
and enjoy this little tree:
But the tree is not decorated at all! That won't do it!
Let's change the code to:
import { Canvas } from "terminal-canvas";
const canvas = Canvas.create().reset();
for (let i = 8; i < 9; i++) {
canvas.moveTo(i, 1);
canvas.background("green").write(" ");
}
for (let i = 6; i < 11; i++) {
canvas.moveTo(i, 2);
canvas.background("green").write(" ");
}
for (let i = 4; i < 13; i++) {
canvas.moveTo(i, 3);
canvas.background("green").write(" ");
}
for (let i = 2; i < 15; i++) {
canvas.moveTo(i, 4);
canvas.background("green").write(" ");
}
for (let i = 6; i < 11; i++) {
canvas.moveTo(i, 5);
canvas.background("green").write(" ");
}
for (let i = 4; i < 13; i++) {
canvas.moveTo(i, 6);
canvas.background("green").write(" ");
}
for (let i = 2; i < 15; i++) {
canvas.moveTo(i, 7);
canvas.background("green").write(" ");
}
for (let i = 0; i < 17; i++) {
canvas.moveTo(i, 8);
canvas.background("green").write(" ");
}
for (let i = 6; i < 11; i++) {
canvas.moveTo(i, 9);
canvas.background("brown").write(" ");
}
const colors = ["red", "cyan", "white", "yellow", "blue", "magenta"];
setInterval(() => {
let x = Math.floor(Math.random() * colors.length);
canvas.moveTo(2, 8);
canvas.background(colors[x]).write(" ");
canvas.moveTo(3, 8);
canvas.background(colors[x]).write(" ");
x = (x+1) % colors.length
canvas.moveTo(5, 7);
canvas.background(colors[x]).write(" ");
canvas.moveTo(6, 7);
canvas.background(colors[x]).write(" ");
x = (x+1) % colors.length
canvas.moveTo(11, 7);
canvas.background(colors[x]).write(" ");
canvas.moveTo(12, 7);
canvas.background(colors[x]).write(" ");
x = (x+1) % colors.length
canvas.moveTo(8, 8);
canvas.background(colors[x]).write(" ");
canvas.moveTo(9, 8);
canvas.background(colors[x]).write(" ");
x = (x+1) % colors.length
canvas.moveTo(13, 8);
canvas.background(colors[x]).write(" ");
canvas.moveTo(14, 8);
canvas.background(colors[x]).write(" ");
x = (x+1) % colors.length
canvas.moveTo(8, 6);
canvas.background(colors[x]).write(" ");
canvas.moveTo(9, 6);
canvas.background(colors[x]).write(" ");
x = (x+1) % colors.length
canvas.moveTo(9, 2);
canvas.background(colors[x]).write(" ");
canvas.moveTo(10, 2);
canvas.background(colors[x]).write(" ");
x = (x+1) % colors.length
canvas.moveTo(5, 3);
canvas.background(colors[x]).write(" ");
canvas.moveTo(6, 3);
canvas.background(colors[x]).write(" ");
x = (x+1) % colors.length
canvas.moveTo(11, 4);
canvas.background(colors[x]).write(" ");
canvas.moveTo(12, 4);
canvas.background(colors[x]).write(" ");
x = (x+1) % colors.length
canvas.moveTo(6, 5);
canvas.background(colors[x]).write(" ");
canvas.moveTo(7, 5);
canvas.background(colors[x]).write(" ");
x = (x+1) % colors.length
canvas.moveTo(8, 4);
canvas.background(colors[x]).write(" ");
canvas.moveTo(9, 4);
canvas.background(colors[x]).write(" ");
canvas.flush();
}, 40);
console.log("");
console.log("");
Do you like what you see? Hope you do!
Happy holidays!
Liked the content and would love to have more of it all year long?
deno Article's
30 articles in total
Deno docker
read article
Day 24: Paint by Pixels π¨
currently reading
Day 22: How the Tables have turned π
read article
Day 20: Not a Dedent! π§
read article
Day 23: Terminal Images πΌοΈ
read article
Day 19: Highlight'em up! π
read article
Day 18: Got a millisecond? β±οΈ
read article
You can omit `run` from `deno run` command
read article
Day 17: Terminal Links & Other Escapes π
read article
Day 8: D-8 π±
read article
Day 7: Your input is valid ποΈ
read article
π Rust Coders, Donβt Miss These 25 Resource Picks
read article
Day 9: Terminal Forms π
read article
Dynamic DNS sync with Cloudflare
read article
Day 14: Keep on Spinning! β»οΈ
read article
π Automate Your PostgreSQL Backups with Ease! π³
read article
Day 21: In the name of Progress! π
read article
π Automate Your PostgreSQL Backups with Ease! π³
read article
Runtime challenge: Bun vs Node
read article
I built a Fullstack Deno 2 application
read article
Self Writing Lang Graph State
read article
Supabase Just Got More Powerful: Queue, Cron, and Background Tasks in Edge Functions
read article
Day 4: ASCII Art Fonts ποΈ
read article
Day 13: I love Boxes! π¦
read article
#2 Daily Rabbit Holes: Diving Deeper into Rust, V8, and the JavaScriptβ’οΈ Saga
read article
Day 12: Pico-co-colors π₯
read article
π Bun vs Deno: When to Use Each in Your Projects?
read article
Exploring Bun.js Over Deno: A Humble Developer's Tale
read article
Day 10: Ho-ho-hono! π₯
read article
Benchmarking in Node.js vs Deno: A Comprehensive Comparison
read article
Featured ones: