libraries/EaselJS_docs/files/createjs_utils_Ticker.js.html (view raw)
1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="utf-8">
5 <link rel="stylesheet" href="../assets/css/yui-cssgrids-min.css">
6 <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
7 <link rel="stylesheet" href="../assets/css/main.css" id="site_styles">
8 <link rel="shortcut icon" type="image/x-icon" href="../assets/favicon.ico">
9 <script src="../assets/js/yui-source.js"></script>
10 <title>EaselJS v1.0.0 API Documentation : createjs/utils/Ticker.js</title>
11</head>
12<body class="yui3-skin-sam">
13
14<div id="doc">
15 <div id="hd" class="yui3-g header">
16 <div class="yui3-u-3-4">
17
18 <h1><a href="http://www.createjs.com/easeljs"><img src="../assets/docs-icon-EaselJS.png" title="EaselJS"></a></h1>
19
20 </div>
21 <div class="version">
22 <em>API Documentation for: 1.0.0</em>
23 </div>
24 </div>
25 <div id="bd" class="yui3-g">
26
27 <div class="yui3-u-1-4">
28 <div id="docs-sidebar" class="sidebar apidocs">
29 <div id="api-list">
30 <h2 class="off-left">APIs</h2>
31 <div id="api-tabview" class="tabview">
32 <ul class="tabs">
33 <li><a href="#api-classes">Classes</a></li>
34 </ul>
35
36 <div id="api-tabview-filter">
37 <input type="search" id="api-filter" placeholder="Type to filter APIs">
38 </div>
39
40 <div id="api-tabview-panel">
41 <ul id="api-classes" class="apis classes">
42
43 <li><a href="../classes/AlphaMapFilter.html">AlphaMapFilter</a></li>
44
45 <li><a href="../classes/AlphaMaskFilter.html">AlphaMaskFilter</a></li>
46
47 <li><a href="../classes/Bitmap.html">Bitmap</a></li>
48
49 <li><a href="../classes/BitmapCache.html">BitmapCache</a></li>
50
51 <li><a href="../classes/BitmapText.html">BitmapText</a></li>
52
53 <li><a href="../classes/BlurFilter.html">BlurFilter</a></li>
54
55 <li><a href="../classes/ButtonHelper.html">ButtonHelper</a></li>
56
57 <li><a href="../classes/ColorFilter.html">ColorFilter</a></li>
58
59 <li><a href="../classes/ColorMatrix.html">ColorMatrix</a></li>
60
61 <li><a href="../classes/ColorMatrixFilter.html">ColorMatrixFilter</a></li>
62
63 <li><a href="../classes/Container.html">Container</a></li>
64
65 <li><a href="../classes/DisplayObject.html">DisplayObject</a></li>
66
67 <li><a href="../classes/DisplayProps.html">DisplayProps</a></li>
68
69 <li><a href="../classes/DOMElement.html">DOMElement</a></li>
70
71 <li><a href="../classes/EaselJS.html">EaselJS</a></li>
72
73 <li><a href="../classes/Event.html">Event</a></li>
74
75 <li><a href="../classes/EventDispatcher.html">EventDispatcher</a></li>
76
77 <li><a href="../classes/Filter.html">Filter</a></li>
78
79 <li><a href="../classes/Graphics.html">Graphics</a></li>
80
81 <li><a href="../classes/Graphics.Arc.html">Graphics.Arc</a></li>
82
83 <li><a href="../classes/Graphics.ArcTo.html">Graphics.ArcTo</a></li>
84
85 <li><a href="../classes/Graphics.BeginPath.html">Graphics.BeginPath</a></li>
86
87 <li><a href="../classes/Graphics.BezierCurveTo.html">Graphics.BezierCurveTo</a></li>
88
89 <li><a href="../classes/Graphics.Circle.html">Graphics.Circle</a></li>
90
91 <li><a href="../classes/Graphics.ClosePath.html">Graphics.ClosePath</a></li>
92
93 <li><a href="../classes/Graphics.Ellipse.html">Graphics.Ellipse</a></li>
94
95 <li><a href="../classes/Graphics.Fill.html">Graphics.Fill</a></li>
96
97 <li><a href="../classes/Graphics.LineTo.html">Graphics.LineTo</a></li>
98
99 <li><a href="../classes/Graphics.MoveTo.html">Graphics.MoveTo</a></li>
100
101 <li><a href="../classes/Graphics.PolyStar.html">Graphics.PolyStar</a></li>
102
103 <li><a href="../classes/Graphics.QuadraticCurveTo.html">Graphics.QuadraticCurveTo</a></li>
104
105 <li><a href="../classes/Graphics.Rect.html">Graphics.Rect</a></li>
106
107 <li><a href="../classes/Graphics.RoundRect.html">Graphics.RoundRect</a></li>
108
109 <li><a href="../classes/Graphics.Stroke.html">Graphics.Stroke</a></li>
110
111 <li><a href="../classes/Graphics.StrokeDash.html">Graphics.StrokeDash</a></li>
112
113 <li><a href="../classes/Graphics.StrokeStyle.html">Graphics.StrokeStyle</a></li>
114
115 <li><a href="../classes/Matrix2D.html">Matrix2D</a></li>
116
117 <li><a href="../classes/MouseEvent.html">MouseEvent</a></li>
118
119 <li><a href="../classes/MovieClip.html">MovieClip</a></li>
120
121 <li><a href="../classes/MovieClipPlugin.html">MovieClipPlugin</a></li>
122
123 <li><a href="../classes/Point.html">Point</a></li>
124
125 <li><a href="../classes/Rectangle.html">Rectangle</a></li>
126
127 <li><a href="../classes/Shadow.html">Shadow</a></li>
128
129 <li><a href="../classes/Shape.html">Shape</a></li>
130
131 <li><a href="../classes/Sprite.html">Sprite</a></li>
132
133 <li><a href="../classes/SpriteSheet.html">SpriteSheet</a></li>
134
135 <li><a href="../classes/SpriteSheetBuilder.html">SpriteSheetBuilder</a></li>
136
137 <li><a href="../classes/SpriteSheetUtils.html">SpriteSheetUtils</a></li>
138
139 <li><a href="../classes/Stage.html">Stage</a></li>
140
141 <li><a href="../classes/StageGL.html">StageGL</a></li>
142
143 <li><a href="../classes/Text.html">Text</a></li>
144
145 <li><a href="../classes/Ticker.html">Ticker</a></li>
146
147 <li><a href="../classes/Touch.html">Touch</a></li>
148
149 <li><a href="../classes/UID.html">UID</a></li>
150
151 <li><a href="../classes/Utility Methods.html">Utility Methods</a></li>
152
153 <li><a href="../classes/VideoBuffer.html">VideoBuffer</a></li>
154
155 <li><a href="../classes/WebGLInspector.html">WebGLInspector</a></li>
156
157 </ul>
158
159 <ul id="api-modules" class="apis modules">
160
161 <li><a href="../modules/CreateJS.html">CreateJS</a></li>
162
163 <li><a href="../modules/EaselJS.html">EaselJS</a></li>
164
165 </ul>
166 </div>
167 </div>
168</div>
169
170 </div>
171 </div>
172 <div class="yui3-u-3-4">
173 <div id="api-options">
174 Show:
175 <label for="api-show-inherited">
176 <input type="checkbox" id="api-show-inherited" checked>
177 Inherited
178 </label>
179
180 <label for="api-show-protected">
181 <input type="checkbox" id="api-show-protected">
182 Protected
183 </label>
184
185 <label for="api-show-private">
186 <input type="checkbox" id="api-show-private">
187 Private
188 </label>
189 <label for="api-show-deprecated">
190 <input type="checkbox" id="api-show-deprecated">
191 Deprecated
192 </label>
193
194 </div>
195 <div class="apidocs">
196 <div id="docs-main">
197 <div class="content source-content">
198 <script>
199 var name = createjs/utils/Ticker.js
200</script>
201<h1 class="file-heading">File:Ticker.js</h1>
202
203<div class="file">
204 <pre class="code prettyprint linenums">
205/*
206* Ticker
207* Visit http://createjs.com/ for documentation, updates and examples.
208*
209* Copyright (c) 2010 gskinner.com, inc.
210*
211* Permission is hereby granted, free of charge, to any person
212* obtaining a copy of this software and associated documentation
213* files (the "Software"), to deal in the Software without
214* restriction, including without limitation the rights to use,
215* copy, modify, merge, publish, distribute, sublicense, and/or sell
216* copies of the Software, and to permit persons to whom the
217* Software is furnished to do so, subject to the following
218* conditions:
219*
220* The above copyright notice and this permission notice shall be
221* included in all copies or substantial portions of the Software.
222*
223* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
224* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
225* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
226* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
227* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
228* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
229* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
230* OTHER DEALINGS IN THE SOFTWARE.
231*/
232
233/**
234 * @module CreateJS
235 */
236
237// namespace:
238this.createjs = this.createjs||{};
239
240(function() {
241 "use strict";
242
243
244// constructor:
245 /**
246 * The Ticker provides a centralized tick or heartbeat broadcast at a set interval. Listeners can subscribe to the tick
247 * event to be notified when a set time interval has elapsed.
248 *
249 * Note that the interval that the tick event is called is a target interval, and may be broadcast at a slower interval
250 * when under high CPU load. The Ticker class uses a static interface (ex. `Ticker.framerate = 30;`) and
251 * can not be instantiated.
252 *
253 * <h4>Example</h4>
254 *
255 * createjs.Ticker.addEventListener("tick", handleTick);
256 * function handleTick(event) {
257 * // Actions carried out each tick (aka frame)
258 * if (!event.paused) {
259 * // Actions carried out when the Ticker is not paused.
260 * }
261 * }
262 *
263 * @class Ticker
264 * @uses EventDispatcher
265 * @static
266 **/
267 function Ticker() {
268 throw "Ticker cannot be instantiated.";
269 }
270
271
272// constants:
273 /**
274 * In this mode, Ticker uses the requestAnimationFrame API, but attempts to synch the ticks to target framerate. It
275 * uses a simple heuristic that compares the time of the RAF return to the target time for the current frame and
276 * dispatches the tick when the time is within a certain threshold.
277 *
278 * This mode has a higher variance for time between frames than {{#crossLink "Ticker/TIMEOUT:property"}}{{/crossLink}},
279 * but does not require that content be time based as with {{#crossLink "Ticker/RAF:property"}}{{/crossLink}} while
280 * gaining the benefits of that API (screen synch, background throttling).
281 *
282 * Variance is usually lowest for framerates that are a divisor of the RAF frequency. This is usually 60, so
283 * framerates of 10, 12, 15, 20, and 30 work well.
284 *
285 * Falls back to {{#crossLink "Ticker/TIMEOUT:property"}}{{/crossLink}} if the requestAnimationFrame API is not
286 * supported.
287 * @property RAF_SYNCHED
288 * @static
289 * @type {String}
290 * @default "synched"
291 * @readonly
292 **/
293 Ticker.RAF_SYNCHED = "synched";
294
295 /**
296 * In this mode, Ticker passes through the requestAnimationFrame heartbeat, ignoring the target framerate completely.
297 * Because requestAnimationFrame frequency is not deterministic, any content using this mode should be time based.
298 * You can leverage {{#crossLink "Ticker/getTime"}}{{/crossLink}} and the {{#crossLink "Ticker/tick:event"}}{{/crossLink}}
299 * event object's "delta" properties to make this easier.
300 *
301 * Falls back on {{#crossLink "Ticker/TIMEOUT:property"}}{{/crossLink}} if the requestAnimationFrame API is not
302 * supported.
303 * @property RAF
304 * @static
305 * @type {String}
306 * @default "raf"
307 * @readonly
308 **/
309 Ticker.RAF = "raf";
310
311 /**
312 * In this mode, Ticker uses the setTimeout API. This provides predictable, adaptive frame timing, but does not
313 * provide the benefits of requestAnimationFrame (screen synch, background throttling).
314 * @property TIMEOUT
315 * @static
316 * @type {String}
317 * @default "timeout"
318 * @readonly
319 **/
320 Ticker.TIMEOUT = "timeout";
321
322
323// static events:
324 /**
325 * Dispatched each tick. The event will be dispatched to each listener even when the Ticker has been paused using
326 * {{#crossLink "Ticker/paused:property"}}{{/crossLink}}.
327 *
328 * <h4>Example</h4>
329 *
330 * createjs.Ticker.addEventListener("tick", handleTick);
331 * function handleTick(event) {
332 * console.log("Paused:", event.paused, event.delta);
333 * }
334 *
335 * @event tick
336 * @param {Object} target The object that dispatched the event.
337 * @param {String} type The event type.
338 * @param {Boolean} paused Indicates whether the ticker is currently paused.
339 * @param {Number} delta The time elapsed in ms since the last tick.
340 * @param {Number} time The total time in ms since Ticker was initialized.
341 * @param {Number} runTime The total time in ms that Ticker was not paused since it was initialized. For example,
342 * you could determine the amount of time that the Ticker has been paused since initialization with `time-runTime`.
343 * @since 0.6.0
344 */
345
346
347// public static properties:
348 /**
349 * Specifies the timing api (setTimeout or requestAnimationFrame) and mode to use. See
350 * {{#crossLink "Ticker/TIMEOUT:property"}}{{/crossLink}}, {{#crossLink "Ticker/RAF:property"}}{{/crossLink}}, and
351 * {{#crossLink "Ticker/RAF_SYNCHED:property"}}{{/crossLink}} for mode details.
352 * @property timingMode
353 * @static
354 * @type {String}
355 * @default Ticker.TIMEOUT
356 **/
357 Ticker.timingMode = null;
358
359 /**
360 * Specifies a maximum value for the delta property in the tick event object. This is useful when building time
361 * based animations and systems to prevent issues caused by large time gaps caused by background tabs, system sleep,
362 * alert dialogs, or other blocking routines. Double the expected frame duration is often an effective value
363 * (ex. maxDelta=50 when running at 40fps).
364 *
365 * This does not impact any other values (ex. time, runTime, etc), so you may experience issues if you enable maxDelta
366 * when using both delta and other values.
367 *
368 * If 0, there is no maximum.
369 * @property maxDelta
370 * @static
371 * @type {number}
372 * @default 0
373 */
374 Ticker.maxDelta = 0;
375
376 /**
377 * When the ticker is paused, all listeners will still receive a tick event, but the <code>paused</code> property
378 * of the event will be `true`. Also, while paused the `runTime` will not increase. See {{#crossLink "Ticker/tick:event"}}{{/crossLink}},
379 * {{#crossLink "Ticker/getTime"}}{{/crossLink}}, and {{#crossLink "Ticker/getEventTime"}}{{/crossLink}} for more
380 * info.
381 *
382 * <h4>Example</h4>
383 *
384 * createjs.Ticker.addEventListener("tick", handleTick);
385 * createjs.Ticker.paused = true;
386 * function handleTick(event) {
387 * console.log(event.paused,
388 * createjs.Ticker.getTime(false),
389 * createjs.Ticker.getTime(true));
390 * }
391 *
392 * @property paused
393 * @static
394 * @type {Boolean}
395 * @default false
396 **/
397 Ticker.paused = false;
398
399
400// mix-ins:
401 // EventDispatcher methods:
402 Ticker.removeEventListener = null;
403 Ticker.removeAllEventListeners = null;
404 Ticker.dispatchEvent = null;
405 Ticker.hasEventListener = null;
406 Ticker._listeners = null;
407 createjs.EventDispatcher.initialize(Ticker); // inject EventDispatcher methods.
408 Ticker._addEventListener = Ticker.addEventListener;
409 Ticker.addEventListener = function() {
410 !Ticker._inited&&Ticker.init();
411 return Ticker._addEventListener.apply(Ticker, arguments);
412 };
413
414
415// private static properties:
416 /**
417 * @property _inited
418 * @static
419 * @type {Boolean}
420 * @private
421 **/
422 Ticker._inited = false;
423
424 /**
425 * @property _startTime
426 * @static
427 * @type {Number}
428 * @private
429 **/
430 Ticker._startTime = 0;
431
432 /**
433 * @property _pausedTime
434 * @static
435 * @type {Number}
436 * @private
437 **/
438 Ticker._pausedTime=0;
439
440 /**
441 * The number of ticks that have passed
442 * @property _ticks
443 * @static
444 * @type {Number}
445 * @private
446 **/
447 Ticker._ticks = 0;
448
449 /**
450 * The number of ticks that have passed while Ticker has been paused
451 * @property _pausedTicks
452 * @static
453 * @type {Number}
454 * @private
455 **/
456 Ticker._pausedTicks = 0;
457
458 /**
459 * @property _interval
460 * @static
461 * @type {Number}
462 * @private
463 **/
464 Ticker._interval = 50;
465
466 /**
467 * @property _lastTime
468 * @static
469 * @type {Number}
470 * @private
471 **/
472 Ticker._lastTime = 0;
473
474 /**
475 * @property _times
476 * @static
477 * @type {Array}
478 * @private
479 **/
480 Ticker._times = null;
481
482 /**
483 * @property _tickTimes
484 * @static
485 * @type {Array}
486 * @private
487 **/
488 Ticker._tickTimes = null;
489
490 /**
491 * Stores the timeout or requestAnimationFrame id.
492 * @property _timerId
493 * @static
494 * @type {Number}
495 * @private
496 **/
497 Ticker._timerId = null;
498
499 /**
500 * True if currently using requestAnimationFrame, false if using setTimeout. This may be different than timingMode
501 * if that property changed and a tick hasn't fired.
502 * @property _raf
503 * @static
504 * @type {Boolean}
505 * @private
506 **/
507 Ticker._raf = true;
508
509
510// static getter / setters:
511 /**
512 * Use the {{#crossLink "Ticker/interval:property"}}{{/crossLink}} property instead.
513 * @method _setInterval
514 * @private
515 * @static
516 * @param {Number} interval
517 **/
518 Ticker._setInterval = function(interval) {
519 Ticker._interval = interval;
520 if (!Ticker._inited) { return; }
521 Ticker._setupTick();
522 };
523 // Ticker.setInterval is @deprecated. Remove for 1.1+
524 Ticker.setInterval = createjs.deprecate(Ticker._setInterval, "Ticker.setInterval");
525
526 /**
527 * Use the {{#crossLink "Ticker/interval:property"}}{{/crossLink}} property instead.
528 * @method _getInterval
529 * @private
530 * @static
531 * @return {Number}
532 **/
533 Ticker._getInterval = function() {
534 return Ticker._interval;
535 };
536 // Ticker.getInterval is @deprecated. Remove for 1.1+
537 Ticker.getInterval = createjs.deprecate(Ticker._getInterval, "Ticker.getInterval");
538
539 /**
540 * Use the {{#crossLink "Ticker/framerate:property"}}{{/crossLink}} property instead.
541 * @method _setFPS
542 * @private
543 * @static
544 * @param {Number} value
545 **/
546 Ticker._setFPS = function(value) {
547 Ticker._setInterval(1000/value);
548 };
549 // Ticker.setFPS is @deprecated. Remove for 1.1+
550 Ticker.setFPS = createjs.deprecate(Ticker._setFPS, "Ticker.setFPS");
551
552 /**
553 * Use the {{#crossLink "Ticker/framerate:property"}}{{/crossLink}} property instead.
554 * @method _getFPS
555 * @static
556 * @private
557 * @return {Number}
558 **/
559 Ticker._getFPS = function() {
560 return 1000/Ticker._interval;
561 };
562 // Ticker.getFPS is @deprecated. Remove for 1.1+
563 Ticker.getFPS = createjs.deprecate(Ticker._getFPS, "Ticker.getFPS");
564
565 /**
566 * Indicates the target time (in milliseconds) between ticks. Default is 50 (20 FPS).
567 * Note that actual time between ticks may be more than specified depending on CPU load.
568 * This property is ignored if the ticker is using the `RAF` timing mode.
569 * @property interval
570 * @static
571 * @type {Number}
572 **/
573
574 /**
575 * Indicates the target frame rate in frames per second (FPS). Effectively just a shortcut to `interval`, where
576 * `framerate == 1000/interval`.
577 * @property framerate
578 * @static
579 * @type {Number}
580 **/
581 try {
582 Object.defineProperties(Ticker, {
583 interval: { get: Ticker._getInterval, set: Ticker._setInterval },
584 framerate: { get: Ticker._getFPS, set: Ticker._setFPS }
585 });
586 } catch (e) { console.log(e); }
587
588
589// public static methods:
590 /**
591 * Starts the tick. This is called automatically when the first listener is added.
592 * @method init
593 * @static
594 **/
595 Ticker.init = function() {
596 if (Ticker._inited) { return; }
597 Ticker._inited = true;
598 Ticker._times = [];
599 Ticker._tickTimes = [];
600 Ticker._startTime = Ticker._getTime();
601 Ticker._times.push(Ticker._lastTime = 0);
602 Ticker.interval = Ticker._interval;
603 };
604
605 /**
606 * Stops the Ticker and removes all listeners. Use init() to restart the Ticker.
607 * @method reset
608 * @static
609 **/
610 Ticker.reset = function() {
611 if (Ticker._raf) {
612 var f = window.cancelAnimationFrame || window.webkitCancelAnimationFrame || window.mozCancelAnimationFrame || window.oCancelAnimationFrame || window.msCancelAnimationFrame;
613 f&&f(Ticker._timerId);
614 } else {
615 clearTimeout(Ticker._timerId);
616 }
617 Ticker.removeAllEventListeners("tick");
618 Ticker._timerId = Ticker._times = Ticker._tickTimes = null;
619 Ticker._startTime = Ticker._lastTime = Ticker._ticks = Ticker._pausedTime = 0;
620 Ticker._inited = false;
621 };
622
623 /**
624 * Returns the average time spent within a tick. This can vary significantly from the value provided by getMeasuredFPS
625 * because it only measures the time spent within the tick execution stack.
626 *
627 * Example 1: With a target FPS of 20, getMeasuredFPS() returns 20fps, which indicates an average of 50ms between
628 * the end of one tick and the end of the next. However, getMeasuredTickTime() returns 15ms. This indicates that
629 * there may be up to 35ms of "idle" time between the end of one tick and the start of the next.
630 *
631 * Example 2: With a target FPS of 30, {{#crossLink "Ticker/framerate:property"}}{{/crossLink}} returns 10fps, which
632 * indicates an average of 100ms between the end of one tick and the end of the next. However, {{#crossLink "Ticker/getMeasuredTickTime"}}{{/crossLink}}
633 * returns 20ms. This would indicate that something other than the tick is using ~80ms (another script, DOM
634 * rendering, etc).
635 * @method getMeasuredTickTime
636 * @static
637 * @param {Number} [ticks] The number of previous ticks over which to measure the average time spent in a tick.
638 * Defaults to the number of ticks per second. To get only the last tick's time, pass in 1.
639 * @return {Number} The average time spent in a tick in milliseconds.
640 **/
641 Ticker.getMeasuredTickTime = function(ticks) {
642 var ttl=0, times=Ticker._tickTimes;
643 if (!times || times.length < 1) { return -1; }
644
645 // by default, calculate average for the past ~1 second:
646 ticks = Math.min(times.length, ticks||(Ticker._getFPS()|0));
647 for (var i=0; i<ticks; i++) { ttl += times[i]; }
648 return ttl/ticks;
649 };
650
651 /**
652 * Returns the actual frames / ticks per second.
653 * @method getMeasuredFPS
654 * @static
655 * @param {Number} [ticks] The number of previous ticks over which to measure the actual frames / ticks per second.
656 * Defaults to the number of ticks per second.
657 * @return {Number} The actual frames / ticks per second. Depending on performance, this may differ
658 * from the target frames per second.
659 **/
660 Ticker.getMeasuredFPS = function(ticks) {
661 var times = Ticker._times;
662 if (!times || times.length < 2) { return -1; }
663
664 // by default, calculate fps for the past ~1 second:
665 ticks = Math.min(times.length-1, ticks||(Ticker._getFPS()|0));
666 return 1000/((times[0]-times[ticks])/ticks);
667 };
668
669 /**
670 * Returns the number of milliseconds that have elapsed since Ticker was initialized via {{#crossLink "Ticker/init"}}.
671 * Returns -1 if Ticker has not been initialized. For example, you could use
672 * this in a time synchronized animation to determine the exact amount of time that has elapsed.
673 * @method getTime
674 * @static
675 * @param {Boolean} [runTime=false] If true only time elapsed while Ticker was not paused will be returned.
676 * If false, the value returned will be total time elapsed since the first tick event listener was added.
677 * @return {Number} Number of milliseconds that have elapsed since Ticker was initialized or -1.
678 **/
679 Ticker.getTime = function(runTime) {
680 return Ticker._startTime ? Ticker._getTime() - (runTime ? Ticker._pausedTime : 0) : -1;
681 };
682
683 /**
684 * Similar to the {{#crossLink "Ticker/getTime"}}{{/crossLink}} method, but returns the time on the most recent {{#crossLink "Ticker/tick:event"}}{{/crossLink}}
685 * event object.
686 * @method getEventTime
687 * @static
688 * @param runTime {Boolean} [runTime=false] If true, the runTime property will be returned instead of time.
689 * @returns {number} The time or runTime property from the most recent tick event or -1.
690 */
691 Ticker.getEventTime = function(runTime) {
692 return Ticker._startTime ? (Ticker._lastTime || Ticker._startTime) - (runTime ? Ticker._pausedTime : 0) : -1;
693 };
694
695 /**
696 * Returns the number of ticks that have been broadcast by Ticker.
697 * @method getTicks
698 * @static
699 * @param {Boolean} pauseable Indicates whether to include ticks that would have been broadcast
700 * while Ticker was paused. If true only tick events broadcast while Ticker is not paused will be returned.
701 * If false, tick events that would have been broadcast while Ticker was paused will be included in the return
702 * value. The default value is false.
703 * @return {Number} of ticks that have been broadcast.
704 **/
705 Ticker.getTicks = function(pauseable) {
706 return Ticker._ticks - (pauseable ? Ticker._pausedTicks : 0);
707 };
708
709
710// private static methods:
711 /**
712 * @method _handleSynch
713 * @static
714 * @private
715 **/
716 Ticker._handleSynch = function() {
717 Ticker._timerId = null;
718 Ticker._setupTick();
719
720 // run if enough time has elapsed, with a little bit of flexibility to be early:
721 if (Ticker._getTime() - Ticker._lastTime >= (Ticker._interval-1)*0.97) {
722 Ticker._tick();
723 }
724 };
725
726 /**
727 * @method _handleRAF
728 * @static
729 * @private
730 **/
731 Ticker._handleRAF = function() {
732 Ticker._timerId = null;
733 Ticker._setupTick();
734 Ticker._tick();
735 };
736
737 /**
738 * @method _handleTimeout
739 * @static
740 * @private
741 **/
742 Ticker._handleTimeout = function() {
743 Ticker._timerId = null;
744 Ticker._setupTick();
745 Ticker._tick();
746 };
747
748 /**
749 * @method _setupTick
750 * @static
751 * @private
752 **/
753 Ticker._setupTick = function() {
754 if (Ticker._timerId != null) { return; } // avoid duplicates
755
756 var mode = Ticker.timingMode;
757 if (mode == Ticker.RAF_SYNCHED || mode == Ticker.RAF) {
758 var f = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame;
759 if (f) {
760 Ticker._timerId = f(mode == Ticker.RAF ? Ticker._handleRAF : Ticker._handleSynch);
761 Ticker._raf = true;
762 return;
763 }
764 }
765 Ticker._raf = false;
766 Ticker._timerId = setTimeout(Ticker._handleTimeout, Ticker._interval);
767 };
768
769 /**
770 * @method _tick
771 * @static
772 * @private
773 **/
774 Ticker._tick = function() {
775 var paused = Ticker.paused;
776 var time = Ticker._getTime();
777 var elapsedTime = time-Ticker._lastTime;
778 Ticker._lastTime = time;
779 Ticker._ticks++;
780
781 if (paused) {
782 Ticker._pausedTicks++;
783 Ticker._pausedTime += elapsedTime;
784 }
785
786 if (Ticker.hasEventListener("tick")) {
787 var event = new createjs.Event("tick");
788 var maxDelta = Ticker.maxDelta;
789 event.delta = (maxDelta && elapsedTime > maxDelta) ? maxDelta : elapsedTime;
790 event.paused = paused;
791 event.time = time;
792 event.runTime = time-Ticker._pausedTime;
793 Ticker.dispatchEvent(event);
794 }
795
796 Ticker._tickTimes.unshift(Ticker._getTime()-time);
797 while (Ticker._tickTimes.length > 100) { Ticker._tickTimes.pop(); }
798
799 Ticker._times.unshift(time);
800 while (Ticker._times.length > 100) { Ticker._times.pop(); }
801 };
802
803 /**
804 * @method _getTime
805 * @static
806 * @private
807 **/
808 var w=window, now=w.performance.now || w.performance.mozNow || w.performance.msNow || w.performance.oNow || w.performance.webkitNow;
809 Ticker._getTime = function() {
810 return ((now&&now.call(w.performance))||(new Date().getTime())) - Ticker._startTime;
811 };
812
813
814 createjs.Ticker = Ticker;
815}());
816
817 </pre>
818</div>
819
820 </div>
821 </div>
822 </div>
823 </div>
824 </div>
825</div>
826<script src="../assets/vendor/prettify/prettify-min.js"></script>
827<script>prettyPrint();</script>
828<script src="../assets/js/yui-prettify.js"></script>
829<script src="../assets/../api.js"></script>
830<script src="../assets/js/api-filter.js"></script>
831<script src="../assets/js/api-list.js"></script>
832<script src="../assets/js/api-search.js"></script>
833<script src="../assets/js/apidocs.js"></script>
834</body>
835</html>