Skip to content

Instantly share code, notes, and snippets.

@subzey
Last active November 21, 2017 16:56
Show Gist options
  • Save subzey/355eff6cef3d4c835e81dec7eb3e7f83 to your computer and use it in GitHub Desktop.
Save subzey/355eff6cef3d4c835e81dec7eb3e7f83 to your computer and use it in GitHub Desktop.
Spiraling Braille

An experiment with Braille unicode chars in terminal.

run npx https://gist.github.com/subzey/355eff6cef3d4c835e81dec7eb3e7f83 in your terminal

#!/usr/bin/env node
if (!process.stdout.isTTY) {
process.stderr.write('stdout is not a terminal');
process.exit(1);
}
const clearScreenSeq = '\u001b[H\u001b[2J';
const clearScreenSeqLen = Buffer.byteLength(clearScreenSeq);
const maths = (x, y, now, tie) => {
const angle = Math.atan2(x, y);
const dist = Math.hypot(x, y);
const phase = Math.cos(now / -2 + dist / 20) * 4 + now;
return Math.cos(phase + angle * 6) > tie;
}
const frame = () => {
const height = process.stdout.rows;
const width = process.stdout.columns;
const buf = new Buffer(width * height * 3 + height - 1 + clearScreenSeqLen);
let ptr = 0;
ptr = buf.write(clearScreenSeq);
const now = Date.now() / 500;
for (let row = 0; row < height; row++) {
const y = row * 2 - height;
for (let column = 0; column < width; column++) {
const x = column - width / 2;
const crossCharTie = column % 2 ? .8 : 1.25;
buf[ptr] = 0xE2;
buf[ptr + 1] = (
0xA0
|
maths(x + .25, y + 1.75, now, .6 * crossCharTie)
|
maths(x + .75, y + 1.75, now, -.6 * crossCharTie) << 1
);
buf[ptr + 2] = (
0x80
|
maths(x + .25, y + .25, now, -.3)
|
maths(x + .25, y + .75, now, .6) << 1
|
maths(x + .25, y + 1.25, now, -.3 * crossCharTie) << 2
|
maths(x + .75, y + .25, now, .3) << 3
|
maths(x + .75, y + .75, now, -.6) << 4
|
maths(x + .75, y + 1.25, now, .3 * crossCharTie) << 5
);
ptr += 3;
}
if (row < height - 1) {
buf[ptr] = 0x0A;
ptr++;
}
}
setTimeout(frame, 33);
process.stdout.write(buf);
}
frame();
{ "private": true, "main": "index.js", "name": "bpiraling-braille", "version": "0.0.1", "bin": "index.js" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment