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