Skip to content

Commit 4ae422f

Browse files
author
crisbeto
committed
replace the "iterations" option with a duration in ms for more precise animations. also cancel the previous animation when a new one has started
1 parent d4aa7de commit 4ae422f

File tree

3 files changed

+35
-26
lines changed

3 files changed

+35
-26
lines changed

src/roundProgress.js

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/roundProgressConfig.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ angular.module('angular-svg-round-progress').constant('roundProgressConfig', {
1010
color: "#45ccce",
1111
bgcolor: "#eaeaea",
1212
stroke: 15,
13-
iterations: 50,
13+
duration: 800,
1414
animation: "easeOutCubic"
1515
});

src/roundProgressService.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ angular.module('angular-svg-round-progress').service('roundProgressService', [fu
4848

4949
service.animations = {
5050

51-
// t: Current iteration
52-
// b: Start value
53-
// c: Change in value
54-
// d: Total iterations
51+
// t: is the current time (or position) of the tween. This can be seconds or frames, steps, seconds, ms, whatever – as long as the unit is the same as is used for the total time.
52+
// b: is the beginning value of the property.
53+
// c: is the change between the beginning and destination value of the property.
54+
// d: is the total time of the tween.
5555
// jshint eqeqeq: false, -W041: true
5656

5757
linearEase: function(t, b, c, d) {

0 commit comments

Comments
 (0)