]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Chart.AnimationService 1058/head
authorTanner Linsley <tannerlinsley@gmail.com>
Sat, 11 Apr 2015 23:19:29 +0000 (17:19 -0600)
committerTanner Linsley <tannerlinsley@gmail.com>
Sat, 11 Apr 2015 23:19:29 +0000 (17:19 -0600)
Chart.js

index c264262ba7352f75c1c2112866e98b2c1fcf9173..f9b30f0a7c66f5e41586a9fa94a1eff6ea5d5868 100644 (file)
--- a/Chart.js
+++ b/Chart.js
                }
        });
 
+       Chart.animationService = {
+               animations: [],
+               addAnimation: function(chart, animationObject) {
+                       for (var index = 0; index < this.animations.length; ++ index){
+                               if (this.animations[index].chart === chart){
+                                       // replacing an in progress animation
+                                       this.animations[index].lastTimeRun = null;
+                                       this.animations[index].animationObject = animationObject;
+                                       return;
+                               }
+                       }
+                       
+                       this.animations.push({
+                               chart: chart,
+                               animationObject: animationObject,
+                               lastTimeRun: null
+                       });
+
+                       // If there are no animations queued, manually kickstart a digest, for lack of a better word
+                       if(!this.animations.length){
+                               helpers.requestAnimFrame(this.startDigest);
+                       }
+               },
+               startDigest: function() {
+
+                       for (var i = 0; i < this.animations.length; i++) {
+
+                               var currentAnimation = this.animations[i];
+
+                               if (currentAnimation.animationObject.currentStep === null){
+                                       currentAnimation.animationObject.currentStep = 0;
+                               } else {
+                                       currentAnimation.animationObject.currentStep++;
+                               }
+                               
+                               currentAnimation.animationObject.render(currentAnimation.animationObject);
+                               
+                               if (currentAnimation.animationObject.currentStep == currentAnimation.animationObject.numSteps){
+                                       // executed the last frame. Remove the animation.
+                                       this.animations.splice(i, 1);
+                                       // Keep the index in place to offset the splice
+                                       i--;
+                               }
+                       }
+
+                       // Do we have more stuff to animate?
+                       if (this.animations.length > 0){
+                               requestAnimationFrame(this.startDigest);
+                       }
+               }
+       };
+
        // Attach global event to resize each chart instance when the browser resizes
        helpers.addEvent(window, "resize", (function(){
                // Basic debounce of resize function so it doesn't hurt performance when resizing browser.