added _setPoints so that points setter methods could leverage the same logic that was in the setAttrs() method

This commit is contained in:
Eric Rowell
2012-05-13 11:58:40 -07:00
parent 7f8a174b51
commit ec415c55de
7 changed files with 98 additions and 56 deletions

70
dist/kinetic-core.js vendored
View File

@@ -165,7 +165,7 @@ Kinetic.GlobalObject = {
/* /*
* takes the arguments passed into a function and * takes the arguments passed into a function and
* creates a point object from it. The arguments * creates a point object from it. The arguments
* can be an obect or an array * can be a point object or an array of two elements
*/ */
_getXY: function(arg) { _getXY: function(arg) {
if(arg.length === 1) { if(arg.length === 1) {
@@ -178,6 +178,10 @@ Kinetic.GlobalObject = {
} }
} }
}, },
/*
* val will be either a point object or an
* array with two elements
*/
_setXY: function(obj, key, val) { _setXY: function(obj, key, val) {
if(obj[key] === undefined) { if(obj[key] === undefined) {
obj[key] = {}; obj[key] = {};
@@ -199,6 +203,11 @@ Kinetic.GlobalObject = {
} }
} }
}, },
/*
* 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
*/
_setSize: function(obj, key, val) { _setSize: function(obj, key, val) {
if(obj[key] === undefined) { if(obj[key] === undefined) {
obj[key] = {}; obj[key] = {};
@@ -219,6 +228,33 @@ Kinetic.GlobalObject = {
obj[key].height = val.height; obj[key].height = val.height;
} }
} }
},
/*
* 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;
}
} }
}; };
@@ -418,27 +454,7 @@ Kinetic.Node.prototype = {
go._setXY(this.attrs, key, val); go._setXY(this.attrs, key, val);
break; break;
case 'points': case 'points':
/* go._setPoints(this.attrs, key, val);
* if points contains an array of objects, just set
* the attr normally
*/
if(Kinetic.GlobalObject._isObject(val[0])) {
this.attrs[key] = config[key];
}
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]
});
}
this.attrs[key] = arr;
}
break; break;
case 'crop': case 'crop':
go._setXY(this.attrs, key, val); go._setXY(this.attrs, key, val);
@@ -3253,10 +3269,11 @@ Kinetic.Polygon = function(config) {
Kinetic.Polygon.prototype = { Kinetic.Polygon.prototype = {
/** /**
* set points array * set points array
* @param {Array} points * @param {Array} can be an array of point objects or an array
* of Numbers. e.g. [{x:1,y:2},{x:3,y:4}] == [1,2,3,4]
*/ */
setPoints: function(points) { setPoints: function(points) {
this.attrs.points = points; Kinetic.GlobalObject._setPoints(this.attrs, 'points', points);
}, },
/** /**
* get points array * get points array
@@ -3752,10 +3769,11 @@ Kinetic.Line = function(config) {
Kinetic.Line.prototype = { Kinetic.Line.prototype = {
/** /**
* set points array * set points array
* @param {Array} points * @param {Array} can be an array of point objects or an array
* of Numbers. e.g. [{x:1,y:2},{x:3,y:4}] == [1,2,3,4]
*/ */
setPoints: function(points) { setPoints: function(points) {
this.attrs.points = points; Kinetic.GlobalObject._setPoints(this.attrs, 'points', points);
}, },
/** /**
* get points array * get points array

File diff suppressed because one or more lines are too long

View File

@@ -137,7 +137,7 @@ Kinetic.GlobalObject = {
/* /*
* takes the arguments passed into a function and * takes the arguments passed into a function and
* creates a point object from it. The arguments * creates a point object from it. The arguments
* can be an obect or an array * can be a point object or an array of two elements
*/ */
_getXY: function(arg) { _getXY: function(arg) {
if(arg.length === 1) { if(arg.length === 1) {
@@ -150,6 +150,10 @@ Kinetic.GlobalObject = {
} }
} }
}, },
/*
* val will be either a point object or an
* array with two elements
*/
_setXY: function(obj, key, val) { _setXY: function(obj, key, val) {
if(obj[key] === undefined) { if(obj[key] === undefined) {
obj[key] = {}; obj[key] = {};
@@ -171,6 +175,11 @@ Kinetic.GlobalObject = {
} }
} }
}, },
/*
* 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
*/
_setSize: function(obj, key, val) { _setSize: function(obj, key, val) {
if(obj[key] === undefined) { if(obj[key] === undefined) {
obj[key] = {}; obj[key] = {};
@@ -191,6 +200,33 @@ Kinetic.GlobalObject = {
obj[key].height = val.height; obj[key].height = val.height;
} }
} }
},
/*
* 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;
}
} }
}; };

View File

@@ -187,27 +187,7 @@ Kinetic.Node.prototype = {
go._setXY(this.attrs, key, val); go._setXY(this.attrs, key, val);
break; break;
case 'points': case 'points':
/* go._setPoints(this.attrs, key, val);
* if points contains an array of objects, just set
* the attr normally
*/
if(Kinetic.GlobalObject._isObject(val[0])) {
this.attrs[key] = config[key];
}
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]
});
}
this.attrs[key] = arr;
}
break; break;
case 'crop': case 'crop':
go._setXY(this.attrs, key, val); go._setXY(this.attrs, key, val);

View File

@@ -52,10 +52,11 @@ Kinetic.Line = function(config) {
Kinetic.Line.prototype = { Kinetic.Line.prototype = {
/** /**
* set points array * set points array
* @param {Array} points * @param {Array} can be an array of point objects or an array
* of Numbers. e.g. [{x:1,y:2},{x:3,y:4}] == [1,2,3,4]
*/ */
setPoints: function(points) { setPoints: function(points) {
this.attrs.points = points; Kinetic.GlobalObject._setPoints(this.attrs, 'points', points);
}, },
/** /**
* get points array * get points array

View File

@@ -32,10 +32,11 @@ Kinetic.Polygon = function(config) {
Kinetic.Polygon.prototype = { Kinetic.Polygon.prototype = {
/** /**
* set points array * set points array
* @param {Array} points * @param {Array} can be an array of point objects or an array
* of Numbers. e.g. [{x:1,y:2},{x:3,y:4}] == [1,2,3,4]
*/ */
setPoints: function(points) { setPoints: function(points) {
this.attrs.points = points; Kinetic.GlobalObject._setPoints(this.attrs, 'points', points);
}, },
/** /**
* get points array * get points array

View File

@@ -1271,6 +1271,12 @@ Test.prototype.tests = {
line.on('dragend', function() { line.on('dragend', function() {
line.saveData(); line.saveData();
}); });
line.setPoints([1, 2, 3, 4]);
test(line.getPoints()[0].x === 1, 'first point x should be 1');
line.setPoints([73, 160, 340, 23]);
test(line.getPoints()[0].x === 73, 'first point x should be 73');
}, },
'SHAPES - add dashed line': function(containerId) { 'SHAPES - add dashed line': function(containerId) {
var stage = new Kinetic.Stage({ var stage = new Kinetic.Stage({