Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Text animation
- -- by @beezing
- random = math.random
- drawText = writeTextAtPosition
- --print('Type a text:')
- txt = 'CODERS is awesome' --input()
- txtSize = 16 --32 is good for fullscreen size
- speed = 0 --0 means auto proportional to font size (smoother)
- box = {}
- box.x = 4
- box.y = 24
- box.w = 312 --768
- box.h = 740 --768
- function writeLegend(isPaused)
- local txt = 'Tap screen to '
- if isPaused then
- txt = txt .. 'start animation...'
- else
- txt = txt .. 'pause animation...'
- end
- setFontSize(12)
- setColor(128,128,128)
- drawText(txt,box.x+4,box.y+16)
- end
- function randomColor(val)
- if val == nil then val = 255 end
- local r,g,b = random(val), random(val), random(val)
- return r,g,b
- end
- function setStr(txt)
- setFontSize(txtSize)
- str = {}
- str.l = string.len(txt)
- str.w, str.h = sizeOfText(txt)
- for i = 1, str.l do
- chr = {}
- chr.ch = string.sub(txt,i,i)
- chr.r, chr.g, chr.b = randomColor(192)
- chr.x, chr.y = -1, -1
- chr.w, chr.h = sizeOfText(chr.ch)
- str[i] = chr
- end
- return str
- end
- function spread(str, box)
- for i = 1, str.l do
- str[i].x = box.x+random(box.w-box.x-2*str[i].w)+str[i].w
- str[i].y = box.y+random(box.h-box.y-2*str[i].h)+str[i].h
- end
- end
- function gather(str, box)
- x = box.x+random(box.w-box.x-str.w)
- y = box.y+random(box.h-box.y-str.h)+str.h
- for i = 1, str.l do
- str[i].x, str[i].y = x,y
- x = x + str[i].w
- --y = y + str[i].h
- end
- end
- function animX(strFrom, strTo, animSpeed)
- if (animSpeed == 0) or (animSpeed == nil) then
- animSpeed = strFrom[1].w/2
- end
- for i = 1, strFrom.l do
- if strFrom[i].x > strTo[i].x then
- strFrom[i].x = strFrom[i].x - animSpeed
- if strFrom[i].x < strTo[i].x then
- strFrom[i].x = strTo[i].x
- end
- elseif strFrom[i].x < strTo[i].x then
- strFrom[i].x = strFrom[i].x + animSpeed
- if strFrom[i].x > strTo[i].x then
- strFrom[i].x = strTo[i].x
- end
- else
- strFrom[i].x = strTo[i].x
- end
- end
- end
- function animY(strFrom, strTo, animSpeed)
- if (animSpeed == 0) or (animSpeed == nil) then
- animSpeed = strFrom[1].h/2
- end
- for i = 1, strFrom.l do
- if strFrom[i].y > strTo[i].y then
- strFrom[i].y = strFrom[i].y - animSpeed
- if strFrom[i].y < strTo[i].y then
- strFrom[i].y = strTo[i].y
- end
- elseif strFrom[i].y < strTo[i].y then
- strFrom[i].y = strFrom[i].y + animSpeed
- if strFrom[i].y > strTo[i].y then
- strFrom[i].y = strTo[i].y
- end
- else
- strFrom[i].y = strTo[i].y
- end
- end
- end
- function anim(strFrom, strTo, isSpread, isDirect, animSpeed)
- if isDirect then
- -- simultaneous motion
- if not isDoneX(strFrom,strTo) then
- animX(strFrom,strTo,animSpeed) end
- if not isDoneY(strFrom,strTo) then
- animY(strFrom,strTo,animSpeed) end
- else
- if not isSpread then
- -- x axis motion first on merge
- if not isDoneX(strFrom,strTo) then
- animX(strFrom,strTo,animSpeed)
- else
- if not isDoneY(strFrom,strTo) then
- animY(strFrom,strTo,animSpeed) end
- end
- else
- -- y axis motion first on spread
- if not isDoneY(strFrom,strTo) then
- animY(strFrom,strTo,animSpeed)
- else
- if not isDoneX(strFrom,strTo) then
- animX(strFrom,strTo,animSpeed) end
- end
- end
- end
- end
- function isDoneX(strFrom, strTo)
- local done = true
- for i = 1, strFrom.l do
- done = done and (strFrom[i].x == strTo[i].x)
- end
- return done
- end
- function isDoneY(strFrom, strTo)
- local done = true
- for i = 1, strFrom.l do
- done = done and (strFrom[i].y == strTo[i].y)
- end
- return done
- end
- function isDone(strFrom, strTo)
- local done = true
- for i = 1, strFrom.l do
- done = done and (strFrom[i].x == strTo[i].x)
- done = done and (strFrom[i].y == strTo[i].y)
- end
- return done
- end
- function drawStr(str, col)
- setFontSize(txtSize)
- for i = 1, str.l do
- if col == nil then
- setColor(str[i].r,str[i].g,str[i].b)
- else
- setColor(col,col,col)
- end
- drawText(str[i].ch, str[i].x, str[i].y)
- end
- end
- function tap()
- x,y = lastTouch()
- if (x > 0) and (y > 0) then
- isPaused = not isPaused
- -- also toggle motion type
- if not isPaused then
- isDirect = not isDirect
- end
- end
- end
- -- init first position
- strFrom = setStr(txt)
- spread(strFrom,box)
- strTo = setStr(txt)
- gather(strTo,box)
- drawStr(strFrom)
- isPaused = false
- isSpread = false
- isDirect = false
- while true do
- disableScreenRefresh()
- clear()
- tap() --tap to pause
- -- compute animation
- if not isPaused then
- anim(strFrom,strTo,isSpread,isDirect,speed)
- end
- -- draw text
- writeLegend(isPaused)
- drawStr(strTo,224)
- drawStr(strFrom)
- -- switch animation direction
- if isDone(strFrom,strTo) then
- isSpread = not isSpread
- strFrom = strTo
- strTo = setStr(txt)
- if isSpread then
- spread(strTo,box)
- else
- gather(strTo,box)
- end
- end
- enableScreenRefresh()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement