added local cached variables to improve performance for Node and Shape

This commit is contained in:
Eric Rowell
2012-11-14 00:07:59 -08:00
parent 914ee2fb4b
commit 38d78cc53a
2 changed files with 37 additions and 32 deletions

View File

@@ -238,10 +238,11 @@ Kinetic.Node = (function() {
* @methodOf Kinetic.Node.prototype * @methodOf Kinetic.Node.prototype
*/ */
getVisible: function() { getVisible: function() {
if(this.attrs.visible && this.getParent() && !this.getParent().getVisible()) { var visible = this.attrs.visible, parent = this.getParent();
if(visible && parent && !parent.getVisible()) {
return false; return false;
} }
return this.attrs.visible; return visible;
}, },
/** /**
* determine if node is listening or not. Node is listening only * determine if node is listening or not. Node is listening only
@@ -251,10 +252,11 @@ Kinetic.Node = (function() {
* @methodOf Kinetic.Node.prototype * @methodOf Kinetic.Node.prototype
*/ */
getListening: function() { getListening: function() {
if(this.attrs.listening && this.getParent() && !this.getParent().getListening()) { var listening = this.attrs.listening, parent = this.getParent();
if(listening && parent && !parent.getListening()) {
return false; return false;
} }
return this.attrs.listening; return listening;
}, },
/** /**
* show node * show node
@@ -349,9 +351,10 @@ Kinetic.Node = (function() {
* @methodOf Kinetic.Node.prototype * @methodOf Kinetic.Node.prototype
*/ */
getPosition: function() { getPosition: function() {
var attrs = this.attrs;
return { return {
x: this.attrs.x, x: attrs.x,
y: this.attrs.y y: attrs.y
}; };
}, },
/** /**
@@ -554,15 +557,14 @@ Kinetic.Node = (function() {
* @methodOf Kinetic.Node.prototype * @methodOf Kinetic.Node.prototype
*/ */
toObject: function() { toObject: function() {
var type = Kinetic.Type; var type = Kinetic.Type, obj = {}, attrs = this.attrs;
var obj = {};
obj.attrs = {}; obj.attrs = {};
// serialize only attributes that are not function, image, DOM, or objects with methods // serialize only attributes that are not function, image, DOM, or objects with methods
for(var key in this.attrs) { for(var key in attrs) {
var val = this.attrs[key]; var val = attrs[key];
if(!type._isFunction(val) && !type._isElement(val) && !(type._isObject(val) && kineticType._hasMethods(val))) { if(!type._isFunction(val) && !type._isElement(val) && !(type._isObject(val) && type._hasMethods(val))) {
obj.attrs[key] = val; obj.attrs[key] = val;
} }
} }
@@ -658,19 +660,19 @@ Kinetic.Node = (function() {
* @methodOf Kinetic.Node.prototype * @methodOf Kinetic.Node.prototype
*/ */
getTransform: function() { getTransform: function() {
var m = new Kinetic.Transform(); var m = new Kinetic.Transform(), attrs = this.attrs, x = attrs.x, y = attrs.y, rotation = attrs.rotation, scale = attrs.scale, scaleX = scale.x, scaleY = scale.y, offset = attrs.offset, offsetX = offset.x, offsetY = offset.y;
if(this.attrs.x !== 0 || this.attrs.y !== 0) { if(x !== 0 || y !== 0) {
m.translate(this.attrs.x, this.attrs.y); m.translate(x, y);
} }
if(this.attrs.rotation !== 0) { if(rotation !== 0) {
m.rotate(this.attrs.rotation); m.rotate(rotation);
} }
if(this.attrs.scale.x !== 1 || this.attrs.scale.y !== 1) { if(scaleX !== 1 || scaleY !== 1) {
m.scale(this.attrs.scale.x, this.attrs.scale.y); m.scale(scaleX, scaleY);
} }
if(this.attrs.offset && (this.attrs.offset.x !== 0 || this.attrs.offset.y !== 0)) { if(offsetX !== 0 || offsetY !== 0) {
m.translate(-1 * this.attrs.offset.x, -1 * this.attrs.offset.y); m.translate(-1 * offsetX, -1 * offsetY);
} }
return m; return m;
@@ -857,17 +859,18 @@ Kinetic.Node = (function() {
} }
}, },
_clearTransform: function() { _clearTransform: function() {
var attrs = this.attrs, scale = attrs.scale, offset = attrs.offset;
var trans = { var trans = {
x: this.attrs.x, x: attrs.x,
y: this.attrs.y, y: attrs.y,
rotation: this.attrs.rotation, rotation: attrs.rotation,
scale: { scale: {
x: this.attrs.scale.x, x: scale.x,
y: this.attrs.scale.y y: scale.y
}, },
offset: { offset: {
x: this.attrs.offset.x, x: offset.x,
y: this.attrs.offset.y y: offset.y
} }
}; };

View File

@@ -288,8 +288,9 @@ Kinetic.Shape = (function() {
* @methodOf Kinetic.Shape.prototype * @methodOf Kinetic.Shape.prototype
*/ */
applyLineJoin: function(context) { applyLineJoin: function(context) {
if(this.attrs.lineJoin) { var lineJoin = this.attrs.lineJoin;
context.lineJoin = this.attrs.lineJoin; if(lineJoin) {
context.lineJoin = lineJoin;
} }
}, },
/** /**
@@ -299,8 +300,9 @@ Kinetic.Shape = (function() {
* @methodOf Kinetic.Shape.prototype * @methodOf Kinetic.Shape.prototype
*/ */
applyLineCap: function(context) { applyLineCap: function(context) {
if(this.attrs.lineCap) { var lineCap = this.attrs.lineCap;
context.lineCap = this.attrs.lineCap; if(lineCap) {
context.lineCap = lineCap;
} }
}, },
/** /**
@@ -425,7 +427,7 @@ Kinetic.Shape = (function() {
delete Kinetic.Global.shapes[this.colorKey]; delete Kinetic.Global.shapes[this.colorKey];
}, },
draw: function(canvas) { draw: function(canvas) {
var drawFunc = this.attrs.drawFunc, drawBufferFunc = this.attrs.drawBufferFunc; var attrs = this.attrs, drawFunc = attrs.drawFunc, drawBufferFunc = attrs.drawBufferFunc;
if(drawFunc && Kinetic.Node.prototype._shouldDraw.call(this, canvas)) { if(drawFunc && Kinetic.Node.prototype._shouldDraw.call(this, canvas)) {
var stage = this.getStage(), context = canvas.getContext(), family = [], parent = this.parent, type = canvas.getContext().type; var stage = this.getStage(), context = canvas.getContext(), family = [], parent = this.parent, type = canvas.getContext().type;