Advertisement
kevinritt

text animation

Feb 21st, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.36 KB | None | 0 0
  1. ;(function($, window, document, undefined){
  2.  
  3. function AnimateText(element, textObjects, options, animations){
  4. var self = this;
  5.  
  6. this.$element = $(element); //ul jQuery object
  7. this.textObjects = textObjects;
  8. this.lastTextObject; //last text object to complete its animation
  9. this.repeated = 0; //# of times the animation sequence has repeated
  10.  
  11. //Default options
  12. this.defaults = {
  13. repeat: true,
  14. element: {
  15. css: {
  16. position: 'relative',
  17. overflow: 'hidden',
  18. margin: 0,
  19. padding: 0,
  20. width: function(){
  21. return $(this).parent().width();
  22. },
  23. height: function(){
  24. return $(this).parent().height();
  25. },
  26. 'list-style': 'none'
  27. }
  28. },
  29. textObject: {
  30. css: {
  31. position: 'absolute',
  32. margin: 0,
  33. padding: 0
  34. },
  35. offset: 0,
  36. animation: 'explode'
  37. },
  38. position: {
  39. duration: 1000,
  40. easing: 'swing'
  41. }
  42. };
  43.  
  44. //Merge default options with user options
  45. this.options = $.extend(true, {}, this.defaults, options);
  46.  
  47. //Default animations
  48. this.animations = {
  49. "fadeIn": {
  50. positions: {
  51. start: {
  52. top: '50%',
  53. left: '50%',
  54. opacity: 0
  55. },
  56. 0: {
  57. opacity: 1,
  58. duration: 1200
  59. },
  60. 1: {
  61. duration: 1200
  62. },
  63. 2: {
  64. opacity: 0,
  65. duration: 300
  66. }
  67. }
  68. },
  69. "fadeOut": {
  70. positions: {
  71. start: {
  72. top: '50%',
  73. left: '50%',
  74. opacity: 1
  75. },
  76. 0: {
  77. duration: 1200
  78. },
  79. 1: {
  80. opacity: 0
  81. }
  82. }
  83. },
  84. "rightToLeft": {
  85. positions: {
  86. start: {
  87. width: '100%',
  88. left: '100%',
  89. opacity: 0,
  90. top: '50%',
  91. 'text-align': 'left'
  92. },
  93. 0: {
  94. left: '25%',
  95. opacity: 1,
  96. duration: 1200
  97. },
  98. 1: {
  99. duration: 1200
  100. },
  101. 2: {
  102. opacity: 0,
  103. duration: 1200
  104. }
  105. }
  106. },
  107. "leftToRight": {
  108. positions: {
  109. start: {
  110. width: '100%',
  111. right: '100%',
  112. opacity: 0,
  113. top: '50%',
  114. 'text-align': 'right'
  115. },
  116. 0: {
  117. right: '25%',
  118. opacity: 1,
  119. duration: 1200
  120. },
  121. 1: {
  122. duration: 1200
  123. },
  124. 2: {
  125. opacity: 0,
  126. duration: 1200
  127. }
  128. }
  129. },
  130. "explode": {
  131. positions: {
  132. start: {
  133. top: '40%',
  134. width: '100%',
  135. opacity: 0,
  136. 'font-size': '10px',
  137. 'text-align': 'center'
  138. },
  139. 0: {
  140. opacity: 1,
  141. duration: 0
  142. },
  143. 1: {
  144. top:'-10%',
  145. 'font-size': '150px',
  146. opacity: 0,
  147. duration: 1000
  148. }
  149. }
  150. },
  151. "implode": {
  152. positions: {
  153. start: {
  154. width: '100%',
  155. opacity: 0,
  156. top: '0%',
  157. 'font-size': '150px',
  158. 'text-align': 'center'
  159. },
  160. 0: {
  161. top: '30%',
  162. 'font-size': '40px',
  163. opacity: 1,
  164. duration: 1000
  165. },
  166. 1: {
  167. duration: 1200
  168. },
  169. 2: {
  170. opacity: 0,
  171. duration: 400
  172. }
  173. }
  174. }
  175. };
  176. //Merge default animations with user animations
  177. $.extend(true, this.animations, animations);
  178.  
  179. this.init();
  180. }
  181.  
  182. AnimateText.prototype = {
  183.  
  184. init: function(){
  185. var self = this;
  186.  
  187. //Hide ul element and set css
  188. this.$element.hide().css(this.options.element.css);
  189.  
  190. //Start animation as soon as animations and textObjects are loaded.
  191. this.loadAnimations();
  192. this.loadTextObjects(function(){
  193. self.$element.show();
  194. self.startAnimation();
  195. });
  196. },
  197.  
  198. loadAnimations: function(){
  199. var self = this;
  200.  
  201. //Validate every position in every animation
  202. $.each(this.animations, function(animationId, animation){
  203. animation.duration = 0;
  204.  
  205. $.each(animation.positions, function(positionId, position){
  206. if(positionId !== "start"){
  207. //Make sure position's duration is set
  208. if(typeof(position.duration) !== "number" || position.duration < 0){
  209. position.duration = self.options.position.duration;
  210. }
  211.  
  212. //Make sure position's easing property is set
  213. if(typeof(position.easing) === "undefined"){
  214. position.easing = self.options.position.easing;
  215. }
  216.  
  217. //Calculate the animation's total duration
  218. animation.duration += position.duration;
  219. }
  220. });
  221. });
  222. },
  223.  
  224. loadTextObjects: function(callback){
  225. var self = this;
  226.  
  227. //Keep track of the animation sequence's total duration
  228. totalDuration = 0;
  229.  
  230. //Validate every textObject
  231. $.each(this.textObjects, function(textObjectId, textObject){
  232. var textObjectDuration = 0;
  233.  
  234. textObject.id = textObjectId;
  235.  
  236. //Set text object element
  237. textObject.$element = self.$element.children("li:eq(" + textObjectId + ")");
  238.  
  239. //Make sure text object element exists
  240. if(!textObject.$element.length){
  241. return;
  242. }
  243.  
  244. //Set textObject to its default style
  245. textObject.$element.css(self.options.textObject.css).attr('id', textObject.id);
  246.  
  247. //Check for valid animation type
  248. if(typeof(textObject.animation) === "undefined" || (typeof(textObject.animation) === "string" && !textObject.animation in self.animations)){
  249. textObject.animation = self.options.textObject.animation;
  250. }
  251.  
  252. //Check for valid time offset
  253. if(typeof(textObject.offset) !== "number" || textObject.offset < 0){
  254. textObject.offset = self.options.textObject.offset;
  255. }
  256.  
  257. if(typeof(textObject.duration) !== "number" || textObject.duration < 0){
  258. textObject.duration = 0;
  259. }
  260.  
  261. //Merge textObject's positions with its animation's positions
  262. textObject.positions = $.extend(true, {}, self.animations[textObject.animation].positions, textObject.positions);
  263.  
  264. //Calculate textObject duration
  265. $.each(textObject.positions, function(positionId, position){
  266. if(positionId !== "start"){
  267. if(typeof(textObject.duration) === "number" && textObject.duration > 0){
  268. position.duration = (position.duration / self.animations[textObject.animation].duration) * textObject.duration;
  269. }
  270. else{
  271. textObjectDuration += position.duration;
  272. }
  273. }
  274. });
  275.  
  276. if(textObjectDuration > 0){
  277. textObject.duration = textObjectDuration;
  278. }
  279.  
  280. //Determine if this textObject will be last to complete its animation
  281. if((textObject.offset + textObject.duration) > totalDuration){
  282. totalDuration = (textObject.offset + textObject.duration);
  283. self.lastTextObject = textObject.id;
  284. }
  285.  
  286. textObject.status = 'ready';
  287. });
  288.  
  289. callback();
  290. },
  291.  
  292. startAnimation: function(){
  293. var self = this;
  294.  
  295. $.each(this.textObjects, function(textObjectId, textObject){
  296. //Set textObject to its start position
  297. textObject.$element.css(textObject.positions.start);
  298.  
  299. //Start textObject's animation after it's offset time has passed
  300. setTimeout(function(){
  301. self.animateTextObject(textObject.id, 0);
  302. }, textObject.offset);
  303. });
  304. },
  305.  
  306. stopAnimation: function(){
  307. this.$element.children("li").stop();
  308. },
  309.  
  310. animateTextObject: function(textObjectId, animationPosition){
  311. var self = this,
  312. textObject = this.textObjects[textObjectId],
  313. animation = this.animations[textObject.animation],
  314. test = function(){
  315. var to = textObject;
  316. console.log(to);
  317. return 'hey';
  318. };
  319.  
  320. //Finally, animate the textObject
  321. textObject.$element.animate(
  322. textObject.positions[animationPosition],
  323. textObject.positions[animationPosition].duration,
  324. textObject.positions[animationPosition].easing,
  325. function(){
  326. //Does this animation have another position? If so animate it.
  327. if(typeof(textObject.positions[(animationPosition + 1)]) !== "undefined"){
  328. self.animateTextObject(textObject.id, (animationPosition + 1));
  329. }
  330. //Is this the last text object in the group?
  331. else if(textObject.id === self.lastTextObject){
  332. self.stopAnimation();
  333.  
  334. //Repeat animations if repeat option is set to true or we're still under the set repeat limit
  335. if(self.options.repeat === true || (typeof(self.options.repeat) === "number" && self.repeated < self.options.repeat)){
  336. self.startAnimation();
  337. self.repeated++;
  338. }
  339. }
  340. }
  341. );
  342. }
  343. };
  344.  
  345. $.fn.animateText = function(textObjects, options, animations){
  346. return this.each(function(){
  347. new AnimateText(this, textObjects, options, animations);
  348. });
  349. };
  350.  
  351. })( jQuery );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement