Skip to content

Instantly share code, notes, and snippets.

@sv-in
Forked from aemkei/LICENSE.txt
Created October 16, 2012 11:10
Show Gist options
  • Save sv-in/3898694 to your computer and use it in GitHub Desktop.
Save sv-in/3898694 to your computer and use it in GitHub Desktop.
JS: Digital Segment Display

Digital Segment Display - 140byt.es

Converts single digits into old-school ASCII string.

See my digital clock example.

Digit List

 _      _  _       _   _  _   _   _
| |  |  _| _| |_| |_  |_   | |_| |_|
|_|  | |_  _|   |  _| |_|  | |_|  _|

Sample Clock

         _  _     _  _  
| |_| .  _| _| . |_   | 
|   | . |_  _| .  _|  | 

For more information

See the 140byt.es site for a showcase of entries (built itself using 140-byte entries!), and follow @140bytes on Twitter.

To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to the wiki.

140byt.es is brought to you by Jed Schmidt, with help from Alex Kloss. It was inspired by work from Thomas Fuchs and Dustin Diaz.

function f(
a, // number to convert
b // placeholer: index in string to replace
){
return "\n_ |"[ // character map
b % 4 && // add line break for position 4 and 8
b % 2 + -~a // draw space, "|" or "_" based on position
] || "ცᕦဨဢᒦႂႊᅦႪႦ" // data is stored in unicode character
.charCodeAt(a) // convert character to integer
.toString(2) // convert to binary representaion
.replace( // parse binary data
/./g, f // analyse every char
)
}
<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Digital Clock - 140bytes</title>
<style type="text/css">
body { font-weight: bold; color: lime; background: black; }
pre { display: inline-block; width: auto; }
</style>
<div id="time"></div>
<script>
var digits =
function f(a,b){return"\n_ |"[b%4&&b%2+-~a]||"ცᕦဨဢᒦႂႊᅦႪႦ".charCodeAt(a).toString(2).replace(/./g,f)}
function pad(number){
return (number < 10 ? "0" : "") + number;
}
setInterval(function(){
var display = "",
date = new Date(),
time = pad(date.getHours()) +
pad(date.getMinutes()) +
pad(date.getSeconds()),
i;
for(i in time){
display += "<pre>" + digits(time[i]) + "</pre>";
if (i==1 || i==3){
display += "<pre>\n.\n.</pre>"
}
}
document.getElementById( "time" ).innerHTML = display
}, 1000);
</script>
function f(a,b){return"\n_ |"[b%4&&b%2+-~a]||"ცᕦဨဢᒦႂႊᅦႪႦ".charCodeAt(a).toString(2).replace(/./g,f)}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "toDigital",
"description": "Converts digits into an old-school digital ASCII string.",
"keywords": [
"number",
"digital",
"ascii",
"string"
],
"contributors": [
{
"name" : "Martin Kleppe",
"url" : "https://github.com/aemkei"
},
{
"name" : "@tsaniel",
"url" : "https://github.com/tsaniel"
}
]
}
<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Digits - 140bytes</title>
<style type="text/css"> pre { display: inline-block; width: auto;}</style>
<div id="output"></div>
<script>
var digits =
function f(a,b){return"\n_ |"[b%4&&b%2+-~a]||"ცᕦဨဢᒦႂႊᅦႪႦ".charCodeAt(a).toString(2).replace(/./g,f)}
for (var i=0; i<10; i++){
document.getElementById( "output" ).innerHTML += "<pre>" + digits(i) + "</pre>"
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment