Created
July 9, 2015 10:33
-
-
Save silverweed/abe96e4dd5ade36768a9 to your computer and use it in GitHub Desktop.
Simple countdown clock in Coffeescript
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
window.startClock = (finalTime, startingDiffHrs) -> | |
$ = init() | |
drawCircle = (coord) -> | |
sw 2 | |
s circle coord... | |
fcol 0, 0, 0 | |
f circle coord[0], coord[1], coord[2]/20 | |
for i in [1..10] | |
a = 2*PI/10 * i | |
r = coord[2] | |
x = coord[0] + r * sin a | |
y = coord[1] - r * cos a | |
_r = if i % 2 is 0 then 7 / 8 * r else 10 / 11 * r | |
_x = coord[0] + _r * sin a | |
_y = coord[1] - _r * cos a | |
s line x, y, _x, _y | |
$.font = "bold 16px Monospace" | |
fcol 255, 0, 0 | |
_x = x - 30 if i < 5 | |
$.fillText (startingDiffHrs/24/10 * (10-i)), _x, _y | |
drawHours = (cx, cy, cr) -> | |
date = new Date() | |
# difference in seconds | |
diffs = (finalTime.getTime() - date.getTime()) / 1000 | |
# Hour 0:00 is -30 days | |
perc = (startingDiffHrs*3600 - diffs) / (startingDiffHrs*3600) | |
a = if abs(perc) <= 1 then 2*PI * perc else 0.1 | |
r = cr * 6 / 10 | |
[x, y] = [cx + r*sin(a), cy - r*cos(a)] | |
sw 10 | |
s line cx, cy, x, y | |
drawSeconds = (cx, cy, cr) -> | |
secs = new Date().getSeconds() | |
a = 2*PI * (secs / 60) | |
r = cr * 14 / 15 | |
[x, y] = [cx + r*sin(a), cy - r*cos(a)] | |
sw 4 | |
s line cx, cy, x, y | |
drawNumbers = (cx, cy, cr) -> | |
diff = (finalTime - new Date()) / 1000 | |
rdays = floor(diff/86400) | |
diff -= rdays*86400 | |
rhrs = floor(diff/3600) | |
diff -= rhrs*3600 | |
rmin = floor(diff/60) | |
diff -= rmin*60 | |
rsec = floor(diff) | |
step = 130 | |
x = (600 - step*4) / 2 | |
y = cy + 5/3*cr | |
$.font = "40px Monospace" | |
fcol 0, 0, 0, 0.7 | |
$.fillText "#{rdays}d,", x, y | |
$.fillText "#{rhrs}h,", x + step, y | |
$.fillText "#{rmin}m,", x + 2*step, y | |
$.fillText "#{rsec}s", x + 3*step, y | |
drawAll = (coord) -> | |
f = -> | |
clear() | |
drawCircle coord | |
drawHours coord... | |
drawSeconds coord... | |
drawNumbers coord... | |
setTimeout f, 1000 | |
f() | |
drawAll [300, 300, 150] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment