Created
November 11, 2013 07:40
-
-
Save alexzhou/7409332 to your computer and use it in GitHub Desktop.
binary clock
二进制时钟
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title></title> | |
</head> | |
<body> | |
<style> | |
.clock .one{ | |
width: 16px; | |
height: 16px; | |
background: green; | |
margin-right: 20px; | |
} | |
.clock .zero{ | |
width: 16px; | |
height: 16px; | |
background: #ccc; | |
margin-right: 20px; | |
} | |
</style> | |
<div class="clock"> | |
<h2><b>h</b></h2> | |
<div id="hour"></div> | |
</div> | |
<div class="clock"> | |
<h2><b>m</b></h2> | |
<div id="min"></div> | |
</div> | |
<div class="clock"> | |
<h2><b>s</b></h2> | |
<div id="sec"></div> | |
</div> | |
<script type="text/javascript"> | |
setInterval("setclock()",1000); | |
function setclock(){ | |
var time = new Date(); | |
var hours = toBin(time.getHours()); | |
var mins = toBin(time.getMinutes()); | |
var secs = toBin(time.getSeconds()); | |
render(hours,'hour'); | |
render(mins,'min'); | |
render(secs,'sec'); | |
} | |
function toBin(dec) { | |
var bits = []; | |
var dividend = dec; | |
var remainder = 0; | |
while (dividend >= 2) { | |
remainder = dividend % 2; | |
bits.push(remainder); | |
dividend = (dividend - remainder) / 2; | |
} | |
bits.push(dividend); | |
bits.reverse(); | |
return bits; | |
} | |
function render(value,type){ | |
var html = ''; | |
if(type=='hour'){ | |
html = makedot(value,5); | |
document.getElementById('hour').innerHTML=html; | |
}else if(type=='min'){ | |
html = makedot(value,6); | |
document.getElementById('min').innerHTML=html; | |
}else if(type=='sec'){ | |
html = makedot(value,6); | |
document.getElementById('sec').innerHTML=html; | |
} | |
} | |
function makedot(value,nums){ | |
var i=null; | |
var html = ''; | |
var length = value.length; | |
for(var i = 0;i<length;i++){ | |
if(value[i]){ | |
html = html+'<span class="one"> </span>'; | |
}else{ | |
html = html+'<span class="zero"> </span>'; | |
} | |
} | |
while(length < nums){ | |
html = '<span class="zero"> </span>'+html; | |
length++; | |
} | |
return html; | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment