Skip to content

Commit 369aa54

Browse files
committed
Simplified Ticker implementation, and fixed issues with useRAF.
Signed-off-by: Grant Skinner <info@gskinner.com>
1 parent e5267de commit 369aa54

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed

VERSIONS.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
Version 0.4.1 [Not released]
1+
Version 0.4.1 [Not tagged]
22
****************************************************************************************************
33
- fixed a problem with preloading sprite sheet images when using tile based sheets
44
- worked around a bug in Safari with addFlippedFrames
55
- added setChildIndex(), swapChildrenAt(), and swapChildren() to Container
66
- made frequency param on enableMouseOver optional
7+
- worked around a Chrome 17 bug that would prevent text w/o maxwidth from rendering
8+
- additional fixes for video as a Bitmap source
9+
- rewrite of Ticker to simplify implementation and solve issues with useRAF in Safari
710

811

912
Version 0.4 [Nov 30, 2011]

src/easeljs/utils/Ticker.js

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ var Ticker = function() {
6161
**/
6262
Ticker.useRAF = null;
6363

64+
/**
65+
* Specifies the animation target to use with requestAnimationFrame if useRAF is true.
66+
* @property animationTarget
67+
* @static
68+
* @type Object
69+
**/
70+
Ticker.animationTarget = null;
71+
6472
/**
6573
* Event broadcast once each tick / interval. The interval is specified via the
6674
* .setInterval(ms) or setFPS methods.
@@ -204,7 +212,7 @@ var Ticker = function() {
204212
Ticker._tickTimes = [];
205213
Ticker._pauseable = [];
206214
Ticker._listeners = [];
207-
Ticker._times.push(Ticker._startTime = Ticker._getTime());
215+
Ticker._times.push(Ticker._lastTime = Ticker._startTime = Ticker._getTime());
208216
Ticker.setInterval(Ticker._interval);
209217
}
210218

@@ -241,20 +249,9 @@ var Ticker = function() {
241249
* @param {Number} interval Time in milliseconds between ticks. Default value is 50.
242250
**/
243251
Ticker.setInterval = function(interval) {
244-
Ticker._lastTime = Ticker._getTime();
245252
Ticker._interval = interval;
246-
if (Ticker.timeoutID != null) { clearTimeout(Ticker.timeoutID); }
247-
if (Ticker.useRAF) {
248-
if (Ticker._rafActive) { return; }
249-
Ticker._rafActive = true;
250-
var f = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame ||
251-
window.oRequestAnimationFrame || window.msRequestAnimationFrame;
252-
if (f) {
253-
f(Ticker._handleAF);
254-
return;
255-
}
256-
}
257-
if (Ticker._inited) { Ticker.timeoutID = setTimeout(Ticker._handleTimeout, interval); }
253+
if (!Ticker._inited) { return; }
254+
Ticker._setupTick();
258255
}
259256

260257
/**
@@ -301,8 +298,8 @@ var Ticker = function() {
301298
Ticker.getMeasuredFPS = function(ticks) {
302299
if (Ticker._times.length < 2) { return -1; }
303300

304-
// by default, calculate fps for the past 1/2 second:
305-
if (ticks == null) { ticks = Ticker.getFPS()>>1; }
301+
// by default, calculate fps for the past 1 second:
302+
if (ticks == null) { ticks = Ticker.getFPS()|0; }
306303
ticks = Math.min(Ticker._times.length-1, ticks);
307304
return 1000/((Ticker._times[0]-Ticker._times[ticks])/ticks);
308305
}
@@ -365,13 +362,8 @@ var Ticker = function() {
365362
if (timeStamp - Ticker._lastTime >= Ticker._interval-1) {
366363
Ticker._tick();
367364
}
368-
if (Ticker.useRAF) {
369-
var f = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame ||
370-
window.oRequestAnimationFrame || window.msRequestAnimationFrame;
371-
f(Ticker._handleAF, Ticker.animationTarget);
372-
} else {
373-
Ticker._rafActive = false;
374-
}
365+
Ticker._rafActive = false;
366+
Ticker._setupTick();
375367
}
376368

377369
/**
@@ -380,7 +372,21 @@ var Ticker = function() {
380372
**/
381373
Ticker._handleTimeout = function() {
382374
Ticker._tick();
383-
if (!Ticker.useRAF) { Ticker.timeoutID = setTimeout(Ticker._handleTimeout, Ticker._interval); }
375+
Ticker.timeoutID = null;
376+
Ticker._setupTick();
377+
}
378+
379+
Ticker._setupTick = function() {
380+
if (Ticker._rafActive || Ticker.timeoutID != null) { return; } // avoid duplicates
381+
if (Ticker.useRAF) {
382+
var f = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame;
383+
if (f) {
384+
f(Ticker._handleAF, Ticker.animationTarget);
385+
Ticker._rafActive = true;
386+
return;
387+
}
388+
}
389+
Ticker.timeoutID = setTimeout(Ticker._handleTimeout, Ticker._interval);
384390
}
385391

386392
/**

0 commit comments

Comments
 (0)