mirror of
https://github.com/konvajs/konva.git
synced 2026-01-18 19:51:21 +08:00
added points Node getter and setter creators for Line and Polygon
This commit is contained in:
26
src/Node.js
26
src/Node.js
@@ -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) {
|
||||
|
||||
@@ -59,29 +59,22 @@
|
||||
}
|
||||
|
||||
canvas.stroke(this);
|
||||
},
|
||||
/**
|
||||
* set points array
|
||||
* @method
|
||||
* @memberof Kinetic.Line.prototype
|
||||
* @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);
|
||||
|
||||
Kinetic.Node.addPointsGetterSetter(Kinetic.Line, 'points');
|
||||
/**
|
||||
* set points array
|
||||
* @method
|
||||
* @memberof Kinetic.Line.prototype
|
||||
* @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]
|
||||
*/
|
||||
|
||||
/**
|
||||
* get points array
|
||||
* @method
|
||||
* @memberof Kinetic.Line.prototype
|
||||
*/
|
||||
})();
|
||||
|
||||
@@ -39,27 +39,22 @@
|
||||
}
|
||||
context.closePath();
|
||||
canvas.fillStroke(this);
|
||||
},
|
||||
/**
|
||||
* set points array
|
||||
* @method
|
||||
* @memberof Kinetic.Polygon.prototype
|
||||
* @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);
|
||||
|
||||
Kinetic.Node.addPointsGetterSetter(Kinetic.Polygon, 'points');
|
||||
/**
|
||||
* set points array
|
||||
* @method
|
||||
* @memberof Kinetic.Polygon.prototype
|
||||
* @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]
|
||||
*/
|
||||
|
||||
/**
|
||||
* get points array
|
||||
* @method
|
||||
* @memberof Kinetic.Polygon.prototype
|
||||
*/
|
||||
})();
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user