Animation.coffee 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. class Animation
  2. slideDown: (elem, props) ->
  3. if props.disableAnimation
  4. return
  5. h = elem.offsetHeight
  6. cstyle = window.getComputedStyle(elem)
  7. margin_top = cstyle.marginTop
  8. margin_bottom = cstyle.marginBottom
  9. padding_top = cstyle.paddingTop
  10. padding_bottom = cstyle.paddingBottom
  11. transition = cstyle.transition
  12. elem.style.boxSizing = "border-box"
  13. elem.style.overflow = "hidden"
  14. elem.style.transform = "scale(0.8)"
  15. elem.style.opacity = "0"
  16. elem.style.height = "0px"
  17. elem.style.marginTop = "0px"
  18. elem.style.marginBottom = "0px"
  19. elem.style.paddingTop = "0px"
  20. elem.style.paddingBottom = "0px"
  21. elem.style.transition = "none"
  22. setTimeout (->
  23. elem.className += " animate-back"
  24. elem.style.height = h+"px"
  25. elem.style.transform = "scale(1)"
  26. elem.style.opacity = "1"
  27. elem.style.marginTop = margin_top
  28. elem.style.marginBottom = margin_bottom
  29. elem.style.paddingTop = padding_top
  30. elem.style.paddingBottom = padding_bottom
  31. ), 1
  32. elem.addEventListener "transitionend", ->
  33. elem.classList.remove("animate-back")
  34. elem.style.transition = elem.style.transform = elem.style.opacity = elem.style.height = null
  35. elem.style.boxSizing = elem.style.marginTop = elem.style.marginBottom = null
  36. elem.style.paddingTop = elem.style.paddingBottom = elem.style.overflow = null
  37. slideUp: (elem, remove_func, props) ->
  38. elem.className += " animate-back"
  39. elem.style.boxSizing = "border-box"
  40. elem.style.height = elem.offsetHeight+"px"
  41. elem.style.overflow = "hidden"
  42. elem.style.transform = "scale(1)"
  43. elem.style.opacity = "1"
  44. setTimeout (->
  45. elem.style.height = "0px"
  46. elem.style.marginTop = "0px"
  47. elem.style.marginBottom = "0px"
  48. elem.style.paddingTop = "0px"
  49. elem.style.paddingBottom = "0px"
  50. elem.style.transform = "scale(0.8)"
  51. elem.style.borderTopWidth = "0px"
  52. elem.style.borderBottomWidth = "0px"
  53. elem.style.opacity = "0"
  54. ), 1
  55. elem.addEventListener("transitionend", remove_func)
  56. showRight: (elem, props) ->
  57. elem.className += " animate"
  58. elem.style.opacity = 0
  59. elem.style.transform = "TranslateX(-20px) Scale(1.01)"
  60. setTimeout (->
  61. elem.style.opacity = 1
  62. elem.style.transform = "TranslateX(0px) Scale(1)"
  63. ), 1
  64. elem.addEventListener "transitionend", ->
  65. elem.classList.remove("animate")
  66. elem.style.transform = elem.style.opacity = null
  67. show: (elem, props) ->
  68. delay = arguments[arguments.length-2]?.delay*1000 or 1
  69. elem.className += " animate"
  70. elem.style.opacity = 0
  71. setTimeout (->
  72. elem.style.opacity = 1
  73. ), delay
  74. elem.addEventListener "transitionend", ->
  75. elem.classList.remove("animate")
  76. elem.style.opacity = null
  77. addVisibleClass: (elem, props) ->
  78. setTimeout ->
  79. elem.classList.add("visible")
  80. termLines: (elem, projection_options, selector, props) ->
  81. lines = elem.innerHTML.split("\n")
  82. delay = props.delay or 0
  83. delay_step = props.delay_step or 0.05
  84. back = []
  85. for line in lines
  86. line = line.replace(/(\.+)(.*?)$/, "<span class='dots'>$1</span><span class='result'>$2</span>", line)
  87. back.push("<span style='transition-delay: #{delay}s'>#{line}</span>")
  88. delay += delay_step
  89. setTimeout ( ->
  90. elem.classList.add("visible")
  91. ), 100
  92. elem.innerHTML = back.join("\n")
  93. scramble: (elem) ->
  94. text_original = elem.value
  95. chars = elem.value.split("")
  96. chars = chars.filter (char) ->
  97. return char != "\n" and char != "\r" and char != " " and char != "​"
  98. #replaces = ["|", "[", "]", "/", "\\", "*", "-", "$", "~", "^", "#", ">", "<", "(", ")", "+", "%", "=", "!"]
  99. replaces = ["⠋", '⠙', '⠹', '⠒', '⠔', '⠃', '⡳', '⠁', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']
  100. replaces.sort ->
  101. return 0.5-Math.random()
  102. frame = 0
  103. timer = setInterval ( ->
  104. for i in [0..Math.round(text_original.length/20)]
  105. char = chars.shift()
  106. elem.value = elem.value.replace(char, replaces[(frame+i) % replaces.length])
  107. if chars.length == 0
  108. clearInterval(timer)
  109. frame += 1
  110. ), 50
  111. ###
  112. showScramble: (elem, props) ->
  113. text_original = elem.innerText
  114. chars = elem.innerText.split("")
  115. # Convert characters to whitespace
  116. clear_chars = chars.map (char) ->
  117. if char != "\n" and char != "\r" and char != " " and char != "​"
  118. return " "
  119. else
  120. return char
  121. elem.innerText = clear_chars.join("")
  122. replaces = ["|", "[", "]", "/", "\\", "*", "-", "$", "~", "^", "#", ">", "<", "(", ")", "+", "%", "=", "!"]
  123. replaces.sort ->
  124. return 0.5-Math.random()
  125. frame = 0
  126. timer = 0
  127. replace_show = ->
  128. for i in [0..10]
  129. replace = replaces[Math.floor(Math.random()*(replaces.length-1))]
  130. elem.innerText = elem.innerText.replace(" ", replace)
  131. elem.innerText = elem.innerText.replace(replace, replaces[frame % (replaces.length-1)])
  132. frame += 1
  133. if frame > chars.length/10
  134. clearInterval(timer)
  135. timer = setInterval text_show, 20
  136. text_show = ->
  137. for i in [0..10]
  138. clearInterval(timer)
  139. timer = setInterval replace_show, 20
  140. scramble2: (elem, props) ->
  141. text_original = elem.innerText
  142. chars = elem.innerText.split("")
  143. chars_num = chars.length
  144. frame = 0
  145. timer = setInterval ( ->
  146. for replace in ["|", "[", "]", "/", "\\", "*", "-", "$", "~", "^", "#", ">", "<", "(", ")", "+", "%", "=", "!"]
  147. index = Math.round(Math.random()*chars_num)
  148. if chars[index] != "\n" and chars[index] != "\r" and chars[index] != " " and chars[index] != "​" # Null character
  149. chars[index] = replace
  150. elem.innerText = chars.join("")
  151. frame += 1
  152. if frame > 100
  153. clearInterval(timer)
  154. ), 20
  155. @
  156. ###
  157. window.Animation = new Animation()