@@ -1205,27 +1205,18 @@ d3.transition = function() {
12051205} ;
12061206
12071207// TODO namespace transitions; cancel collisions
1208- // TODO easing
12091208function d3_transition ( groups ) {
12101209 var transition = { } ,
12111210 tweens = { } ,
1212- timeout = setTimeout ( start , 1 ) ,
1213- interval ,
1214- then = Date . now ( ) ,
12151211 event = d3 . dispatch ( "start" , "end" ) ,
12161212 stage = [ ] ,
12171213 delay = [ ] ,
12181214 duration = [ ] ,
12191215 durationMax ,
12201216 ease = d3 . ease ( "cubic-in-out" ) ;
12211217
1222- function start ( ) {
1223- interval = setInterval ( step , 24 ) ;
1224- }
1225-
1226- function step ( ) {
1227- var elapsed = Date . now ( ) - then ,
1228- clear = true ,
1218+ function step ( elapsed ) {
1219+ var clear = true ,
12291220 k = - 1 ;
12301221 groups . each ( function ( d , i ) {
12311222 if ( stage [ ++ k ] == 2 ) return ; // ended
@@ -1247,7 +1238,7 @@ function d3_transition(groups) {
12471238 event . end . dispatch . apply ( this , arguments ) ;
12481239 }
12491240 } ) ;
1250- if ( clear ) clearInterval ( interval ) ;
1241+ return clear ;
12511242 }
12521243
12531244 transition . delay = function ( value ) {
@@ -1264,8 +1255,7 @@ function d3_transition(groups) {
12641255 delay [ ++ k ] = delayMin ;
12651256 } ) ;
12661257 }
1267- clearTimeout ( timeout ) ;
1268- timeout = setTimeout ( start , delayMin ) ;
1258+ d3_timer ( step , delayMin ) ;
12691259 return transition ;
12701260 } ;
12711261
@@ -1366,6 +1356,66 @@ function d3_transition(groups) {
13661356
13671357 return transition . delay ( 0 ) . duration ( 250 ) ;
13681358}
1359+ var d3_timer_queue = null ,
1360+ d3_timer_timeout = 0 ,
1361+ d3_timer_interval ;
1362+
1363+ function d3_timer ( callback , delay ) {
1364+ var now = Date . now ( ) ,
1365+ found = false ,
1366+ start = now + delay ,
1367+ t0 ,
1368+ t1 = d3_timer_queue ;
1369+
1370+ // Scan the queue for earliest callback.
1371+ while ( t1 ) {
1372+ if ( t1 . callback == callback ) {
1373+ t1 . then = now ;
1374+ t1 . delay = delay ;
1375+ found = true ;
1376+ } else {
1377+ var x = t1 . then + t1 . delay ;
1378+ if ( x < start ) start = x ;
1379+ }
1380+ t0 = t1 ;
1381+ t1 = t1 . next ;
1382+ }
1383+
1384+ // Otherwise, add the callback to the queue.
1385+ if ( ! found ) d3_timer_queue = {
1386+ callback : callback ,
1387+ then : now ,
1388+ delay : delay ,
1389+ next : d3_timer_queue
1390+ } ;
1391+
1392+ if ( ! d3_timer_interval ) {
1393+ clearTimeout ( d3_timer_timeout ) ;
1394+ d3_timer_timeout = setTimeout ( d3_timer_start , Math . min ( 24 , start - now ) ) ;
1395+ }
1396+ }
1397+
1398+ function d3_timer_start ( ) {
1399+ d3_timer_interval = setInterval ( d3_timer_step , 24 ) ;
1400+ d3_timer_timeout = 0 ;
1401+ }
1402+
1403+ function d3_timer_step ( ) {
1404+ var elapsed ,
1405+ now = Date . now ( ) ,
1406+ t0 = null ,
1407+ t1 = d3_timer_queue ;
1408+ while ( t1 ) {
1409+ elapsed = now - t1 . then ;
1410+ if ( ( elapsed > t1 . delay ) && t1 . callback ( elapsed ) ) {
1411+ if ( t0 ) t0 . next = t1 . next ;
1412+ else d3_timer_queue = t1 . next ;
1413+ }
1414+ t0 = t1 ;
1415+ t1 = t1 . next ;
1416+ }
1417+ if ( ! t0 ) d3_timer_interval = clearInterval ( d3_timer_interval ) ;
1418+ }
13691419function d3_tween ( b ) {
13701420 return typeof b == "function"
13711421 ? function ( d , i , a ) { return d3 . interpolate ( a , b . call ( this , d , i ) ) ; }
0 commit comments