pulled out _setPointAttr from Node and moved it to the GlobalObject as _setXY, which enables x,y property setting for any object, not just attrs. also added _setSize which has similar logic for height width properties

This commit is contained in:
Eric Rowell 2012-05-12 18:37:07 -07:00
parent 9e3475f37a
commit 81df49e75d
5 changed files with 81 additions and 61 deletions

68
dist/kinetic-core.js vendored
View File

@ -172,6 +172,40 @@ Kinetic.GlobalObject = {
y: arg[1]
}
}
},
_setXY: function(obj, key, val) {
// 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;
}
}
},
_setSize: function(obj, key, val) {
// val is an array
if(Kinetic.GlobalObject._isArray(val)) {
obj[key].x = val[2];
obj[key].y = val[3];
}
// 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;
}
}
}
};
@ -362,13 +396,13 @@ Kinetic.Node.prototype = {
* config objects
*/
case 'centerOffset':
this._setPointAttr(key, val);
go._setXY(this.attrs, key, val);
break;
case 'shadowOffset':
this._setPointAttr(key, val);
go._setXY(this.attrs, key, val);
break;
case 'scale':
this._setPointAttr(key, val);
go._setXY(this.attrs, key, val);
break;
case 'points':
/*
@ -394,13 +428,8 @@ Kinetic.Node.prototype = {
}
break;
case 'crop':
this._setPointAttr(key, val);
if(val.width !== undefined) {
this.attrs[key].width = val.width;
}
if(val.height !== undefined) {
this.attrs[key].height = val.height;
}
go._setXY(this.attrs, key, val);
go._setSize(this.attrs, key, val);
break;
default:
this.attrs[key] = config[key];
@ -1063,25 +1092,6 @@ Kinetic.Node.prototype = {
if(!evt.cancelBubble && node.parent.nodeType !== 'Stage') {
this._handleEvent(node.parent, mouseoverParent, mouseoutParent, eventType, evt);
}
},
/**
* set point attr
*/
_setPointAttr: function(key, val) {
if(Kinetic.GlobalObject._isArray(val)) {
// val is an array
this.attrs[key].x = val[0];
this.attrs[key].y = val[1];
}
else {
// val is an object
if(val.x !== undefined) {
this.attrs[key].x = val.x;
}
if(val.y !== undefined) {
this.attrs[key].y = val.y;
}
}
}
};

File diff suppressed because one or more lines are too long

View File

@ -144,6 +144,40 @@ Kinetic.GlobalObject = {
y: arg[1]
}
}
},
_setXY: function(obj, key, val) {
// 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;
}
}
},
_setSize: function(obj, key, val) {
// val is an array
if(Kinetic.GlobalObject._isArray(val)) {
obj[key].x = val[2];
obj[key].y = val[3];
}
// 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;
}
}
}
};

View File

@ -178,13 +178,13 @@ Kinetic.Node.prototype = {
* config objects
*/
case 'centerOffset':
this._setPointAttr(key, val);
go._setXY(this.attrs, key, val);
break;
case 'shadowOffset':
this._setPointAttr(key, val);
go._setXY(this.attrs, key, val);
break;
case 'scale':
this._setPointAttr(key, val);
go._setXY(this.attrs, key, val);
break;
case 'points':
/*
@ -210,13 +210,8 @@ Kinetic.Node.prototype = {
}
break;
case 'crop':
this._setPointAttr(key, val);
if(val.width !== undefined) {
this.attrs[key].width = val.width;
}
if(val.height !== undefined) {
this.attrs[key].height = val.height;
}
go._setXY(this.attrs, key, val);
go._setSize(this.attrs, key, val);
break;
default:
this.attrs[key] = config[key];
@ -879,24 +874,5 @@ Kinetic.Node.prototype = {
if(!evt.cancelBubble && node.parent.nodeType !== 'Stage') {
this._handleEvent(node.parent, mouseoverParent, mouseoutParent, eventType, evt);
}
},
/**
* set point attr
*/
_setPointAttr: function(key, val) {
if(Kinetic.GlobalObject._isArray(val)) {
// val is an array
this.attrs[key].x = val[0];
this.attrs[key].y = val[1];
}
else {
// val is an object
if(val.x !== undefined) {
this.attrs[key].x = val.x;
}
if(val.y !== undefined) {
this.attrs[key].y = val.y;
}
}
}
};

View File

@ -1021,11 +1021,11 @@ Test.prototype.tests = {
test(darth.getCenterOffset().y === 30, 'center offset y should be 30');
var crop = darth.getCrop();
test(crop.x === 20, 'crop x should be 20');
test(crop.y === 20, 'crop y should be 20');
test(crop.width === 200, 'crop width should be 200');
test(crop.height === 250, 'crop height should be 250');
};
imageObj.src = '../darth-vader.jpg';
},