My favorites | Sign in
Logo
Project hosting will be READ-ONLY Wednesday at 8am PST due to brief network maintenance.
             
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Copyright 2006 Google Inc. All Rights Reserved.

/**
* @fileoverview Set of objects that allow animation and visual effects to be
* easily achieved.
*
* (Based loosly on my animation code for 13thparallel.org, with extra
* inspiration from the DojoToolkit's modifications to my code)
*/

goog.provide('goog.fx');
goog.provide('goog.fx.Animation');
goog.provide('goog.fx.Animation.EventType');
goog.provide('goog.fx.Animation.State');
goog.provide('goog.fx.AnimationEvent');
goog.provide('goog.fx.easing');

goog.require('goog.Timer');
goog.require('goog.array');
goog.require('goog.events.Event');
goog.require('goog.events.EventTarget');
goog.require('goog.object');


//==============================================================================
// Constants
//==============================================================================


/**
* Default timeout for animations (in milliseconds).
* @type {number}
*/
goog.fx.TIMEOUT = 20;



/**
* Ease in - Start slow and speed up.
* @param {number} t Input between 0 and 1.
* @return {number} Output between 0 and 1.
*/
goog.fx.easing.easeIn = function(t) {
return t * t * t;
};

/**
* Ease out - Start fastest and slows to a stop.
* @param {number} t Input between 0 and 1.
* @return {number} Output between 0 and 1.
*/
goog.fx.easing.easeOut = function(t) {
return 1 - Math.pow(1 - t, 3);
};


/**
* Ease in and out - Start slow, speed up, then slow down.
* @param {number} t Input between 0 and 1.
* @return {number} Output between 0 and 1.
*/
goog.fx.easing.inAndOut = function(t) {
return 3 * t * t - 2 * t * t * t;
};



/**
* Constructor for an animation object.
* @param {Array.<number>} start Array for start coordinates.
* @param {Array.<number>} end Array for end coordinates.
* @param {number} duration Length of animation in milliseconds.
* @param {Function} opt_acc Acceleration function, returns 0-1 for inputs 0-1.
* @constructor
* @extends {goog.events.EventTarget}
*/
goog.fx.Animation = function(start, end, duration, opt_acc) {
goog.events.EventTarget.call(this);

if (!goog.isArray(start) || !goog.isArray(end)) {
throw Error('Start and end parameters must be arrays');
}

if (start.length != end.length) {
throw Error('Start and end points must be the same length');
}

/**
* Start point.
* @type {Array.<number>}
* @protected
*/
this.startPoint = start;

/**
* End point.
* @type {Array.<number>}
* @protected
*/
this.endPoint = end;

/**
* Duration of animation in milliseconds.
* @type {number}
* @protected
*/
this.duration = duration;

/**
* Acceleration function, which must return a number between 0 and 1 for
* inputs between 0 and 1.
* @type {Function|undefined}
* @private
*/
this.accel_ = opt_acc;

/**
* Current coordinate for animation.
* @type {Array.<number>}
* @protected
*/
this.coords = [];
};
goog.inherits(goog.fx.Animation, goog.events.EventTarget);


/**
* Events fired by the animation.
* @enum {string}
*/
goog.fx.Animation.EventType = {
/**
* Dispatched when played for the first time OR when it is resumed.
*/
PLAY: 'play',

/**
* Dispatched only when the animation starts from the beginning.
*/
BEGIN: 'begin',

/**
* Dispatched only when animation is restarted after a pause.
*/
RESUME: 'resume',

/**
* Dispatched when animation comes to the end of its duration OR stop
* is called.
*/
END: 'end',

/**
* Dispatched only when stop is called.
*/
STOP: 'stop',

/**
* Dispatched only when animation comes to its end naturally.
*/
FINISH: 'finish',

/**
* Dispatched when an animation is paused.
*/
PAUSE: 'pause',

/**
* Dispatched each frame of the animation. This is where the actual animator
* will listen.
*/
ANIMATE: 'animate',

/**
* Dispatched when the animation is destroyed.
*/
DESTROY: 'destroy'
};


/**
* Enum for the possible states of an animation.
* @enum {number}
*/
goog.fx.Animation.State = {
STOPPED: 0,
PAUSED: -1,
PLAYING: 1
};


/**
* A map of animations which should be cycled on the global timer.
* @type {Object}
* @private
*/
goog.fx.Animation.activeAnimations_ = {};


/**
* An interval ID for the global timer.
* @type {number?}
* @private
*/
goog.fx.Animation.globalTimer_ = null;


/**
* Cycle all registered animations.
* @private
*/
goog.fx.Animation.cycleAnimations_ = function() {
goog.Timer.defaultTimerObject.clearTimeout(goog.fx.Animation.globalTimer_);

// Cycle all animations at the "same time".
var now = goog.now();

for (var hc in goog.fx.Animation.activeAnimations_) {
goog.fx.Animation.activeAnimations_[hc].cycle(now);
}

goog.fx.Animation.globalTimer_ =
goog.object.isEmpty(goog.fx.Animation.activeAnimations_) ?
null :
goog.Timer.defaultTimerObject.setTimeout(
goog.fx.Animation.cycleAnimations_, goog.fx.TIMEOUT);
};


/**
* Register an animation to be cycled on the global timer.
* @param {Object} animation The animation to register.
*/
goog.fx.Animation.registerAnimation = function(animation) {
var hc = goog.getHashCode(animation);
if (!(hc in goog.fx.Animation.activeAnimations_)) {
goog.fx.Animation.activeAnimations_[hc] = animation;
}

// If the timer is not already started, start it now.
if (!goog.fx.Animation.globalTimer_) {
goog.fx.Animation.globalTimer_ = goog.Timer.defaultTimerObject.setTimeout(
goog.fx.Animation.cycleAnimations_, goog.fx.TIMEOUT);
}
};


/**
* Remove an animation from the list of animations which are cycled on the
* global timer.
* @param {Object} animation The animation to unregister.
*/
goog.fx.Animation.unregisterAnimation = function(animation) {
var hc = goog.getHashCode(animation);
delete goog.fx.Animation.activeAnimations_[hc];

// If a timer is running and we no longer have any active timers we stop the
// timers.
if (goog.fx.Animation.globalTimer_ &&
goog.object.isEmpty(goog.fx.Animation.activeAnimations_)) {
goog.Timer.defaultTimerObject.clearTimeout(goog.fx.Animation.globalTimer_);
goog.fx.Animation.globalTimer_ = null;
}
};


/**
* Current state of the animation.
* @type {goog.fx.Animation.State}
* @private
*/
goog.fx.Animation.prototype.state_ = goog.fx.Animation.State.STOPPED;


/**
* Current frame rate.
* @type {number}
* @private
*/
goog.fx.Animation.prototype.fps_ = 0;


/**
* Percent of the way through the animation.
* @type {number}
* @protected
*/
goog.fx.Animation.prototype.progress = 0;


/**
* Timestamp for when animation was started.
* @type {number?}
* @protected
*/
goog.fx.Animation.prototype.startTime = null;


/**
* Timestamp for when animation was started.
* @type {number?}
* @protected
*/
goog.fx.Animation.prototype.endTime = null;


/**
* Timestamp for when last frame was run.
* @type {number?}
* @protected
*/
goog.fx.Animation.prototype.lastFrame = null;


/**
* Starts or resumes an animation.
* @param {boolean} opt_restart Whether to restart the
* animation from the beginning if it has been paused.
* @return {boolean} Whether animation was started.
*/
goog.fx.Animation.prototype.play = function(opt_restart) {
if (opt_restart || this.state_ == goog.fx.Animation.State.STOPPED) {
this.progress = 0;
this.coords = this.startPoint;
} else if (this.state_ == goog.fx.Animation.State.PLAYING) {
return false;
}

goog.fx.Animation.unregisterAnimation(this);

this.startTime = /** @type {number} */ (goog.now());

if (this.state_ == goog.fx.Animation.State.PAUSED) {
this.startTime -= this.duration * this.progress;
}

this.endTime = this.startTime + this.duration;
this.lastFrame = this.startTime;

if (!this.progress) {
this.onBegin();
}

this.onPlay();

if (this.state_ == goog.fx.Animation.State.PAUSED) {
this.onResume();
}

this.state_ = goog.fx.Animation.State.PLAYING;

goog.fx.Animation.registerAnimation(this);
this.cycle(this.startTime);

return true;
};


/**
* Stops the animation.
* @param {boolean} gotoEnd If true the animation will move to the end coords.
*/
goog.fx.Animation.prototype.stop = function(gotoEnd) {
goog.fx.Animation.unregisterAnimation(this);
this.state_ = goog.fx.Animation.State.STOPPED;

if (gotoEnd) {
this.progress = 1;
}

this.updateCoords_(this.progress);

this.onStop();
this.onEnd();
};


/**
* Pauses the animation (iff it's playing).
*/
goog.fx.Animation.prototype.pause = function() {
if (this.state_ == goog.fx.Animation.State.PLAYING) {
goog.fx.Animation.unregisterAnimation(this);
this.state_ = goog.fx.Animation.State.PAUSED;
this.onPause();
}
};


/**
* Disposes of the animation. Stops an animation, fires a 'destroy' event and
* then removes all the event handlers to clean up memory.
*/
goog.fx.Animation.prototype.disposeInternal = function() {
if (this.state_ != goog.fx.Animation.State.STOPPED) {
this.stop(false);
}
this.onDestroy();
goog.fx.Animation.superClass_.disposeInternal.call(this);
};


/**
* Stops an animation, fires a 'destroy' event and then removes all the event
* handlers to clean up memory.
* @deprecated Use dispose() instead.
*/
goog.fx.Animation.prototype.destroy = function() {
this.dispose();
};


/**
* Handles the actual iteration of the animation in a timeout
* @param {number} now The current time.
*/
goog.fx.Animation.prototype.cycle = function(now) {
this.progress = (now - this.startTime) / (this.endTime - this.startTime);

if (this.progress >= 1) {
this.progress = 1;
}

this.fps_ = 1000 / (now - this.lastFrame);
this.lastFrame = now;

if (goog.isFunction(this.accel_)) {
this.updateCoords_(this.accel_(this.progress));
} else {
this.updateCoords_(this.progress);
}

// Animation has finished.
if (this.progress == 1) {
this.state_ = goog.fx.Animation.State.STOPPED;
goog.fx.Animation.unregisterAnimation(this);

this.onFinish();
this.onEnd();

// Animation is still under way.
} else if (this.state_ == goog.fx.Animation.State.PLAYING) {
this.onAnimate();
}
};


/**
* Calculates current coordinates, based on the current state.
* @param {number} t Percentage of the way through the animation as a decimal.
* @private
*/
goog.fx.Animation.prototype.updateCoords_ = function(t) {
this.coords = new Array(this.startPoint.length);
for (var i = 0; i < this.startPoint.length; i++) {
this.coords[i] = (this.endPoint[i] - this.startPoint[i]) * t +
this.startPoint[i];
}
};


/**
* Dispatches the ANIMATE event. Sub classes should override this instead
* of listening to the event.
* @protected
*/
goog.fx.Animation.prototype.onAnimate = function() {
this.dispatchAnimationEvent_(goog.fx.Animation.EventType.ANIMATE);
};



/**
* Dispatches the BEGIN event. Sub classes should override this instead
* of listening to the event.
* @protected
*/
goog.fx.Animation.prototype.onBegin = function() {
this.dispatchAnimationEvent_(goog.fx.Animation.EventType.BEGIN);
};


