2012-12-02 04:04:10 +08:00
|
|
|
(function() {
|
2012-08-04 15:23:56 +08:00
|
|
|
/**
|
2012-12-02 04:04:10 +08:00
|
|
|
* Stage constructor. A stage is used to contain multiple layers and handle
|
|
|
|
* animations
|
|
|
|
* @constructor
|
|
|
|
* @param {Function} func function executed on each animation frame
|
2013-01-14 11:59:35 +08:00
|
|
|
* @param {Kinetic.Node} [node] node to be redrawn. Can be a layer or the stage. Not specifying a node will result in no redraw.
|
2012-08-04 15:23:56 +08:00
|
|
|
*/
|
2012-12-02 04:04:10 +08:00
|
|
|
Kinetic.Animation = function(func, node) {
|
|
|
|
this.func = func;
|
|
|
|
this.node = node;
|
|
|
|
this.id = Kinetic.Animation.animIdCounter++;
|
2012-12-12 14:34:58 +08:00
|
|
|
this.frame = {
|
|
|
|
time: 0,
|
|
|
|
timeDiff: 0,
|
|
|
|
lastTime: new Date().getTime()
|
|
|
|
};
|
2012-12-02 04:04:10 +08:00
|
|
|
};
|
|
|
|
/*
|
|
|
|
* Animation methods
|
2012-08-04 15:23:56 +08:00
|
|
|
*/
|
2012-12-02 04:04:10 +08:00
|
|
|
Kinetic.Animation.prototype = {
|
2013-01-14 11:59:35 +08:00
|
|
|
/**
|
|
|
|
* determine if animation is running. returns true or false
|
|
|
|
* @name isRunning
|
|
|
|
* @methodOf Kinetic.Aniamtion.prototype
|
|
|
|
*/
|
|
|
|
isRunning: function() {
|
|
|
|
var a = Kinetic.Animation, animations = a.animations;
|
|
|
|
for(var n = 0; n < animations.length; n++) {
|
|
|
|
if(animations[n].id === this.id) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
2012-12-02 04:04:10 +08:00
|
|
|
/**
|
|
|
|
* start animation
|
|
|
|
* @name start
|
|
|
|
* @methodOf Kinetic.Animation.prototype
|
|
|
|
*/
|
|
|
|
start: function() {
|
|
|
|
this.stop();
|
2012-12-12 14:34:58 +08:00
|
|
|
this.frame.timeDiff = 0;
|
|
|
|
this.frame.lastTime = new Date().getTime();
|
2012-12-02 04:04:10 +08:00
|
|
|
Kinetic.Animation._addAnimation(this);
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* stop animation
|
|
|
|
* @name stop
|
|
|
|
* @methodOf Kinetic.Animation.prototype
|
|
|
|
*/
|
|
|
|
stop: function() {
|
|
|
|
Kinetic.Animation._removeAnimation(this);
|
2012-12-12 14:34:58 +08:00
|
|
|
},
|
2013-01-08 11:36:12 +08:00
|
|
|
_updateFrameObject: function(time) {
|
2012-12-12 14:34:58 +08:00
|
|
|
this.frame.timeDiff = time - this.frame.lastTime;
|
|
|
|
this.frame.lastTime = time;
|
|
|
|
this.frame.time += this.frame.timeDiff;
|
|
|
|
this.frame.frameRate = 1000 / this.frame.timeDiff;
|
2012-12-02 04:04:10 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
Kinetic.Animation.animations = [];
|
|
|
|
Kinetic.Animation.animIdCounter = 0;
|
|
|
|
Kinetic.Animation.animRunning = false;
|
2012-11-04 02:16:33 +08:00
|
|
|
|
2012-12-02 04:04:10 +08:00
|
|
|
Kinetic.Animation.fixedRequestAnimFrame = function(callback) {
|
|
|
|
window.setTimeout(callback, 1000 / 60);
|
|
|
|
};
|
2012-11-04 02:16:33 +08:00
|
|
|
|
2012-12-02 04:04:10 +08:00
|
|
|
Kinetic.Animation._addAnimation = function(anim) {
|
|
|
|
this.animations.push(anim);
|
2012-12-31 15:14:23 +08:00
|
|
|
this._handleAnimation();
|
2012-12-02 04:04:10 +08:00
|
|
|
};
|
|
|
|
Kinetic.Animation._removeAnimation = function(anim) {
|
2013-01-08 11:38:16 +08:00
|
|
|
var id = anim.id, animations = this.animations, len = animations.length;
|
|
|
|
for(var n = 0; n < len; n++) {
|
2012-12-02 04:04:10 +08:00
|
|
|
if(animations[n].id === id) {
|
|
|
|
this.animations.splice(n, 1);
|
|
|
|
break;
|
|
|
|
}
|
2012-07-04 14:00:52 +08:00
|
|
|
}
|
2012-12-02 04:04:10 +08:00
|
|
|
};
|
2012-12-12 14:34:58 +08:00
|
|
|
|
2012-12-02 04:04:10 +08:00
|
|
|
Kinetic.Animation._runFrames = function() {
|
2013-01-10 00:27:57 +08:00
|
|
|
var nodes = {}, animations = this.animations;
|
2012-12-02 04:04:10 +08:00
|
|
|
/*
|
|
|
|
* loop through all animations and execute animation
|
|
|
|
* function. if the animation object has specified node,
|
|
|
|
* we can add the node to the nodes hash to eliminate
|
|
|
|
* drawing the same node multiple times. The node property
|
|
|
|
* can be the stage itself or a layer
|
|
|
|
*/
|
2013-01-10 00:27:57 +08:00
|
|
|
/*
|
|
|
|
* WARNING: don't cache animations.length because it could change while
|
|
|
|
* the for loop is running, causing a JS error
|
|
|
|
*/
|
|
|
|
for(var n = 0; n < animations.length; n++) {
|
2013-01-08 11:36:12 +08:00
|
|
|
var anim = animations[n], node = anim.node, func = anim.func;
|
|
|
|
anim._updateFrameObject(new Date().getTime());
|
|
|
|
if(node && node._id !== undefined) {
|
|
|
|
nodes[node._id] = node;
|
2012-12-02 04:04:10 +08:00
|
|
|
}
|
|
|
|
// if animation object has a function, execute it
|
2013-01-08 11:36:12 +08:00
|
|
|
if(func) {
|
|
|
|
func(anim.frame);
|
2012-12-02 04:04:10 +08:00
|
|
|
}
|
2012-07-04 14:00:52 +08:00
|
|
|
}
|
2012-08-01 14:23:00 +08:00
|
|
|
|
2012-12-02 04:04:10 +08:00
|
|
|
for(var key in nodes) {
|
|
|
|
nodes[key].draw();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Kinetic.Animation._animationLoop = function() {
|
2013-01-08 11:36:12 +08:00
|
|
|
var that = this;
|
2012-12-02 04:04:10 +08:00
|
|
|
if(this.animations.length > 0) {
|
|
|
|
this._runFrames();
|
|
|
|
Kinetic.Animation.requestAnimFrame(function() {
|
|
|
|
that._animationLoop();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.animRunning = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Kinetic.Animation._handleAnimation = function() {
|
2012-07-04 14:00:52 +08:00
|
|
|
var that = this;
|
2012-12-02 04:04:10 +08:00
|
|
|
if(!this.animRunning) {
|
|
|
|
this.animRunning = true;
|
2012-07-04 14:00:52 +08:00
|
|
|
that._animationLoop();
|
2012-12-02 04:04:10 +08:00
|
|
|
}
|
|
|
|
};
|
2013-01-08 11:36:12 +08:00
|
|
|
RAF = (function() {
|
|
|
|
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || Kinetic.Animation.fixedRequestAnimFrame;
|
|
|
|
})();
|
2012-12-02 04:04:10 +08:00
|
|
|
|
2013-01-08 11:36:12 +08:00
|
|
|
Kinetic.Animation.requestAnimFrame = function(callback) {
|
|
|
|
RAF(callback);
|
2012-12-02 04:04:10 +08:00
|
|
|
};
|
|
|
|
})();
|