2012-03-07 13:45:48 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
// Global Object
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
/**
|
|
|
|
* Kinetic Namespace
|
|
|
|
* @namespace
|
|
|
|
*/
|
|
|
|
var Kinetic = {};
|
|
|
|
/**
|
|
|
|
* Kinetic Global Object
|
|
|
|
* @property {Object} GlobalObjet
|
|
|
|
*/
|
|
|
|
Kinetic.GlobalObject = {
|
|
|
|
stages: [],
|
|
|
|
idCounter: 0,
|
2012-04-09 02:01:31 +08:00
|
|
|
tempNodes: [],
|
2012-04-03 12:03:59 +08:00
|
|
|
animations: [],
|
|
|
|
animIdCounter: 0,
|
2012-04-13 12:33:40 +08:00
|
|
|
dragTimeInterval: 0,
|
2012-04-15 01:04:17 +08:00
|
|
|
maxDragTimeInterval: 20,
|
2012-03-07 13:45:48 +08:00
|
|
|
frame: {
|
|
|
|
time: 0,
|
|
|
|
timeDiff: 0,
|
|
|
|
lastTime: 0
|
|
|
|
},
|
|
|
|
drag: {
|
|
|
|
moving: false,
|
|
|
|
node: undefined,
|
|
|
|
offset: {
|
|
|
|
x: 0,
|
|
|
|
y: 0
|
2012-04-13 12:33:40 +08:00
|
|
|
},
|
|
|
|
lastDrawTime: 0
|
2012-03-07 13:45:48 +08:00
|
|
|
},
|
|
|
|
extend: function(obj1, obj2) {
|
|
|
|
for(var key in obj2.prototype) {
|
2012-03-11 08:52:16 +08:00
|
|
|
if(obj2.prototype.hasOwnProperty(key) && obj1.prototype[key] === undefined) {
|
2012-03-07 13:45:48 +08:00
|
|
|
obj1.prototype[key] = obj2.prototype[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2012-04-29 02:23:47 +08:00
|
|
|
_addAnimation: function(anim) {
|
2012-04-29 02:41:58 +08:00
|
|
|
anim.id = this.animIdCounter++;
|
2012-04-04 13:23:13 +08:00
|
|
|
this.animations.push(anim);
|
2012-04-03 13:54:05 +08:00
|
|
|
},
|
2012-04-29 02:23:47 +08:00
|
|
|
_removeAnimation: function(id) {
|
2012-04-03 13:54:05 +08:00
|
|
|
var animations = this.animations;
|
|
|
|
for(var n = 0; n < animations.length; n++) {
|
|
|
|
if(animations[n].id === id) {
|
|
|
|
this.animations.splice(n, 1);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2012-04-09 02:01:31 +08:00
|
|
|
_pullNodes: function(stage) {
|
2012-04-29 02:41:58 +08:00
|
|
|
var tempNodes = this.tempNodes;
|
2012-04-09 02:01:31 +08:00
|
|
|
for(var n = 0; n < tempNodes.length; n++) {
|
|
|
|
var node = tempNodes[n];
|
|
|
|
if(node.getStage() !== undefined && node.getStage()._id === stage._id) {
|
|
|
|
stage._addId(node);
|
|
|
|
stage._addName(node);
|
2012-04-29 02:41:58 +08:00
|
|
|
this.tempNodes.splice(n, 1);
|
2012-04-09 02:01:31 +08:00
|
|
|
n -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2012-03-07 13:45:48 +08:00
|
|
|
_runFrames: function() {
|
2012-04-04 14:00:35 +08:00
|
|
|
var nodes = {};
|
2012-04-03 12:03:59 +08:00
|
|
|
for(var n = 0; n < this.animations.length; n++) {
|
2012-04-03 13:54:05 +08:00
|
|
|
var anim = this.animations[n];
|
2012-04-09 02:01:31 +08:00
|
|
|
if(anim.node && anim.node._id !== undefined) {
|
|
|
|
nodes[anim.node._id] = anim.node;
|
2012-04-03 14:38:14 +08:00
|
|
|
}
|
2012-04-03 13:54:05 +08:00
|
|
|
anim.func(this.frame);
|
2012-04-03 12:03:59 +08:00
|
|
|
}
|
2012-03-13 13:41:09 +08:00
|
|
|
|
2012-04-04 14:00:35 +08:00
|
|
|
for(var key in nodes) {
|
|
|
|
nodes[key].draw();
|
2012-04-03 13:54:05 +08:00
|
|
|
}
|
2012-03-07 13:45:48 +08:00
|
|
|
},
|
|
|
|
_updateFrameObject: function() {
|
|
|
|
var date = new Date();
|
|
|
|
var time = date.getTime();
|
|
|
|
if(this.frame.lastTime === 0) {
|
|
|
|
this.frame.lastTime = time;
|
2012-03-14 12:16:25 +08:00
|
|
|
}
|
|
|
|
else {
|
2012-03-07 13:45:48 +08:00
|
|
|
this.frame.timeDiff = time - this.frame.lastTime;
|
|
|
|
this.frame.lastTime = time;
|
|
|
|
this.frame.time += this.frame.timeDiff;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_animationLoop: function() {
|
2012-04-03 12:03:59 +08:00
|
|
|
if(this.animations.length > 0) {
|
2012-03-07 13:45:48 +08:00
|
|
|
this._updateFrameObject();
|
|
|
|
this._runFrames();
|
|
|
|
var that = this;
|
|
|
|
requestAnimFrame(function() {
|
|
|
|
that._animationLoop();
|
|
|
|
});
|
|
|
|
}
|
2012-04-03 12:03:59 +08:00
|
|
|
else {
|
|
|
|
this.frame.lastTime = 0;
|
|
|
|
}
|
2012-03-07 13:45:48 +08:00
|
|
|
},
|
|
|
|
_handleAnimation: function() {
|
|
|
|
var that = this;
|
2012-04-03 12:03:59 +08:00
|
|
|
if(this.animations.length > 0) {
|
2012-03-07 13:45:48 +08:00
|
|
|
that._animationLoop();
|
2012-03-14 12:16:25 +08:00
|
|
|
}
|
2012-04-03 12:03:59 +08:00
|
|
|
else {
|
|
|
|
this.frame.lastTime = 0;
|
|
|
|
}
|
2012-04-28 10:42:04 +08:00
|
|
|
},
|
|
|
|
_isElement: function(obj) {
|
|
|
|
return !!(obj && obj.nodeType == 1);
|
|
|
|
},
|
|
|
|
_isFunction: function(obj) {
|
|
|
|
return !!(obj && obj.constructor && obj.call && obj.apply);
|
2012-04-28 14:57:01 +08:00
|
|
|
},
|
|
|
|
_getPoint: function(arg) {
|
|
|
|
|
|
|
|
if(arg.length === 1) {
|
|
|
|
return arg[0];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return {
|
|
|
|
x: arg[0],
|
|
|
|
y: arg[1]
|
|
|
|
}
|
|
|
|
}
|
2012-03-07 13:45:48 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
window.requestAnimFrame = (function(callback) {
|
|
|
|
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
|
|
|
|
function(callback) {
|
|
|
|
window.setTimeout(callback, 1000 / 60);
|
|
|
|
};
|
2012-03-11 08:52:16 +08:00
|
|
|
})();
|