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

72
dist/kinetic-core.js vendored
View File

@ -163,9 +163,9 @@ Kinetic.GlobalObject = {
return obj === Object(obj);
},
/*
* takes the arguments passed into a function and
* takes the arguments passed into a function and
* 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) {
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) {
if(obj[key] === undefined) {
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) {
if(obj[key] === undefined) {
obj[key] = {};
@ -219,6 +228,33 @@ Kinetic.GlobalObject = {
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);
break;
case 'points':
/*
* 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;
}
go._setPoints(this.attrs, key, val);
break;
case 'crop':
go._setXY(this.attrs, key, val);
@ -3253,10 +3269,11 @@ Kinetic.Polygon = function(config) {
Kinetic.Polygon.prototype = {
/**
* 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) {
this.attrs.points = points;
Kinetic.GlobalObject._setPoints(this.attrs, 'points', points);
},
/**
* get points array
@ -3752,10 +3769,11 @@ Kinetic.Line = function(config) {
Kinetic.Line.prototype = {
/**
* 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) {
this.attrs.points = points;
Kinetic.GlobalObject._setPoints(this.attrs, 'points', points);
},
/**
* get points array

File diff suppressed because one or more lines are too long

View File

@ -135,9 +135,9 @@ Kinetic.GlobalObject = {
return obj === Object(obj);
},
/*
* takes the arguments passed into a function and
* takes the arguments passed into a function and
* 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) {
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) {
if(obj[key] === undefined) {
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) {
if(obj[key] === undefined) {
obj[key] = {};
@ -191,6 +200,33 @@ Kinetic.GlobalObject = {
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);
break;
case 'points':
/*
* 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;
}
go._setPoints(this.attrs, key, val);
break;
case 'crop':
go._setXY(this.attrs, key, val);

View File

@ -52,10 +52,11 @@ Kinetic.Line = function(config) {
Kinetic.Line.prototype = {
/**
* 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) {
this.attrs.points = points;
Kinetic.GlobalObject._setPoints(this.attrs, 'points', points);
},
/**
* get points array

View File

@ -32,10 +32,11 @@ Kinetic.Polygon = function(config) {
Kinetic.Polygon.prototype = {
/**
* 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) {
this.attrs.points = points;
Kinetic.GlobalObject._setPoints(this.attrs, 'points', points);
},
/**
* get points array

View File

@ -1271,6 +1271,12 @@ Test.prototype.tests = {
line.on('dragend', function() {
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) {
var stage = new Kinetic.Stage({