Animation.coffee 5.4 KB

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