source: Dev/trunk/src/client/dojox/fx/text.js @ 532

Last change on this file since 532 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

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