2012-12-01 12:04:10 -08:00
|
|
|
(function() {
|
|
|
|
|
/**
|
|
|
|
|
* Rect constructor
|
|
|
|
|
* @constructor
|
2013-05-16 00:28:49 -07:00
|
|
|
* @memberof Kinetic
|
2012-12-01 12:04:10 -08:00
|
|
|
* @augments Kinetic.Shape
|
|
|
|
|
* @param {Object} config
|
2013-01-02 23:55:56 -08:00
|
|
|
* @param {Number} [config.cornerRadius]
|
2013-06-01 10:27:44 -07:00
|
|
|
* @@shapeParams
|
|
|
|
|
* @@nodeParams
|
2013-05-17 15:09:57 -07:00
|
|
|
* @example
|
|
|
|
|
* var rect = new Kinetic.Rect({<br>
|
|
|
|
|
* width: 100,<br>
|
|
|
|
|
* height: 50,<br>
|
|
|
|
|
* fill: 'red',<br>
|
2014-02-25 08:30:11 +08:00
|
|
|
* stroke: 'black',<br>
|
2013-05-17 15:35:21 -07:00
|
|
|
* strokeWidth: 5<br>
|
2013-05-17 15:09:57 -07:00
|
|
|
* });
|
2012-12-01 12:04:10 -08:00
|
|
|
*/
|
|
|
|
|
Kinetic.Rect = function(config) {
|
2013-07-22 21:41:41 -07:00
|
|
|
this.___init(config);
|
2012-12-22 23:08:03 -08:00
|
|
|
};
|
2013-07-22 21:41:41 -07:00
|
|
|
|
2012-12-01 12:04:10 -08:00
|
|
|
Kinetic.Rect.prototype = {
|
2013-07-22 21:41:41 -07:00
|
|
|
___init: function(config) {
|
2012-12-01 12:04:10 -08:00
|
|
|
Kinetic.Shape.call(this, config);
|
2013-05-19 21:48:48 -07:00
|
|
|
this.className = 'Rect';
|
2014-01-04 23:34:01 -08:00
|
|
|
this.sceneFunc(this._sceneFunc);
|
2012-12-01 12:04:10 -08:00
|
|
|
},
|
2014-01-04 23:34:01 -08:00
|
|
|
_sceneFunc: function(context) {
|
2013-09-01 13:28:52 -07:00
|
|
|
var cornerRadius = this.getCornerRadius(),
|
2013-07-22 21:41:41 -07:00
|
|
|
width = this.getWidth(),
|
2013-03-15 08:33:05 -07:00
|
|
|
height = this.getHeight();
|
2013-07-22 21:41:41 -07:00
|
|
|
|
2013-11-27 10:13:34 -08:00
|
|
|
|
2013-09-01 13:28:52 -07:00
|
|
|
context.beginPath();
|
2013-07-22 21:41:41 -07:00
|
|
|
|
2013-03-15 08:33:05 -07:00
|
|
|
if(!cornerRadius) {
|
2012-12-01 12:04:10 -08:00
|
|
|
// simple rect - don't bother doing all that complicated maths stuff.
|
2013-09-01 01:13:52 -07:00
|
|
|
context.rect(0, 0, width, height);
|
2012-12-01 12:04:10 -08:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// arcTo would be nicer, but browser support is patchy (Opera)
|
2013-09-01 13:28:52 -07:00
|
|
|
context.moveTo(cornerRadius, 0);
|
|
|
|
|
context.lineTo(width - cornerRadius, 0);
|
|
|
|
|
context.arc(width - cornerRadius, cornerRadius, cornerRadius, Math.PI * 3 / 2, 0, false);
|
|
|
|
|
context.lineTo(width, height - cornerRadius);
|
|
|
|
|
context.arc(width - cornerRadius, height - cornerRadius, cornerRadius, 0, Math.PI / 2, false);
|
|
|
|
|
context.lineTo(cornerRadius, height);
|
|
|
|
|
context.arc(cornerRadius, height - cornerRadius, cornerRadius, Math.PI / 2, Math.PI, false);
|
|
|
|
|
context.lineTo(0, cornerRadius);
|
|
|
|
|
context.arc(cornerRadius, cornerRadius, cornerRadius, Math.PI, Math.PI * 3 / 2, false);
|
2012-12-01 12:04:10 -08:00
|
|
|
}
|
2013-09-01 13:28:52 -07:00
|
|
|
context.closePath();
|
2013-09-02 11:09:30 -07:00
|
|
|
context.fillStrokeShape(this);
|
2012-07-26 22:58:38 -07:00
|
|
|
}
|
2012-12-01 12:04:10 -08:00
|
|
|
};
|
|
|
|
|
|
2013-05-07 23:51:02 -07:00
|
|
|
Kinetic.Util.extend(Kinetic.Rect, Kinetic.Shape);
|
2012-06-10 13:07:09 -07:00
|
|
|
|
2013-08-10 21:11:34 -07:00
|
|
|
Kinetic.Factory.addGetterSetter(Kinetic.Rect, 'cornerRadius', 0);
|
2012-12-22 23:08:03 -08:00
|
|
|
/**
|
2014-01-07 00:43:31 -08:00
|
|
|
* get/set corner radius
|
|
|
|
|
* @name cornerRadius
|
2013-05-16 00:28:49 -07:00
|
|
|
* @method
|
|
|
|
|
* @memberof Kinetic.Rect.prototype
|
2014-01-07 00:43:31 -08:00
|
|
|
* @param {Number} cornerRadius
|
2013-09-16 22:05:28 -07:00
|
|
|
* @returns {Number}
|
2014-01-07 00:43:31 -08:00
|
|
|
* @example
|
|
|
|
|
* // get corner radius<br>
|
|
|
|
|
* var cornerRadius = rect.cornerRadius();<br><br>
|
|
|
|
|
*
|
|
|
|
|
* // set corner radius<br>
|
|
|
|
|
* rect.cornerRadius(10);
|
2012-12-22 23:08:03 -08:00
|
|
|
*/
|
2014-01-19 21:35:28 -08:00
|
|
|
|
2014-02-27 08:49:18 +08:00
|
|
|
Kinetic.Collection.mapMethods(Kinetic.Rect);
|
2012-12-01 12:04:10 -08:00
|
|
|
})();
|