text.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.fx.text"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.fx.text"] = true;
  8. dojo.provide("dojox.fx.text");
  9. // dojo.require("dojox.fx._split");
  10. dojo.require("dojo.fx");
  11. dojo.require("dojo.fx.easing");
  12. dojox.fx.text._split = function(/*Object*/ args){
  13. // summary: Split a block of text into words or letters
  14. //
  15. // description:
  16. // Returns an animation that will split the node into a grid
  17. // of pieces that move independently.
  18. //
  19. // NOTE:
  20. // In some rendering engines, the text will appear to "jump" from its initial position
  21. // when the animation begins. To work around this bug, enclose the node's text in a <p> or <div>.
  22. //
  23. // args:
  24. // args.crop: Boolean - If true, pieces will be positioned relatively rather than absolutely
  25. // args.text: String - Text to place inside the node (otherwise node.innerHTML is used)
  26. // args.words: Boolean - If true, the text will be split into words rather than characters
  27. // args.pieceAnimation: Function(piece, pieceCoords, nodeCoords, number, numPieces)
  28. // - Returns either the dojo.Animation or an array of dojo.Animation objects for the piece;
  29. // pieceCoords is the result of dojo.coords(piece, true);
  30. // nodeCoords is the result of dojo.coords(args.node, true);
  31. // number is the piece's position in the array of pieces, and numPieces is the array.length
  32. var node = args.node = dojo.byId(args.node),
  33. s = node.style,
  34. cs = dojo.getComputedStyle(node),
  35. nodeCoords = dojo.coords(node, true);
  36. args.duration = args.duration || 1000;
  37. args.words = args.words || false;
  38. var originalHTML = (args.text && typeof(args.text) == "string") ? args.text : node.innerHTML,
  39. originalHeight = s.height,
  40. originalWidth = s.width,
  41. animations = [];
  42. dojo.style(node, {
  43. height: cs.height,
  44. width: cs.width
  45. });
  46. // The following regular expression courtesy of Phil Haack
  47. // http://haacked.com/archive/2004/10/25/usingregularexpressionstomatchhtml.aspx
  48. var tagReg = /(<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>)/g;
  49. // Translation: /(HTML tag plus spaces)|(word/letter without '<' plus spaces)/g
  50. var reg = (args.words ?
  51. /(<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>)\s*|([^\s<]+\s*)/g :
  52. /(<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>)\s*|([^\s<]\s*)/g
  53. );
  54. // Split the text into pieces
  55. var pieces = (typeof args.text == "string") ? args.text.match(reg) : node.innerHTML.match(reg);
  56. var html = "";
  57. var numPieces = 0;
  58. var number = 0;
  59. for(var i = 0; i < pieces.length; i++){
  60. var piece = pieces[i];
  61. if(!piece.match(tagReg)){
  62. html += "<span>" + piece + "</span>";
  63. numPieces++;
  64. }else{
  65. html += piece;
  66. }
  67. }
  68. node.innerHTML = html;
  69. // Find the newly-created spans and create their animations
  70. function animatePieces(piece){
  71. var next = piece.nextSibling;
  72. if(piece.tagName == "SPAN" && piece.childNodes.length == 1 && piece.firstChild.nodeType == 3){
  73. var pieceCoords = dojo.coords(piece, true);
  74. number++;
  75. dojo.style(piece, {
  76. padding: 0,
  77. margin: 0,
  78. top: (args.crop ? "0px" : pieceCoords.t + "px"),
  79. left: (args.crop ? "0px" : pieceCoords.l + "px"),
  80. display: "inline"
  81. });
  82. var pieceAnimation = args.pieceAnimation(piece, pieceCoords, nodeCoords, number, numPieces);
  83. if(dojo.isArray(pieceAnimation)){
  84. // if pieceAnimation is an array, append its elements
  85. animations = animations.concat(pieceAnimation);
  86. }else{
  87. // otherwise, append it
  88. animations[animations.length] = pieceAnimation;
  89. }
  90. }else if(piece.firstChild){
  91. animatePieces(piece.firstChild);
  92. }
  93. if(next){
  94. animatePieces(next);
  95. }
  96. }
  97. animatePieces(node.firstChild);
  98. var anim = dojo.fx.combine(animations);
  99. dojo.connect(anim, "onEnd", anim, function(){
  100. node.innerHTML = originalHTML;
  101. dojo.style(node, {
  102. height: originalHeight,
  103. width: originalWidth
  104. });
  105. });
  106. if(args.onPlay){
  107. dojo.connect(anim, "onPlay", anim, args.onPlay);
  108. }
  109. if(args.onEnd){
  110. dojo.connect(anim, "onEnd", anim, args.onEnd);
  111. }
  112. return anim; // dojo.Animation
  113. };
  114. dojox.fx.text.explode = function(/*Object*/ args){
  115. // summary: Explode a block of text into words or letters
  116. //
  117. // description:
  118. // Returns an animation that will split the text into a spans
  119. // of words or characters that fly away from the center.
  120. //
  121. // args:
  122. // args.crop: Boolean - If true, pieces will be positioned relatively rather than absolutely
  123. // args.words: Boolean - If true, text will be split into words rather than characters
  124. // args.random: Float - If set, pieces fly to random distances, for random durations,
  125. // and in slightly random directions. The value defines how much
  126. // randomness is introduced.
  127. // args.distance: Float - Multiplier for the distance the pieces fly (even when random)
  128. // args.fade: Boolean - If true, pieces fade out while in motion (default is true)
  129. // args.fadeEasing: Function - If args.fade is true, the fade animations use this easing function
  130. // args.unhide: Boolean - If true, the animation is reversed
  131. // args.sync: Boolean - If args.unhide is true, all the pieces converge at the same time
  132. // (default is true)
  133. var node = args.node = dojo.byId(args.node);
  134. var s = node.style;
  135. args.distance = args.distance || 1;
  136. args.duration = args.duration || 1000;
  137. args.random = args.random || 0;
  138. if(typeof(args.fade) == "undefined"){
  139. args.fade = true;
  140. }
  141. if(typeof(args.sync) == "undefined"){
  142. args.sync = true;
  143. }
  144. args.random = Math.abs(args.random);
  145. // Returns the animation object for each piece
  146. args.pieceAnimation = function(piece, pieceCoords, coords, number, numPieces){
  147. var pieceHeight = pieceCoords.h;
  148. var pieceWidth = pieceCoords.w;
  149. var distance = args.distance * 2;
  150. var duration = args.duration;
  151. var startTop = parseFloat(piece.style.top);
  152. var startLeft = parseFloat(piece.style.left);
  153. var delay = 0;
  154. var randomX = 0;
  155. var randomY = 0;
  156. if(args.random){
  157. var seed = (Math.random() * args.random) + Math.max(1 - args.random, 0);
  158. distance *= seed;
  159. duration *= seed;
  160. // To syncronize, give each piece an appropriate delay so they end together
  161. delay = ((args.unhide && args.sync) || (!args.unhide && !args.sync)) ? (args.duration - duration) : 0;
  162. // Slightly randomize the direction of each piece
  163. randomX = Math.random() - 0.5;
  164. randomY = Math.random() - 0.5;
  165. }
  166. var distanceY = ((coords.h - pieceHeight) / 2 - (pieceCoords.y - coords.y));
  167. var distanceX = ((coords.w - pieceWidth) / 2 - (pieceCoords.x - coords.x));
  168. var distanceXY = Math.sqrt(Math.pow(distanceX, 2) + Math.pow(distanceY, 2));
  169. var endTop = startTop - distanceY * distance + distanceXY * randomY;
  170. var endLeft = startLeft - distanceX * distance + distanceXY * randomX;
  171. // Create the animation objects for the piece
  172. // These are separate anim objects so they can have different curves
  173. var pieceSlide = dojo.animateProperty({
  174. node: piece,
  175. duration: duration,
  176. delay: delay,
  177. easing: (args.easing || (args.unhide ? dojo.fx.easing.sinOut : dojo.fx.easing.circOut)),
  178. beforeBegin: (args.unhide ? function(){
  179. if(args.fade){
  180. //piece.style.opacity = 0;
  181. dojo.style(piece,"opacity", 0);
  182. }
  183. piece.style.position = args.crop ? "relative" : "absolute";
  184. piece.style.top = endTop + "px";
  185. piece.style.left = endLeft + "px";
  186. } : function(){piece.style.position = args.crop ? "relative" : "absolute";}),
  187. properties: {
  188. top: (args.unhide ? { start: endTop, end: startTop } : { start: startTop, end: endTop }),
  189. left: (args.unhide ? { start: endLeft, end: startLeft } : { start: startLeft, end: endLeft })
  190. }
  191. });
  192. if(args.fade){
  193. var pieceFade = dojo.animateProperty({
  194. node: piece,
  195. duration: duration,
  196. delay: delay,
  197. easing: (args.fadeEasing || dojo.fx.easing.quadOut),
  198. properties: {
  199. opacity: (args.unhide ? {start: 0, end: 1} : {end: 0})
  200. }
  201. });
  202. // return both animations as an array
  203. return (args.unhide ? [pieceFade, pieceSlide] : [pieceSlide, pieceFade]);
  204. }else{
  205. // Otherwise return only the slide animation
  206. return pieceSlide;
  207. }
  208. };
  209. var anim = dojox.fx.text._split(args);
  210. return anim; // dojo.Animation
  211. };
  212. dojox.fx.text.converge = function(/*Object*/ args){
  213. args.unhide = true;
  214. return dojox.fx.text.explode(args);
  215. };
  216. dojox.fx.text.disintegrate = function(/*Object*/ args){
  217. // summary: Split a block of text into words or letters and let them fall
  218. //
  219. // description:
  220. // Returns an animation that will split the text into spans of words
  221. // or characters that drop.
  222. //
  223. // args:
  224. // args.crop: Boolean - If true, pieces will be positioned relatively rather than absolutely
  225. // args.words: Boolean - If true, text will be split into words rather than characters
  226. // args.interval: Float - The number of milliseconds between each piece's animation
  227. // args.distance: Float - The number of the node's heights to drop (default is 1.5)
  228. // args.fade: Boolean - If true, pieces fade out while in motion (default is true)
  229. // args.random: Float - If set, pieces fall in random order. The value defines how much
  230. // randomness is introduced
  231. // args.reverseOrder: Boolean - If true, pieces animate in reversed order
  232. // args.unhide: Boolean - If true, the peices fall from above and land in place
  233. var node = args.node = dojo.byId(args.node);
  234. var s = node.style;
  235. args.duration = args.duration || 1500;
  236. args.distance = args.distance || 1.5;
  237. args.random = args.random || 0;
  238. if(!args.fade){
  239. args.fade = true;
  240. }
  241. var random = Math.abs(args.random);
  242. // Returns the animation object for each piece
  243. args.pieceAnimation = function(piece, pieceCoords, coords, number, numPieces){
  244. var pieceHeight = pieceCoords.h;
  245. var pieceWidth = pieceCoords.w;
  246. var interval = args.interval || (args.duration / (1.5 * numPieces));
  247. var duration = (args.duration - numPieces * interval);
  248. var randomDelay = Math.random() * numPieces * interval;
  249. // If distance is negative, start from the top right instead of bottom left
  250. var uniformDelay = (args.reverseOrder || args.distance < 0) ?
  251. (number * interval) : ((numPieces - number) * interval);
  252. var delay = randomDelay * random + Math.max(1 - random, 0) * uniformDelay;
  253. // Create the animation object for the piece
  254. var properties = {};
  255. if(args.unhide){
  256. properties.top = {
  257. start: (parseFloat(piece.style.top) - coords.h * args.distance),
  258. end: parseFloat(piece.style.top)
  259. };
  260. if(args.fade){
  261. properties.opacity = {start: 0, end: 1};
  262. }
  263. }else{
  264. properties.top = {end: (parseFloat(piece.style.top) + coords.h * args.distance)};
  265. if(args.fade){
  266. properties.opacity = {end: 0};
  267. }
  268. }
  269. var pieceAnimation = dojo.animateProperty({
  270. node: piece,
  271. duration: duration,
  272. delay: delay,
  273. easing: (args.easing || (args.unhide ? dojo.fx.easing.sinIn : dojo.fx.easing.circIn)),
  274. properties: properties,
  275. beforeBegin: (args.unhide ? function(){
  276. if(args.fade){
  277. // piece.style.opacity = 0;
  278. dojo.style(piece, "opacity", 0);
  279. }
  280. piece.style.position = args.crop ? "relative" : "absolute";
  281. piece.style.top = properties.top.start + "px";
  282. } : function(){ piece.style.position = args.crop ? "relative" : "absolute";})
  283. });
  284. return pieceAnimation;
  285. };
  286. var anim = dojox.fx.text._split(args);
  287. return anim; // dojo.Animation
  288. };
  289. dojox.fx.text.build = function(/*Object*/ args){
  290. args.unhide = true;
  291. return dojox.fx.text.disintegrate(args);
  292. };
  293. dojox.fx.text.blockFadeOut = function(/*Object*/ args){
  294. // summary: Split a block of text into words or letters and fade them
  295. //
  296. // description:
  297. // Returns an animation that will split the text into spans of words
  298. // or characters that fade in or out.
  299. //
  300. // args:
  301. // args.words: Boolean - If true, text will be split into words rather than characters
  302. // args.interval: Float - The number of milliseconds between each piece's animation (default is 0)
  303. // args.random: Float - If true, pieces have a random delay. The value defines how much
  304. // randomness is introduced
  305. // args.reverseOrder: Boolean - If true, pieces animate in reversed order
  306. // args.unhide: Boolean - If true, the animation is reversed
  307. var node = args.node = dojo.byId(args.node);;
  308. var s = node.style;
  309. args.duration = args.duration || 1000;
  310. args.random = args.random || 0;
  311. var random = Math.abs(args.random);
  312. // Returns the animation object for each piece
  313. args.pieceAnimation = function(piece, pieceCoords, coords, number, numPieces){
  314. var interval = args.interval || (args.duration / (1.5 * numPieces));
  315. var duration = (args.duration - numPieces * interval);
  316. var randomDelay = Math.random() * numPieces * interval;
  317. // If interval or random is negative, start from the bottom instead of top
  318. var uniformDelay = (args.reverseOrder) ?
  319. ((numPieces - number) * interval) : (number * interval);
  320. var delay = randomDelay * random + Math.max(1 - random, 0) * uniformDelay;
  321. // Create the animation object for the piece
  322. var pieceAnimation = dojo.animateProperty({
  323. node: piece,
  324. duration: duration,
  325. delay: delay,
  326. easing: (args.easing || dojo.fx.easing.sinInOut),
  327. properties: {
  328. opacity: (args.unhide ? {start: 0, end: 1} : {end:0})
  329. },
  330. beforeBegin: (args.unhide ? function(){ dojo.style(piece,"opacity",0); } : undefined)
  331. });
  332. return pieceAnimation;
  333. };
  334. var anim = dojox.fx.text._split(args);
  335. return anim; // dojo.Animation
  336. };
  337. dojox.fx.text.blockFadeIn = function(/*Object*/ args){
  338. args.unhide = true;
  339. return dojox.fx.text.blockFadeOut(args);
  340. };
  341. dojox.fx.text.backspace = function(/*Object*/ args){
  342. // summary: Split a block of text into words or letters and backspace them in sequence
  343. //
  344. // description:
  345. // Returns an animation that will split the text into spans of words
  346. // or characters that appear as if they were being backspaced (or typed) in real-time.
  347. //
  348. // args:
  349. // args.interval: Float - The number of milliseconds between each piece's animation
  350. // (default is determined by text length and args.duration);
  351. // args.wordDelay: Integer - The number of milliseconds between each word
  352. // (only effective when args.unhide = true)
  353. // args.fixed: Boolean - If true, only style.opacity changes; otherwise, style.display
  354. // changes between none and inline, adding realism (default = false)
  355. // args.random: Float - If true, pieces have a random delay. The value defines how much
  356. // randomness is introduced (only effective when args.unhide = true)
  357. // args.unhide: Boolean - If true, the animation is reversed
  358. var node = args.node = dojo.byId(args.node);
  359. var s = node.style;
  360. args.words = false;
  361. args.duration = args.duration || 2000;
  362. args.random = args.random || 0;
  363. var random = Math.abs(args.random);
  364. var delay = 10;
  365. // Returns the animation object for each piece
  366. args.pieceAnimation = function(piece, pieceCoords, coords, number, numPieces){
  367. var interval = args.interval || (args.duration / (1.5 * numPieces)),
  368. text = ("textContent" in piece) ? piece.textContent : piece.innerText,
  369. whitespace = text.match(/\s/g);
  370. if(typeof(args.wordDelay) == "undefined"){
  371. args.wordDelay = interval * 2;
  372. }
  373. if(!args.unhide){
  374. delay = (numPieces - number - 1) * interval;
  375. }
  376. var beforeBegin, onEnd;
  377. if(args.fixed){
  378. if(args.unhide){
  379. var beforeBegin = function(){ dojo.style(piece,"opacity",0); };
  380. }
  381. }else{
  382. if(args.unhide){
  383. var beforeBegin = function(){piece.style.display = "none";};
  384. var onEnd = function(){piece.style.display = "inline";};
  385. }else{
  386. var onEnd = function(){piece.style.display = "none";};
  387. }
  388. }
  389. // Create the animation object for the piece
  390. var pieceAnimation = dojo.animateProperty({
  391. node: piece,
  392. duration: 1,
  393. delay: delay,
  394. easing: (args.easing || dojo.fx.easing.sinInOut),
  395. properties: {
  396. opacity: (args.unhide ? {start: 0, end: 1} : {end:0})
  397. },
  398. beforeBegin: beforeBegin,
  399. onEnd: onEnd
  400. });
  401. if(args.unhide){
  402. var randomDelay = Math.random() * text.length * interval;
  403. var wordDelay = randomDelay * random / 2 + Math.max(1 - random / 2, 0) * args.wordDelay;
  404. delay += randomDelay * random + Math.max(1 - random, 0) * interval * text.length +
  405. (wordDelay * (whitespace && text.lastIndexOf(whitespace[whitespace.length-1]) == text.length - 1));
  406. }
  407. return pieceAnimation;
  408. };
  409. var anim = dojox.fx.text._split(args);
  410. return anim; // dojo.Animation
  411. };
  412. dojox.fx.text.type = function(/*Object*/ args){
  413. args.unhide = true;
  414. return dojox.fx.text.backspace(args);
  415. };
  416. }