mirror of
https://github.com/konvajs/konva.git
synced 2025-06-28 15:23:44 +08:00
84 lines
2.4 KiB
JavaScript
84 lines
2.4 KiB
JavaScript
![]() |
///////////////////////////////////////////////////////////////////////
|
||
|
// Animation
|
||
|
///////////////////////////////////////////////////////////////////////
|
||
|
Kinetic.Animation = {
|
||
|
animations: [],
|
||
|
animIdCounter: 0,
|
||
|
animRunning: false,
|
||
|
frame: {
|
||
|
time: 0,
|
||
|
timeDiff: 0,
|
||
|
lastTime: 0
|
||
|
},
|
||
|
_addAnimation: function(anim) {
|
||
|
anim.id = this.animIdCounter++;
|
||
|
this.animations.push(anim);
|
||
|
},
|
||
|
_removeAnimation: function(anim) {
|
||
|
var id = anim.id;
|
||
|
var animations = this.animations;
|
||
|
for(var n = 0; n < animations.length; n++) {
|
||
|
if(animations[n].id === id) {
|
||
|
this.animations.splice(n, 1);
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
},
|
||
|
_runFrames: function() {
|
||
|
var nodes = {};
|
||
|
for(var n = 0; n < this.animations.length; n++) {
|
||
|
var anim = this.animations[n];
|
||
|
if(anim.node && anim.node._id !== undefined) {
|
||
|
nodes[anim.node._id] = anim.node;
|
||
|
}
|
||
|
anim.func(this.frame);
|
||
|
}
|
||
|
|
||
|
for(var key in nodes) {
|
||
|
nodes[key].draw();
|
||
|
}
|
||
|
},
|
||
|
_updateFrameObject: function() {
|
||
|
var date = new Date();
|
||
|
var time = date.getTime();
|
||
|
if(this.frame.lastTime === 0) {
|
||
|
this.frame.lastTime = time;
|
||
|
}
|
||
|
else {
|
||
|
this.frame.timeDiff = time - this.frame.lastTime;
|
||
|
this.frame.lastTime = time;
|
||
|
this.frame.time += this.frame.timeDiff;
|
||
|
}
|
||
|
},
|
||
|
_animationLoop: function() {
|
||
|
if(this.animations.length > 0) {
|
||
|
this._updateFrameObject();
|
||
|
this._runFrames();
|
||
|
var that = this;
|
||
|
requestAnimFrame(function() {
|
||
|
that._animationLoop();
|
||
|
});
|
||
|
}
|
||
|
else {
|
||
|
this.animRunning = false;
|
||
|
this.frame.lastTime = 0;
|
||
|
}
|
||
|
},
|
||
|
_handleAnimation: function() {
|
||
|
var that = this;
|
||
|
if(!this.animRunning) {
|
||
|
this.animRunning = true;
|
||
|
that._animationLoop();
|
||
|
}
|
||
|
else {
|
||
|
this.frame.lastTime = 0;
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
requestAnimFrame = (function(callback) {
|
||
|
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
|
||
|
function(callback) {
|
||
|
window.setTimeout(callback, 1000 / 60);
|
||
|
};
|
||
|
})();
|