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-05-04 03:05:54 +08:00
|
|
|
animRunning: false,
|
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-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-04-29 08:45:13 +08:00
|
|
|
/*
|
|
|
|
* animation support
|
|
|
|
*/
|
|
|
|
_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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
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 {
|
2012-05-04 03:05:54 +08:00
|
|
|
this.animRunning = false;
|
2012-04-03 12:03:59 +08:00
|
|
|
this.frame.lastTime = 0;
|
|
|
|
}
|
2012-03-07 13:45:48 +08:00
|
|
|
},
|
|
|
|
_handleAnimation: function() {
|
|
|
|
var that = this;
|
2012-05-04 03:05:54 +08:00
|
|
|
if(!this.animRunning) {
|
|
|
|
this.animRunning = true;
|
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
|
|
|
},
|
2012-04-29 08:45:13 +08:00
|
|
|
/*
|
|
|
|
* utilities
|
|
|
|
*/
|
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
|
|
|
},
|
2012-05-09 13:11:37 +08:00
|
|
|
_isArray: function(obj) {
|
|
|
|
return Object.prototype.toString.call(obj) == '[object Array]';
|
|
|
|
},
|
|
|
|
_isObject: function(obj) {
|
|
|
|
return obj === Object(obj);
|
|
|
|
},
|
2012-05-14 01:46:49 +08:00
|
|
|
/*
|
2012-05-14 02:58:40 +08:00
|
|
|
* takes the arguments passed into a function and
|
2012-05-14 01:46:49 +08:00
|
|
|
* creates a point object from it. The arguments
|
2012-05-14 02:58:40 +08:00
|
|
|
* can be a point object or an array of two elements
|
2012-05-14 01:46:49 +08:00
|
|
|
*/
|
|
|
|
_getXY: function(arg) {
|
2012-04-28 14:57:01 +08:00
|
|
|
if(arg.length === 1) {
|
|
|
|
return arg[0];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return {
|
|
|
|
x: arg[0],
|
|
|
|
y: arg[1]
|
|
|
|
}
|
|
|
|
}
|
2012-05-13 09:37:07 +08:00
|
|
|
},
|
2012-05-14 02:58:40 +08:00
|
|
|
/*
|
|
|
|
* val will be either a point object or an
|
|
|
|
* array with two elements
|
|
|
|
*/
|
2012-05-13 09:37:07 +08:00
|
|
|
_setXY: function(obj, key, val) {
|
2012-05-14 01:27:40 +08:00
|
|
|
if(obj[key] === undefined) {
|
|
|
|
obj[key] = {};
|
|
|
|
}
|
|
|
|
|
2012-05-13 09:37:07 +08:00
|
|
|
// val is an array
|
|
|
|
if(Kinetic.GlobalObject._isArray(val)) {
|
|
|
|
obj[key].x = val[0];
|
|
|
|
obj[key].y = val[1];
|
|
|
|
}
|
|
|
|
// val is an object
|
|
|
|
else if(obj[key] !== undefined) {
|
|
|
|
|
|
|
|
if(val.x !== undefined) {
|
|
|
|
obj[key].x = val.x;
|
|
|
|
}
|
|
|
|
if(val.y !== undefined) {
|
|
|
|
obj[key].y = val.y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2012-05-14 02:58:40 +08:00
|
|
|
/*
|
|
|
|
* val will be either an object with height and
|
|
|
|
* width properties or an array with four elements
|
|
|
|
* in which the last two elements are width and height
|
|
|
|
*/
|
2012-05-13 09:37:07 +08:00
|
|
|
_setSize: function(obj, key, val) {
|
2012-05-14 01:27:40 +08:00
|
|
|
if(obj[key] === undefined) {
|
|
|
|
obj[key] = {};
|
|
|
|
}
|
|
|
|
|
2012-05-13 09:37:07 +08:00
|
|
|
// val is an array
|
|
|
|
if(Kinetic.GlobalObject._isArray(val)) {
|
2012-05-14 02:32:26 +08:00
|
|
|
obj[key].width = val[2];
|
|
|
|
obj[key].height = val[3];
|
2012-05-13 09:37:07 +08:00
|
|
|
}
|
|
|
|
// val is an object
|
|
|
|
else if(obj[key] !== undefined) {
|
|
|
|
|
|
|
|
if(val.width !== undefined) {
|
|
|
|
obj[key].width = val.width;
|
|
|
|
}
|
|
|
|
if(val.y !== undefined) {
|
|
|
|
obj[key].height = val.height;
|
|
|
|
}
|
|
|
|
}
|
2012-05-14 02:58:40 +08:00
|
|
|
},
|
|
|
|
/*
|
|
|
|
* val will be either an array of numbers or
|
|
|
|
* an array of point objects
|
|
|
|
*/
|
|
|
|
_setPoints: function(obj, key, val) {
|
|
|
|
/*
|
|
|
|
* if points contains an array of objects, just set
|
|
|
|
* the attr normally
|
|
|
|
*/
|
|
|
|
if(this._isObject(val[0])) {
|
|
|
|
obj[key] = val;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/*
|
|
|
|
* convert array of numbers into an array
|
|
|
|
* of objects containing x, y
|
|
|
|
*/
|
|
|
|
var arr = [];
|
|
|
|
for(var n = 0; n < val.length; n += 2) {
|
|
|
|
arr.push({
|
|
|
|
x: val[n],
|
|
|
|
y: val[n + 1]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
obj[key] = arr;
|
|
|
|
}
|
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
|
|
|
})();
|