1
0
mirror of https://github.com/konvajs/konva.git synced 2025-04-05 20:48:28 +08:00

added support for Factor getter setter validators

This commit is contained in:
Eric Rowell 2014-01-10 01:29:41 -08:00
parent 4b367ae2c8
commit 9e574fd241
15 changed files with 166 additions and 41 deletions

View File

@ -42,9 +42,9 @@
Y = 'y';
Kinetic.Factory = {
addGetterSetter: function(constructor, attr, def, afterFunc) {
addGetterSetter: function(constructor, attr, def, validator) {
this.addGetter(constructor, attr, def);
this.addSetter(constructor, attr, afterFunc);
this.addSetter(constructor, attr, validator);
this.addOverloadedGetterSetter(constructor, attr);
},
addGetter: function(constructor, attr, def) {
@ -56,18 +56,19 @@
return val === undefined ? def : val;
};
},
addSetter: function(constructor, attr, afterFunc) {
addSetter: function(constructor, attr, validator) {
var method = SET + Kinetic.Util._capitalize(attr);
constructor.prototype[method] = function(val) {
this._setAttr(attr, val);
if (afterFunc) {
afterFunc.call(this);
if (validator) {
val = validator.call(this, val);
}
this._setAttr(attr, val);
return this;
};
},
addComponentsGetterSetter: function(constructor, attr, components, afterFunc) {
addComponentsGetterSetter: function(constructor, attr, components, validator) {
var len = components.length,
capitalize = Kinetic.Util._capitalize,
getter = GET + capitalize(attr),
@ -91,12 +92,12 @@
var oldVal = this.attrs[attr],
key;
for (key in val) {
this._setAttr(attr + capitalize(key), val[key]);
if (validator) {
val = validator.call(this, val);
}
if (afterFunc) {
afterFunc.call(this);
for (key in val) {
this._setAttr(attr + capitalize(key), val[key]);
}
this._fireChangeEvent(attr, oldVal, val);

View File

@ -297,7 +297,17 @@
* shape.stroke('rgba(0,255,0,0.5');
*/
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'strokeRed', 0);
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'strokeRed', 0, function(val) {
if (val > 255) {
return 255;
}
else if (val < 0) {
return 0;
}
else {
return Math.round(val);
}
});
/**
* get/set stroke red component
@ -314,7 +324,17 @@
* shape.strokeRed(0);
*/
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'strokeGreen', 0);
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'strokeGreen', 0, function(val) {
if (val > 255) {
return 255;
}
else if (val < 0) {
return 0;
}
else {
return Math.round(val);
}
});
/**
* get/set stroke green component
@ -331,7 +351,17 @@
* shape.strokeGreen(255);
*/
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'strokeBlue', 0);
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'strokeBlue', 0, function(val) {
if (val > 255) {
return 255;
}
else if (val < 0) {
return 0;
}
else {
return Math.round(val);
}
});
/**
* get/set stroke blue component
@ -490,7 +520,17 @@
* shape.shadowColor('rgba(0,255,0,0.5');
*/
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'shadowRed', 0);
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'shadowRed', 0, function(val) {
if (val > 255) {
return 255;
}
else if (val < 0) {
return 0;
}
else {
return Math.round(val);
}
});
/**
* get/set shadow red component
@ -507,7 +547,17 @@
* shape.shadowRed(0);
*/
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'shadowGreen', 0);
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'shadowGreen', 0, function(val) {
if (val > 255) {
return 255;
}
else if (val < 0) {
return 0;
}
else {
return Math.round(val);
}
});
/**
* get/set shadow green component
@ -524,7 +574,17 @@
* shape.shadowGreen(255);
*/
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'shadowBlue', 0);
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'shadowBlue', 0, function(val) {
if (val > 255) {
return 255;
}
else if (val < 0) {
return 0;
}
else {
return Math.round(val);
}
});
/**
* get/set shadow blue component
@ -678,7 +738,17 @@
* shape.fill('rgba(0,255,0,0.5');
*/
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'fillRed', 0);
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'fillRed', 0, function(val) {
if (val > 255) {
return 255;
}
else if (val < 0) {
return 0;
}
else {
return Math.round(val);
}
});
/**
* get/set fill red component
@ -695,7 +765,17 @@
* shape.fillRed(0);
*/
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'fillGreen', 0);
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'fillGreen', 0, function(val) {
if (val > 255) {
return 255;
}
else if (val < 0) {
return 0;
}
else {
return Math.round(val);
}
});
/**
* get/set fill green component
@ -712,7 +792,17 @@
* shape.fillGreen(255);
*/
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'fillBlue', 0);
Kinetic.Factory.addGetterSetter(Kinetic.Shape, 'fillBlue', 0, function(val) {
if (val > 255) {
return 255;
}
else if (val < 0) {
return 0;
}
else {
return Math.round(val);
}
});
/**
* get/set fill blue component

View File

@ -337,7 +337,7 @@
}
};
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'blurRadius', 0, function() {this._filterUpToDate = false;});
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'blurRadius', 0, function(val) {this._filterUpToDate = false;return val;});
/**
* get/set blur radius

View File

@ -21,7 +21,7 @@
}
};
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'brightness', 0, function() {this._filterUpToDate = false;});
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'brightness', 0, function(val) {this._filterUpToDate = false;return val;});
/**
* get/set filter brightness. The brightness is a number between -1 and 1.&nbsp; Positive values
* brighten the pixels and negative values darken them.

View File

@ -123,7 +123,7 @@
} while (--y);
};
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'embossStrength', 0.5, function() {this._filterUpToDate = false;});
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'embossStrength', 0.5, function(val) {this._filterUpToDate = false;return val;});
/**
* get/set emboss strength
* @name embossStrength
@ -133,7 +133,7 @@
* @returns {Number}
*/
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'embossWhiteLevel', 0.5, function() {this._filterUpToDate = false;});
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'embossWhiteLevel', 0.5, function(val) {this._filterUpToDate = false;return val;});
/**
* get/set emboss white level
* @name embossWhiteLevel
@ -143,7 +143,7 @@
* @returns {Number}
*/
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'embossDirection', 'top-left', function() {this._filterUpToDate = false;});
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'embossDirection', 'top-left', function(val) {this._filterUpToDate = false;return val;});
/**
* get/set emboss direction
* @name embossDirection
@ -154,7 +154,7 @@
* @returns {String}
*/
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'embossBlend', false, function() {this._filterUpToDate = false;});
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'embossBlend', false, function(val) {this._filterUpToDate = false;return val;});
/**
* get/set emboss blend
* @name embossBlend

View File

@ -105,7 +105,7 @@
}
};
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'enhance', 0, function() {this._filterUpToDate = false;});
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'enhance', 0, function(val) {this._filterUpToDate = false;return val;});
/**
* get/set enhance

View File

@ -57,7 +57,7 @@
};
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'hue', 0, function() {this._filterUpToDate = false;});
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'hue', 0, function(val) {this._filterUpToDate = false;return val;});
/**
* get/set hsv hue in degrees
* @name hue
@ -67,7 +67,7 @@
* @returns {Number}
*/
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'saturation', 1, function() {this._filterUpToDate = false;});
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'saturation', 1, function(val) {this._filterUpToDate = false;return val;});
/**
* get/set hsv saturation
* @name saturation
@ -77,7 +77,7 @@
* @returns {Number}
*/
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'value', 1, function() {this._filterUpToDate = false;});
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'value', 1, function(val) {this._filterUpToDate = false;return val;});
/**
* get/set hsv value
* @name value

View File

@ -310,8 +310,8 @@
FromPolar(scratchData,imageData,{polarRotation:0});
};
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'kaleidoscopePower', 2, function() {this._filterUpToDate = false;});
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'kaleidoscopeAngle', 0, function() {this._filterUpToDate = false;});
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'kaleidoscopePower', 2, function(val) {this._filterUpToDate = false;return val;});
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'kaleidoscopeAngle', 0, function(val) {this._filterUpToDate = false;return val;});
/**
* get/set kaleidoscope power

View File

@ -188,5 +188,5 @@
return imageData;
};
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'threshold', 0);
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'threshold', 0, function(val) {this._filterUpToDate = false;return val;});
})();

View File

@ -21,7 +21,7 @@
}
};
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'noise', 0.2, function() {this._filterUpToDate = false;});
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'noise', 0.2, function(val) {this._filterUpToDate = false;return val;});
/**
* get/set noise amount. Must be a value between 0 and 1

View File

@ -76,7 +76,7 @@
};
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'pixelSize', 8, function() {this._filterUpToDate = false;});
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'pixelSize', 8, function(val) {this._filterUpToDate = false;return val;});
/**
* get/set pixel size

View File

@ -23,7 +23,7 @@
}
};
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'levels', 0.5, function() {this._filterUpToDate = false;});
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'levels', 0.5, function(val) {this._filterUpToDate = false;return val;});
/**
* get/set levels. Must be a number between 0 and 1

View File

@ -23,7 +23,18 @@
}
};
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'red', 255, function() {this._filterUpToDate = false;});
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'red', 0, function(val) {
this._filterUpToDate = false;
if (val > 255) {
return 255;
}
else if (val < 0) {
return 0;
}
else {
return Math.round(val);
}
});
/**
* get/set filter red value
* @name red
@ -33,7 +44,18 @@
* @returns {Integer}
*/
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'green', 0, function() {this._filterUpToDate = false;});
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'green', 0, function(val) {
this._filterUpToDate = false;
if (val > 255) {
return 255;
}
else if (val < 0) {
return 0;
}
else {
return Math.round(val);
}
});
/**
* get/set filter green value
* @name green
@ -43,7 +65,18 @@
* @returns {Integer}
*/
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'blue', 0, function() {this._filterUpToDate = false;});
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'blue', 0, function(val) {
this._filterUpToDate = false;
if (val > 255) {
return 255;
}
else if (val < 0) {
return 0;
}
else {
return Math.round(val);
}
});
/**
* get/set filter blue value
* @name blue

View File

@ -21,7 +21,7 @@
}
};
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'threshold', 0.5, function() {this._filterUpToDate = false;});
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'threshold', 0.5, function(val) {this._filterUpToDate = false;return val;});
/**
* get/set threshold. Must be a value between 0 and 1

View File

@ -433,7 +433,8 @@
* text.wrap('word');
*/
Kinetic.Factory.addGetter(Kinetic.Text, TEXT, EMPTY_STRING);
Kinetic.Factory.addGetter(Kinetic.Text, 'text', EMPTY_STRING);
Kinetic.Factory.addOverloadedGetterSetter(Kinetic.Text, 'text');
/**
* get/set text