Advertisement
beezing

Text Animation - Coders

Nov 3rd, 2011
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.07 KB | None | 0 0
  1. -- Text animation
  2. -- by @beezing
  3.  
  4. random = math.random
  5. drawText = writeTextAtPosition
  6.  
  7. --print('Type a text:')
  8. txt = 'CODERS is awesome' --input()
  9. txtSize = 16 --32 is good for fullscreen size
  10. speed = 0 --0 means auto proportional to font size (smoother)
  11.  
  12. box = {}
  13. box.x = 4
  14. box.y = 24
  15. box.w = 312 --768
  16. box.h = 740 --768
  17.  
  18. function writeLegend(isPaused)
  19.   local txt = 'Tap screen to '
  20.   if isPaused then
  21.     txt = txt .. 'start animation...'
  22.   else
  23.     txt = txt .. 'pause animation...'
  24.   end
  25.  
  26.   setFontSize(12)
  27.   setColor(128,128,128)
  28.   drawText(txt,box.x+4,box.y+16)
  29. end
  30.  
  31. function randomColor(val)
  32.   if val == nil then val = 255 end
  33.   local r,g,b = random(val), random(val), random(val)
  34.   return r,g,b
  35. end
  36.  
  37. function setStr(txt)
  38.   setFontSize(txtSize)
  39.   str = {}
  40.   str.l = string.len(txt)
  41.   str.w, str.h = sizeOfText(txt)
  42.   for i = 1, str.l do
  43.     chr = {}
  44.     chr.ch = string.sub(txt,i,i)
  45.     chr.r, chr.g, chr.b = randomColor(192)
  46.     chr.x, chr.y = -1, -1
  47.     chr.w, chr.h = sizeOfText(chr.ch)
  48.     str[i] = chr
  49.   end
  50.   return str
  51. end
  52.  
  53. function spread(str, box)
  54.   for i = 1, str.l do
  55.     str[i].x = box.x+random(box.w-box.x-2*str[i].w)+str[i].w
  56.     str[i].y = box.y+random(box.h-box.y-2*str[i].h)+str[i].h
  57.   end
  58. end
  59.  
  60. function gather(str, box)
  61.   x = box.x+random(box.w-box.x-str.w)
  62.   y = box.y+random(box.h-box.y-str.h)+str.h
  63.   for i = 1, str.l do
  64.     str[i].x, str[i].y = x,y
  65.     x = x + str[i].w
  66.     --y = y + str[i].h
  67.   end
  68. end
  69.  
  70. function animX(strFrom, strTo, animSpeed)
  71.   if (animSpeed == 0) or (animSpeed == nil) then
  72.     animSpeed = strFrom[1].w/2
  73.   end
  74.  
  75.   for i = 1, strFrom.l do
  76.     if strFrom[i].x > strTo[i].x then
  77.       strFrom[i].x = strFrom[i].x - animSpeed
  78.       if strFrom[i].x < strTo[i].x then
  79.         strFrom[i].x = strTo[i].x
  80.       end
  81.     elseif strFrom[i].x < strTo[i].x then
  82.       strFrom[i].x = strFrom[i].x + animSpeed
  83.       if strFrom[i].x > strTo[i].x then
  84.         strFrom[i].x = strTo[i].x
  85.       end
  86.     else
  87.       strFrom[i].x = strTo[i].x
  88.     end
  89.   end
  90. end
  91.  
  92. function animY(strFrom, strTo, animSpeed)
  93.   if (animSpeed == 0) or (animSpeed == nil) then
  94.     animSpeed = strFrom[1].h/2
  95.   end
  96.  
  97.   for i = 1, strFrom.l do
  98.     if strFrom[i].y > strTo[i].y then
  99.       strFrom[i].y = strFrom[i].y - animSpeed
  100.       if strFrom[i].y < strTo[i].y then
  101.         strFrom[i].y = strTo[i].y
  102.       end
  103.     elseif strFrom[i].y < strTo[i].y then
  104.       strFrom[i].y = strFrom[i].y + animSpeed
  105.       if strFrom[i].y > strTo[i].y then
  106.         strFrom[i].y = strTo[i].y
  107.       end
  108.     else
  109.       strFrom[i].y = strTo[i].y
  110.     end
  111.   end
  112. end
  113.  
  114. function anim(strFrom, strTo, isSpread, isDirect, animSpeed)
  115.   if isDirect then
  116.     -- simultaneous motion
  117.     if not isDoneX(strFrom,strTo) then
  118.       animX(strFrom,strTo,animSpeed) end
  119.     if not isDoneY(strFrom,strTo) then
  120.       animY(strFrom,strTo,animSpeed) end
  121.   else
  122.     if not isSpread then
  123.       -- x axis motion first on merge
  124.       if not isDoneX(strFrom,strTo) then
  125.         animX(strFrom,strTo,animSpeed)
  126.       else
  127.         if not isDoneY(strFrom,strTo) then
  128.           animY(strFrom,strTo,animSpeed) end
  129.       end
  130.     else
  131.       -- y axis motion first on spread
  132.       if not isDoneY(strFrom,strTo) then
  133.         animY(strFrom,strTo,animSpeed)
  134.       else
  135.         if not isDoneX(strFrom,strTo) then
  136.           animX(strFrom,strTo,animSpeed) end
  137.       end
  138.     end
  139.   end
  140. end
  141.  
  142. function isDoneX(strFrom, strTo)
  143.   local done = true
  144.   for i = 1, strFrom.l do
  145.     done = done and (strFrom[i].x == strTo[i].x)
  146.   end
  147.   return done
  148. end
  149.  
  150. function isDoneY(strFrom, strTo)
  151.   local done = true
  152.   for i = 1, strFrom.l do
  153.     done = done and (strFrom[i].y == strTo[i].y)
  154.   end
  155.   return done
  156. end
  157.  
  158. function isDone(strFrom, strTo)
  159.   local done = true
  160.   for i = 1, strFrom.l do
  161.     done = done and (strFrom[i].x == strTo[i].x)
  162.     done = done and (strFrom[i].y == strTo[i].y)
  163.   end
  164.   return done
  165. end
  166.  
  167. function drawStr(str, col)
  168.   setFontSize(txtSize)
  169.   for i = 1, str.l do
  170.     if col == nil then
  171.       setColor(str[i].r,str[i].g,str[i].b)
  172.     else
  173.       setColor(col,col,col)
  174.     end
  175.     drawText(str[i].ch, str[i].x, str[i].y)
  176.   end
  177. end
  178.  
  179. function tap()
  180.   x,y = lastTouch()
  181.   if (x > 0) and (y > 0) then
  182.     isPaused = not isPaused
  183.     -- also toggle motion type
  184.     if not isPaused then
  185.       isDirect = not isDirect
  186.     end
  187.   end
  188. end
  189.  
  190. -- init first position
  191. strFrom = setStr(txt)
  192. spread(strFrom,box)
  193. strTo = setStr(txt)
  194. gather(strTo,box)
  195. drawStr(strFrom)
  196.  
  197. isPaused = false
  198. isSpread = false
  199. isDirect = false
  200.  
  201. while true do
  202.   disableScreenRefresh()
  203.   clear()
  204.   tap() --tap to pause
  205.  
  206.   -- compute animation
  207.   if not isPaused then
  208.     anim(strFrom,strTo,isSpread,isDirect,speed)
  209.   end
  210.  
  211.   -- draw text
  212.   writeLegend(isPaused)
  213.   drawStr(strTo,224)
  214.   drawStr(strFrom)
  215.  
  216.   -- switch animation direction
  217.   if isDone(strFrom,strTo) then
  218.     isSpread = not isSpread
  219.     strFrom = strTo
  220.  
  221.     strTo = setStr(txt)
  222.     if isSpread then
  223.       spread(strTo,box)
  224.     else
  225.       gather(strTo,box)
  226.     end
  227.   end
  228.  
  229.   enableScreenRefresh()
  230. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement