moved animation logic into Animation.js. Global.js now only contains logic and properties that operate on stages

This commit is contained in:
Eric Rowell 2012-07-03 23:00:52 -07:00
parent 6126c73a84
commit de7cab4cf3
13 changed files with 228 additions and 209 deletions

View File

@ -3,7 +3,7 @@ require 'json/pure'
class Build < Thor class Build < Thor
# This is the list of files to concatenate. The first file will appear at the top of the final file. All files are relative to the lib directory. # This is the list of files to concatenate. The first file will appear at the top of the final file. All files are relative to the lib directory.
FILES = [ FILES = [
"license.js", "src/Global.js", "src/util/Type.js", "src/util/Class.js", "src/Node.js", "src/Container.js", "src/Stage.js", "license.js", "src/Global.js", "src/util/Type.js", "src/util/Class.js", "src/Animation.js", "src/Node.js", "src/Container.js", "src/Stage.js",
"src/Layer.js", "src/Group.js", "src/Shape.js", "src/shapes/Rect.js", "src/shapes/Ellipse.js", "src/shapes/Image.js", "src/Layer.js", "src/Group.js", "src/Shape.js", "src/shapes/Rect.js", "src/shapes/Ellipse.js", "src/shapes/Image.js",
"src/shapes/Sprite.js", "src/shapes/Polygon.js", "src/shapes/RegularPolygon.js", "src/shapes/Star.js", "src/shapes/Text.js", "src/shapes/Sprite.js", "src/shapes/Polygon.js", "src/shapes/RegularPolygon.js", "src/shapes/Star.js", "src/shapes/Text.js",
"src/shapes/Line.js", "src/shapes/Path.js", "src/util/Transform.js", "src/Transition.js", "src/util/Tween.js" "src/shapes/Line.js", "src/shapes/Path.js", "src/util/Transform.js", "src/Transition.js", "src/util/Tween.js"

208
dist/kinetic-core.js vendored
View File

@ -27,33 +27,20 @@
*/ */
/////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////
// Global Object // Global
/////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////
/** /**
* Kinetic Namespace * Kinetic Namespace
* @namespace * @namespace
*/ */
var Kinetic = {}; var Kinetic = {};
/**
* Kinetic Global
* @property {Object} Global
*/
Kinetic.Global = { Kinetic.Global = {
stages: [], stages: [],
idCounter: 0, idCounter: 0,
tempNodes: [], tempNodes: [],
animations: [],
animIdCounter: 0,
animRunning: false,
maxDragTimeInterval: 20, maxDragTimeInterval: 20,
frame: {
time: 0,
timeDiff: 0,
lastTime: 0
},
drag: { drag: {
moving: false, moving: false,
node: undefined,
offset: { offset: {
x: 0, x: 0,
y: 0 y: 0
@ -71,83 +58,12 @@ Kinetic.Global = {
n -= 1; n -= 1;
} }
} }
},
/*
* 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;
}
}
},
_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;
}
} }
}; };
window.requestAnimFrame = (function(callback) { ///////////////////////////////////////////////////////////////////////
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || // Type
function(callback) { ///////////////////////////////////////////////////////////////////////
window.setTimeout(callback, 1000 / 60);
};
})();
/* /*
* utilities that determine data type and transform * utilities that determine data type and transform
* one type into another * one type into another
@ -355,6 +271,9 @@ Kinetic.Type = {
} }
}; };
///////////////////////////////////////////////////////////////////////
// Class
///////////////////////////////////////////////////////////////////////
/* Simple JavaScript Inheritance /* Simple JavaScript Inheritance
* By John Resig http://ejohn.org/ * By John Resig http://ejohn.org/
* MIT Licensed. * MIT Licensed.
@ -415,6 +334,90 @@ Kinetic.Type = {
return Class; return Class;
}; };
})(); })();
///////////////////////////////////////////////////////////////////////
// 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);
};
})();
/////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////
// Node // Node
/////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////
@ -1003,14 +1006,14 @@ Kinetic.Node = Kinetic.Class.extend({
* transition completes * transition completes
*/ */
transitionTo: function(config) { transitionTo: function(config) {
var go = Kinetic.Global; var a = Kinetic.Animation;
/* /*
* clear transition if one is currently running for this * clear transition if one is currently running for this
* node * node
*/ */
if(this.transAnim !== undefined) { if(this.transAnim !== undefined) {
go._removeAnimation(this.transAnim); a._removeAnimation(this.transAnim);
this.transAnim = undefined; this.transAnim = undefined;
} }
@ -1034,12 +1037,12 @@ Kinetic.Node = Kinetic.Class.extend({
* adding the animation with the addAnimation * adding the animation with the addAnimation
* method auto generates an id * method auto generates an id
*/ */
go._addAnimation(anim); a._addAnimation(anim);
// subscribe to onFinished for first tween // subscribe to onFinished for first tween
trans.onFinished = function() { trans.onFinished = function() {
// remove animation // remove animation
go._removeAnimation(anim); a._removeAnimation(anim);
that.transAnim = undefined; that.transAnim = undefined;
// callback // callback
@ -1052,7 +1055,7 @@ Kinetic.Node = Kinetic.Class.extend({
// auto start // auto start
trans.start(); trans.start();
go._handleAnimation(); a._handleAnimation();
return trans; return trans;
}, },
@ -1676,7 +1679,6 @@ Kinetic.Stage = Kinetic.Container.extend({
* @param {function} func * @param {function} func
*/ */
onFrame: function(func) { onFrame: function(func) {
var go = Kinetic.Global;
this.anim = { this.anim = {
func: func func: func
}; };
@ -1686,9 +1688,9 @@ Kinetic.Stage = Kinetic.Container.extend({
*/ */
start: function() { start: function() {
if(!this.animRunning) { if(!this.animRunning) {
var go = Kinetic.Global; var a = Kinetic.Animation;
go._addAnimation(this.anim); a._addAnimation(this.anim);
go._handleAnimation(); a._handleAnimation();
this.animRunning = true; this.animRunning = true;
} }
}, },
@ -1696,8 +1698,7 @@ Kinetic.Stage = Kinetic.Container.extend({
* stop animation * stop animation
*/ */
stop: function() { stop: function() {
var go = Kinetic.Global; Kinetic.Animation._removeAnimation(this.anim);
go._removeAnimation(this.anim);
this.animRunning = false; this.animRunning = false;
}, },
/** /**
@ -4792,6 +4793,9 @@ Kinetic.Node.addGettersSetters(Kinetic.Path, ['data']);
* @name getData * @name getData
* @methodOf Kinetic.Path.prototype * @methodOf Kinetic.Path.prototype
*/ */
///////////////////////////////////////////////////////////////////////
// Transform
///////////////////////////////////////////////////////////////////////
/* /*
* Last updated November 2011 * Last updated November 2011
* By Simon Sarris * By Simon Sarris
@ -4909,6 +4913,9 @@ Kinetic.Transform.prototype = {
} }
}; };
///////////////////////////////////////////////////////////////////////
// Transition
///////////////////////////////////////////////////////////////////////
/** /**
* Transition constructor. The transitionTo() Node method * Transition constructor. The transitionTo() Node method
* returns a reference to the transition object which you can use * returns a reference to the transition object which you can use
@ -5003,6 +5010,9 @@ Kinetic.Transition.prototype = {
} }
}; };
///////////////////////////////////////////////////////////////////////
// Tween
///////////////////////////////////////////////////////////////////////
/* /*
* The Tween class was ported from an Adobe Flash Tween library * The Tween class was ported from an Adobe Flash Tween library
* to JavaScript by Xaric. In the context of KineticJS, a Tween is * to JavaScript by Xaric. In the context of KineticJS, a Tween is

File diff suppressed because one or more lines are too long

83
src/Animation.js Normal file
View File

@ -0,0 +1,83 @@
///////////////////////////////////////////////////////////////////////
// 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);
};
})();

View File

@ -1,31 +1,18 @@
/////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////
// Global Object // Global
/////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////
/** /**
* Kinetic Namespace * Kinetic Namespace
* @namespace * @namespace
*/ */
var Kinetic = {}; var Kinetic = {};
/**
* Kinetic Global
* @property {Object} Global
*/
Kinetic.Global = { Kinetic.Global = {
stages: [], stages: [],
idCounter: 0, idCounter: 0,
tempNodes: [], tempNodes: [],
animations: [],
animIdCounter: 0,
animRunning: false,
maxDragTimeInterval: 20, maxDragTimeInterval: 20,
frame: {
time: 0,
timeDiff: 0,
lastTime: 0
},
drag: { drag: {
moving: false, moving: false,
node: undefined,
offset: { offset: {
x: 0, x: 0,
y: 0 y: 0
@ -43,79 +30,5 @@ Kinetic.Global = {
n -= 1; n -= 1;
} }
} }
},
/*
* 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;
}
}
},
_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;
}
} }
}; };
window.requestAnimFrame = (function(callback) {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000 / 60);
};
})();

View File

@ -586,14 +586,14 @@ Kinetic.Node = Kinetic.Class.extend({
* transition completes * transition completes
*/ */
transitionTo: function(config) { transitionTo: function(config) {
var go = Kinetic.Global; var a = Kinetic.Animation;
/* /*
* clear transition if one is currently running for this * clear transition if one is currently running for this
* node * node
*/ */
if(this.transAnim !== undefined) { if(this.transAnim !== undefined) {
go._removeAnimation(this.transAnim); a._removeAnimation(this.transAnim);
this.transAnim = undefined; this.transAnim = undefined;
} }
@ -617,12 +617,12 @@ Kinetic.Node = Kinetic.Class.extend({
* adding the animation with the addAnimation * adding the animation with the addAnimation
* method auto generates an id * method auto generates an id
*/ */
go._addAnimation(anim); a._addAnimation(anim);
// subscribe to onFinished for first tween // subscribe to onFinished for first tween
trans.onFinished = function() { trans.onFinished = function() {
// remove animation // remove animation
go._removeAnimation(anim); a._removeAnimation(anim);
that.transAnim = undefined; that.transAnim = undefined;
// callback // callback
@ -635,7 +635,7 @@ Kinetic.Node = Kinetic.Class.extend({
// auto start // auto start
trans.start(); trans.start();
go._handleAnimation(); a._handleAnimation();
return trans; return trans;
}, },

View File

@ -53,7 +53,6 @@ Kinetic.Stage = Kinetic.Container.extend({
* @param {function} func * @param {function} func
*/ */
onFrame: function(func) { onFrame: function(func) {
var go = Kinetic.Global;
this.anim = { this.anim = {
func: func func: func
}; };
@ -63,9 +62,9 @@ Kinetic.Stage = Kinetic.Container.extend({
*/ */
start: function() { start: function() {
if(!this.animRunning) { if(!this.animRunning) {
var go = Kinetic.Global; var a = Kinetic.Animation;
go._addAnimation(this.anim); a._addAnimation(this.anim);
go._handleAnimation(); a._handleAnimation();
this.animRunning = true; this.animRunning = true;
} }
}, },
@ -73,8 +72,7 @@ Kinetic.Stage = Kinetic.Container.extend({
* stop animation * stop animation
*/ */
stop: function() { stop: function() {
var go = Kinetic.Global; Kinetic.Animation._removeAnimation(this.anim);
go._removeAnimation(this.anim);
this.animRunning = false; this.animRunning = false;
}, },
/** /**

View File

@ -1,3 +1,6 @@
///////////////////////////////////////////////////////////////////////
// Transition
///////////////////////////////////////////////////////////////////////
/** /**
* Transition constructor. The transitionTo() Node method * Transition constructor. The transitionTo() Node method
* returns a reference to the transition object which you can use * returns a reference to the transition object which you can use

View File

@ -1,3 +1,6 @@
///////////////////////////////////////////////////////////////////////
// Class
///////////////////////////////////////////////////////////////////////
/* Simple JavaScript Inheritance /* Simple JavaScript Inheritance
* By John Resig http://ejohn.org/ * By John Resig http://ejohn.org/
* MIT Licensed. * MIT Licensed.

View File

@ -1,3 +1,6 @@
///////////////////////////////////////////////////////////////////////
// Transform
///////////////////////////////////////////////////////////////////////
/* /*
* Last updated November 2011 * Last updated November 2011
* By Simon Sarris * By Simon Sarris

View File

@ -1,3 +1,6 @@
///////////////////////////////////////////////////////////////////////
// Tween
///////////////////////////////////////////////////////////////////////
/* /*
* The Tween class was ported from an Adobe Flash Tween library * The Tween class was ported from an Adobe Flash Tween library
* to JavaScript by Xaric. In the context of KineticJS, a Tween is * to JavaScript by Xaric. In the context of KineticJS, a Tween is

View File

@ -1,3 +1,6 @@
///////////////////////////////////////////////////////////////////////
// Type
///////////////////////////////////////////////////////////////////////
/* /*
* utilities that determine data type and transform * utilities that determine data type and transform
* one type into another * one type into another

View File

@ -667,7 +667,7 @@ Test.prototype.tests = {
id: 'myCircle' id: 'myCircle'
}); });
var go = Kinetic.GlobalObject; var go = Kinetic.Global;
test(go.tempNodes.length === 0, 'shouldn\'t be nodes in the tempNdoes array'); test(go.tempNodes.length === 0, 'shouldn\'t be nodes in the tempNdoes array');
@ -4470,29 +4470,29 @@ Test.prototype.tests = {
rect.setX(amplitude * Math.sin(frame.time * 2 * Math.PI / period) + centerX); rect.setX(amplitude * Math.sin(frame.time * 2 * Math.PI / period) + centerX);
layer.draw(); layer.draw();
}); });
var go = Kinetic.GlobalObject; var a = Kinetic.Animation;
test(go.animations.length === 0, 'should be no animations running'); test(a.animations.length === 0, 'should be no animations running');
test(stage.animRunning === false, 'animRunning should be false'); test(stage.animRunning === false, 'animRunning should be false');
stage.start(); stage.start();
test(go.animations.length === 1, 'should be 1 animation running'); test(a.animations.length === 1, 'should be 1 animation running');
test(stage.animRunning === true, 'animRunning should be true'); test(stage.animRunning === true, 'animRunning should be true');
stage.start(); stage.start();
test(go.animations.length === 1, 'should be 1 animation running'); test(a.animations.length === 1, 'should be 1 animation running');
test(stage.animRunning === true, 'animRunning should be true'); test(stage.animRunning === true, 'animRunning should be true');
stage.stop(); stage.stop();
test(go.animations.length === 0, 'should be no animations running'); test(a.animations.length === 0, 'should be no animations running');
test(stage.animRunning === false, 'animRunning should be false'); test(stage.animRunning === false, 'animRunning should be false');
stage.stop(); stage.stop();
test(go.animations.length === 0, 'should be no animations running'); test(a.animations.length === 0, 'should be no animations running');
test(stage.animRunning === false, 'animRunning should be false'); test(stage.animRunning === false, 'animRunning should be false');
}, },
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
@ -4532,7 +4532,7 @@ Test.prototype.tests = {
stage.stop(); stage.stop();
centerX = 300; centerX = 300;
var go = Kinetic.GlobalObject; var go = Kinetic.Global;
rect.transitionTo({ rect.transitionTo({
x: 300, x: 300,