@@ -28,14 +28,15 @@ angular.module('angular-svg-round-progress')
2828 color : "@" ,
2929 bgcolor : "@" ,
3030 stroke : "@" ,
31- iterations : "@" ,
31+ duration : "@" ,
3232 animation : "@"
3333 } ,
3434 link : function ( scope , element ) {
3535 var svg = angular . element ( element [ 0 ] . querySelector ( 'svg' ) ) ;
3636 var ring = svg . find ( 'path' ) ;
3737 var background = svg . find ( 'circle' ) ;
3838 var options = angular . copy ( roundProgressConfig ) ;
39+ var lastAnimationId ;
3940
4041 var renderCircle = function ( ) {
4142 var isSemicircle = options . semi ;
@@ -86,35 +87,43 @@ angular.module('angular-svg-round-progress')
8687
8788 var renderState = function ( newValue , oldValue ) {
8889 var max = service . toNumber ( options . max || 0 ) ;
89- var current = newValue > max ? max : ( newValue < 0 || ! newValue ? 0 : newValue ) ;
90- var start = ( oldValue === current || oldValue < 0 ) ? 0 : ( oldValue || 0 ) ; // fixes the initial animation
91- var changeInValue = current - start ;
90+ var end = newValue > max ? max : ( newValue < 0 || ! newValue ? 0 : newValue ) ;
91+ var start = ( oldValue === end || oldValue < 0 ) ? 0 : ( oldValue || 0 ) ; // fixes the initial animation
92+ var changeInValue = end - start ;
9293
9394 var easingAnimation = service . animations [ options . animation ] ;
94- var currentIteration = 0 ;
95- var totalIterations = parseInt ( options . iterations ) ;
95+ var startTime = new Date ( ) ;
96+ var duration = parseInt ( options . duration ) ;
9697
9798 var radius = options . radius ;
9899 var circleSize = radius - ( options . stroke / 2 ) ;
99100 var elementSize = radius * 2 ;
100101
101- ( function animation ( ) {
102- service . updateState (
103- easingAnimation ( currentIteration , start , changeInValue , totalIterations ) ,
104- max ,
105- circleSize ,
106- ring ,
107- elementSize ,
108- options . semi ) ;
109-
110- if ( currentIteration < totalIterations ) {
111- $window . requestAnimationFrame ( animation ) ;
112- currentIteration ++ ;
113- }
114- } ) ( ) ;
102+ $window . cancelAnimationFrame ( lastAnimationId ) ;
103+
104+ // below 25ms stuff starts to break
105+ if ( duration > 25 ) {
106+ ( function animation ( ) {
107+ var currentTime = new Date ( ) - startTime ;
108+
109+ service . updateState (
110+ easingAnimation ( currentTime , start , changeInValue , duration ) ,
111+ max ,
112+ circleSize ,
113+ ring ,
114+ elementSize ,
115+ options . semi ) ;
116+
117+ if ( currentTime < duration ) {
118+ lastAnimationId = $window . requestAnimationFrame ( animation ) ;
119+ }
120+ } ) ( ) ;
121+ } else {
122+ service . updateState ( end , max , circleSize , ring , elementSize , options . semi ) ;
123+ }
115124 } ;
116125
117- scope . $watchCollection ( '[current, max, semi, rounded, clockwise, radius, color, bgcolor, stroke, iterations , responsive]' , function ( newValue , oldValue , scope ) {
126+ scope . $watchCollection ( '[current, max, semi, rounded, clockwise, radius, color, bgcolor, stroke, duration , responsive]' , function ( newValue , oldValue , scope ) {
118127
119128 // pretty much the same as angular.extend,
120129 // but this skips undefined values and internal angular keys
0 commit comments