/**
* Dispatches the DESTROY event. Sub classes should override this instead
* of listening to the event.
* @protected
*/
goog.fx.Animation.prototype.onDestroy = function() {
this.dispatchAnimationEvent_(goog.fx.Animation.EventType.DESTROY);
};


/**
* Dispatches the END event. Sub classes should override this instead
* of listening to the event.
* @protected
*/
goog.fx.Animation.prototype.onEnd = function() {
this.dispatchAnimationEvent_(goog.fx.Animation.EventType.END);
};


/**
* Dispatches the FINISH event. Sub classes should override this instead
* of listening to the event.
* @protected
*/
goog.fx.Animation.prototype.onFinish = function() {
this.dispatchAnimationEvent_(goog.fx.Animation.EventType.FINISH);
};


/**
* Dispatches the PAUSE event. Sub classes should override this instead
* of listening to the event.
* @protected
*/
goog.fx.Animation.prototype.onPause = function() {
this.dispatchAnimationEvent_(goog.fx.Animation.EventType.PAUSE);
};


/**
* Dispatches the PLAY event. Sub classes should override this instead
* of listening to the event.
* @protected
*/
goog.fx.Animation.prototype.onPlay = function() {
this.dispatchAnimationEvent_(goog.fx.Animation.EventType.PLAY);
};

/**
* Dispatches the RESUME event. Sub classes should override this instead
* of listening to the event.
* @protected
*/
goog.fx.Animation.prototype.onResume = function() {
this.dispatchAnimationEvent_(goog.fx.Animation.EventType.RESUME);
};


/**
* Dispatches the STOP event. Sub classes should override this instead
* of listening to the event.
* @protected
*/
goog.fx.Animation.prototype.onStop = function() {
this.dispatchAnimationEvent_(goog.fx.Animation.EventType.STOP);
};


/**
* Returns an event object for the current animation.
* @param {string} type Event type that will be dispatched.
* @private
*/
goog.fx.Animation.prototype.dispatchAnimationEvent_ = function(type) {
this.dispatchEvent(new goog.fx.AnimationEvent(type, this));
};



/**
* Class for an animation event object.
* @param {string} type Event type.
* @param {goog.fx.Animation} anim An animation object.
* @constructor
* @extends {goog.events.Event}
*/
goog.fx.AnimationEvent = function(type, anim) {
goog.events.Event.call(this, type);

/**
* The current coordinates.
* @type {Array.<number>}
*/
this.coords = anim.coords;

/**
* The x coordinate.
* @type {number}
*/
this.x = anim.coords[0];

/**
* The y coordinate.
* @type {number}
*/
this.y = anim.coords[1];

/**
* The z coordinate.
* @type {number}
*/
this.z = anim.coords[2];

/**
* The current duration.
* @type {number}
*/
this.duration = anim.duration;

/**
* The current progress.
* @type {number}
*/
this.progress = anim.progress;

/**
* Frames per second so far.
*/
this.fps = anim.fps_;

/**
* The state of the animation.
* @type {number}
*/
this.state = anim.state_;

/**
* The animation object.
* @type {goog.fx.Animation}
*/
// TODO: This can be removed as this is the same as the target
this.anim = anim;
};
goog.inherits(goog.fx.AnimationEvent, goog.events.Event);


/**
* Returns the coordinates as integers (rounded to nearest integer).
* @return {Array.<number>} An array of the coordinates rounded to
* the nearest integer.
*/
goog.fx.AnimationEvent.prototype.coordsAsInts = function() {
return goog.array.map(this.coords, Math.round);
};
Show details Hide details

Change log

r2 by nanaze on Nov 03, 2009   Diff
Initial checkin of Closure Library and
docs.

Good luck little buddy.

Go to: 

Older revisions

All revisions of this file

File info

Size: 15150 bytes, 649 lines

File properties

svn:mime-type
text/javascript