added points Node getter and setter creators for Line and Polygon

This commit is contained in:
Eric Rowell
2013-06-08 11:11:49 -07:00
parent 0c3ada88f5
commit e8ea9340fe
5 changed files with 55 additions and 51 deletions

View File

@@ -1179,6 +1179,10 @@
this.addSetter(constructor, attr + UPPER_X, isTransform);
this.addSetter(constructor, attr + UPPER_Y, isTransform);
};
Kinetic.Node.addPointsGetterSetter = function(constructor, attr) {
this.addPointsGetter(constructor, attr);
this.addPointsSetter(constructor, attr);
};
Kinetic.Node.addRotationGetterSetter = function(constructor, attr, def, isTransform) {
this.addRotationGetter(constructor, attr, def);
this.addRotationSetter(constructor, attr, isTransform);
@@ -1232,6 +1236,22 @@
this[prefix + RGB](obj);
};
};
Kinetic.Node.addPointsSetter = function(constructor, attr) {
var method = SET + Kinetic.Util._capitalize(attr);
constructor.prototype[method] = function(val) {
var points = Kinetic.Util._getPoints(val);
this._setAttr('points', points);
};
};
Kinetic.Node.addPointsGetter = function(constructor, attr) {
var that = this,
method = GET + Kinetic.Util._capitalize(attr);
constructor.prototype[method] = function(arg) {
var val = this.attrs[attr];
return val === undefined ? [] : val;
};
};
Kinetic.Node.addSetter = function(constructor, attr, isTransform) {
var that = this,
method = SET + Kinetic.Util._capitalize(attr);
@@ -1293,11 +1313,7 @@
constructor.prototype[method] = function(arg) {
var val = this.attrs[attr];
if (val === undefined) {
val = def;
}
return val;
return val === undefined ? def : val;
};
};
Kinetic.Node.addPointGetter = function(constructor, attr) {

View File

@@ -59,7 +59,11 @@
}
canvas.stroke(this);
},
}
};
Kinetic.Util.extend(Kinetic.Line, Kinetic.Shape);
Kinetic.Node.addPointsGetterSetter(Kinetic.Line, 'points');
/**
* set points array
* @method
@@ -67,21 +71,10 @@
* @param {Array} can be an array of point objects or an array
* of Numbers. e.g. [{x:1,y:2},{x:3,y:4}] or [1,2,3,4]
*/
setPoints: function(val) {
var points = Kinetic.Util._getPoints(val);
this._setAttr('points', points);
this._points = points;
},
/**
* get points array
* @method
* @memberof Kinetic.Line.prototype
*/
// NOTE: cannot use getter method because we need to return a new
// default array literal each time because arrays are modified by reference
getPoints: function() {
return this.attrs.points || [];
}
};
Kinetic.Util.extend(Kinetic.Line, Kinetic.Shape);
})();

View File

@@ -39,7 +39,11 @@
}
context.closePath();
canvas.fillStroke(this);
},
}
};
Kinetic.Util.extend(Kinetic.Polygon, Kinetic.Shape);
Kinetic.Node.addPointsGetterSetter(Kinetic.Polygon, 'points');
/**
* set points array
* @method
@@ -47,19 +51,10 @@
* @param {Array} can be an array of point objects or an array
* of Numbers. e.g. [{x:1,y:2},{x:3,y:4}] or [1,2,3,4]
*/
setPoints: function(val) {
this._setAttr('points', Kinetic.Util._getPoints(val));
},
/**
* get points array
* @method
* @memberof Kinetic.Polygon.prototype
*/
// NOTE: cannot use getter method because we need to return a new
// default array literal each time because arrays are modified by reference
getPoints: function() {
return this.attrs.points || [];
}
};
Kinetic.Util.extend(Kinetic.Polygon, Kinetic.Shape);
})();

View File

@@ -1,5 +1,5 @@
Test.Modules.LINE = {
'add line': function(containerId) {
'*add line': function(containerId) {
var stage = new Kinetic.Stage({
container: containerId,
width: 578,
@@ -84,7 +84,7 @@ Test.Modules.LINE = {
test(redLine.getPoints()[0].x === 4, 'redLine points is wrong');
},
'add dashed line': function(containerId) {
'*add dashed line': function(containerId) {
var stage = new Kinetic.Stage({
container: containerId,
width: 578,

View File

@@ -1,5 +1,5 @@
Test.Modules.POLYGON - {
'add polygon': function(containerId) {
Test.Modules.POLYGON = {
'*add polygon': function(containerId) {
var stage = new Kinetic.Stage({
container: containerId,
width: 578,