Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Clock animation
- -- by @beezing
- abs = math.abs
- sin = math.sin
- cos = math.cos
- rad = math.rad
- floor = math.floor
- format = string.format
- drawText = writeTextAtPosition
- hour = 10
- minute = 17
- second = 5
- clockX = 165 --384
- clockY = 615 --384
- clockR = 100 --300 --play with this value to see it auto-adjust
- borderThick = 2
- hourThick = 6
- minuteThick = 3
- secondThick = 1
- digitalSize = 18 --play with this value too
- clockColor = 'BLACK'
- borderColor = 'GRAY'
- hourColor = 'BLUE'
- minuteColor = 'DARKBLUE'
- secondColor = 'RED'
- function colorName(name)
- if name == 'WHITE' then r,g,b = 255,255,255 end
- if name == 'BLACK' then r,g,b = 0, 0, 0 end
- if name == 'GRAY' then r,g,b = 192,192,192 end
- if name == 'RED' then r,g,b = 192, 0, 0 end
- if name == 'GREEN' then r,g,b = 0,192, 0 end
- if name == 'BLUE' then r,g,b = 0, 0,192 end
- if name == 'LIGHTRED' then r,g,b = 255, 0, 0 end
- if name == 'LIGHTGREEN' then r,g,b = 0,255, 0 end
- if name == 'LIGHTBLUE' then r,g,b = 0, 0,255 end
- if name == 'DARKRED' then r,g,b = 128, 0, 0 end
- if name == 'DARKGREEN' then r,g,b = 0,128, 0 end
- if name == 'DARKBLUE' then r,g,b = 0, 0,128 end
- return r,g,b
- end
- function circle(r, x, y)
- move(x+r, y)
- for i = 0, 3.14*2+0.1, 0.1 do
- draw(x+r*cos(i), y+r*sin(i))
- end
- end
- function clock12ToDeg(clockVal)
- if clockVal >= 12 then clockVal = clockVal-12 end
- degVal = 90 - (30 * clockVal)
- return degVal
- end
- function clock60ToDeg(clockVal)
- if clockVal >= 60 then clockVal = clockVal-60 end
- degVal = 90 - (6 * clockVal)
- return degVal
- end
- function degPos(degree, r,x,y)
- if r == nil then r = clockR end
- if x == nil then x = clockX end
- if y == nil then y = clockY end
- mx = x + r * cos(rad(degree))
- my = y + r * sin(rad(degree))
- return mx, my
- end
- function drawNumbers()
- setFontSize(digitalSize-2)
- local w,h = sizeOfText('0')
- -- adjust text position to clock size
- local r = clockR - w-2
- local x = clockX - w/2
- local y = clockY + h/2
- for i = 5, 55, 5 do
- local x,y = degPos(clock60ToDeg(i),r,x,y)
- drawText(i/5,x,y)
- end
- drawText('12', degPos(clock60ToDeg(0),r,x-w/2,y))
- end
- function drawClock()
- -- draw clock border
- setColor(colorName(borderColor))
- setLineWidth(borderThick)
- circle(clockR+borderThick+5, clockX,clockY)
- -- draw 60 clock dot marks
- setColor(colorName(clockColor))
- for i = 0, 59 do
- drawPoint(degPos(clock60ToDeg(i)))
- end
- -- draw 12 clock dot marks
- for i = 0, 59, 5 do
- circle(1, degPos(clock60ToDeg(i)))
- end
- -- draw 12 clock numbers, if size fit
- if clockR >= 4*(digitalSize-2) then
- drawNumbers()
- end
- end
- function drawHour(hourVal)
- local r = clockR*2/3 - 5
- move(clockX,clockY)
- setLineWidth(hourThick)
- setColor(colorName(hourColor))
- draw(degPos(clock12ToDeg(hourVal),r))
- end
- function drawMinute(minuteVal)
- local r = clockR - 5
- move(clockX,clockY)
- setLineWidth(minuteThick)
- setColor(colorName(minuteColor))
- draw(degPos(clock60ToDeg(minuteVal),r))
- end
- function drawSecond(secondVal)
- local r = clockR
- move(clockX,clockY)
- setLineWidth(secondThick)
- setColor(colorName(secondColor))
- draw(degPos(clock60ToDeg(secondVal),r))
- local r = clockR/5
- move(clockX,clockY)
- setLineWidth(secondThick*2)
- setColor(colorName(secondColor))
- draw(degPos(clock60ToDeg(secondVal+30),r))
- move(clockX,clockY)
- setLineWidth(secondThick*10)
- setColor(colorName(secondColor))
- circle(secondThick*3,clockX,clockY)
- end
- function drawDigital()
- setFontSize(digitalSize)
- w,h = sizeOfText('00:00:00')
- x = clockX - w/2
- y = clockY - clockR/2 + h
- -- draw out of clock area if not fit
- if w >= clockR-10 then y = clockY - clockR - h end
- ht = format('%02d', floor(hour))
- mt = format('%02d', floor(minute))
- st = format('%02d', floor(second))
- txt = ht..':'..mt..':'..st
- setColor(colorName(clockColor))
- drawText(txt,x,y)
- end
- -- adjust hand position to values
- minute = minute + second/60
- hour = hour + minute/60
- speed = 1 --0.111
- while true do
- disableScreenRefresh()
- clearCanvas()
- if second >= 60 then second = 0 end
- if minute >= 60 then minute = 0 end
- if hour >= 24 then hour = 0 end
- drawDigital()
- drawClock()
- drawHour(hour)
- drawMinute(minute)
- drawSecond(second)
- second = second + speed
- minute = minute + speed/60
- hour = hour + speed/3600
- enableScreenRefresh()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement