Skip to content

Instantly share code, notes, and snippets.

@TheGU
Created February 10, 2025 10:08
Show Gist options
  • Save TheGU/a9a8d0343d78a1caeec1dea9f4b1c118 to your computer and use it in GitHub Desktop.
Save TheGU/a9a8d0343d78a1caeec1dea9f4b1c118 to your computer and use it in GitHub Desktop.
Create a hidden text with stereoscopic effect
var arr = "The old building sighed, each creak a testament to time. Dust motes danced in a lone sunbeam. A layer of grime settled under a neglected wooden bench. The sharp jangle of a telephone abruptly shattered the pervasive quiet. Near this lonely, forgotten corner, the cold steel doors of the elevator silently waited.".split(' ');
var secret = "under telephone Near elevator".split(' ');
var i = -1;
var MAX_WIDTH = 30;
var lines = [[]];
var _l = 0;
var result = '';
while (i++ < arr.length - 1) {
if ((_l + arr[i].length) < MAX_WIDTH) {
if (secret.indexOf(arr[i]) > -1) {arr[i] = (_l?' ':' ') + arr[i]}
lines[lines.length - 1].push(arr[i]);
_l += arr[i].length + 1;
} else {
_l = 0;
i--;
lines.push([]);
}
}
for (var i = 0; i < lines.length - 1; putText(lines[i++].join(chars))) {
// Align text
var chars = ' ';
while (lines[i].join(chars + ' ').length < MAX_WIDTH) {
chars += ' ';
}
}
putText(lines[i].join(' ') + ' ');
function putText(line) {
while (line.length < MAX_WIDTH) {
line = line.replace(' ', ' ');
}
// Make the illusion
result += line;
for (var val in secret) {
line = line.split(' '+secret[val]).join(' ' + secret[val] + ' ');
}
result += (' ' + line) + '\n';
}
console.log(result);
The old building sighed, each The old building sighed, each
creak a testament to time. creak a testament to time.
Dust motes danced in a lone Dust motes danced in a lone
sunbeam. A layer of grime sunbeam. A layer of grime
settled under a neglected settled under a neglected
wooden bench. The sharp wooden bench. The sharp
jangle of a telephone jangle of a telephone
abruptly shattered the abruptly shattered the
pervasive quiet. Near this pervasive quiet. Near this
lonely, forgotten corner, the lonely, forgotten corner, the
cold steel doors of the cold steel doors of the
elevator silently waited. elevator silently waited.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment