text.js 16 KB

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