2012-03-07 13:45:48 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
// Rect
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
2012-07-09 12:56:52 +08:00
|
|
|
/**
|
|
|
|
* Rect constructor
|
|
|
|
* @constructor
|
|
|
|
* @augments Kinetic.Shape
|
|
|
|
* @param {Object} config
|
|
|
|
*/
|
2012-07-04 03:07:27 +08:00
|
|
|
Kinetic.Rect = Kinetic.Shape.extend({
|
|
|
|
init: function(config) {
|
|
|
|
this.setDefaultAttrs({
|
|
|
|
width: 0,
|
|
|
|
height: 0,
|
|
|
|
cornerRadius: 0
|
|
|
|
});
|
|
|
|
this.shapeType = "Rect";
|
2012-07-27 13:58:38 +08:00
|
|
|
config.drawFunc = this.drawFunc;
|
2012-07-04 03:07:27 +08:00
|
|
|
// call super constructor
|
|
|
|
this._super(config);
|
|
|
|
},
|
2012-07-27 13:58:38 +08:00
|
|
|
drawFunc: function(context) {
|
|
|
|
context.beginPath();
|
|
|
|
if(this.attrs.cornerRadius === 0) {
|
|
|
|
// simple rect - don't bother doing all that complicated maths stuff.
|
|
|
|
context.rect(0, 0, this.attrs.width, this.attrs.height);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// arcTo would be nicer, but browser support is patchy (Opera)
|
|
|
|
context.moveTo(this.attrs.cornerRadius, 0);
|
|
|
|
context.lineTo(this.attrs.width - this.attrs.cornerRadius, 0);
|
|
|
|
context.arc(this.attrs.width - this.attrs.cornerRadius, this.attrs.cornerRadius, this.attrs.cornerRadius, Math.PI * 3 / 2, 0, false);
|
|
|
|
context.lineTo(this.attrs.width, this.attrs.height - this.attrs.cornerRadius);
|
|
|
|
context.arc(this.attrs.width - this.attrs.cornerRadius, this.attrs.height - this.attrs.cornerRadius, this.attrs.cornerRadius, 0, Math.PI / 2, false);
|
|
|
|
context.lineTo(this.attrs.cornerRadius, this.attrs.height);
|
|
|
|
context.arc(this.attrs.cornerRadius, this.attrs.height - this.attrs.cornerRadius, this.attrs.cornerRadius, Math.PI / 2, Math.PI, false);
|
|
|
|
context.lineTo(0, this.attrs.cornerRadius);
|
|
|
|
context.arc(this.attrs.cornerRadius, this.attrs.cornerRadius, this.attrs.cornerRadius, Math.PI, Math.PI * 3 / 2, false);
|
|
|
|
}
|
|
|
|
context.closePath();
|
|
|
|
|
|
|
|
this.fill(context);
|
|
|
|
this.stroke(context);
|
|
|
|
},
|
2012-03-07 13:45:48 +08:00
|
|
|
/**
|
|
|
|
* set width and height
|
2012-07-09 12:56:52 +08:00
|
|
|
* @name setSize
|
|
|
|
* @methodOf Kinetic.Rect.prototype
|
2012-03-07 13:45:48 +08:00
|
|
|
*/
|
2012-05-27 07:37:37 +08:00
|
|
|
setSize: function() {
|
2012-07-04 13:08:59 +08:00
|
|
|
var size = Kinetic.Type._getSize(Array.prototype.slice.call(arguments));
|
2012-05-29 14:46:40 +08:00
|
|
|
this.setAttrs(size);
|
2012-03-31 15:08:50 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* return rect size
|
2012-07-09 12:56:52 +08:00
|
|
|
* @name getSize
|
|
|
|
* @methodOf Kinetic.Rect.prototype
|
2012-03-31 15:08:50 +08:00
|
|
|
*/
|
|
|
|
getSize: function() {
|
|
|
|
return {
|
2012-04-06 14:48:58 +08:00
|
|
|
width: this.attrs.width,
|
|
|
|
height: this.attrs.height
|
2012-03-31 15:08:50 +08:00
|
|
|
};
|
2012-06-10 15:02:16 +08:00
|
|
|
}
|
2012-07-04 03:07:27 +08:00
|
|
|
});
|
2012-06-11 04:07:09 +08:00
|
|
|
|
2012-07-04 03:07:27 +08:00
|
|
|
// add getters setters
|
|
|
|
Kinetic.Node.addGettersSetters(Kinetic.Rect, ['width', 'height', 'cornerRadius']);
|
2012-06-11 04:07:09 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* set width
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name setWidth
|
|
|
|
* @methodOf Kinetic.Rect.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
* @param {Number} width
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set height
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name setHeight
|
|
|
|
* @methodOf Kinetic.Rect.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
* @param {Number} height
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set corner radius
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name setCornerRadius
|
|
|
|
* @methodOf Kinetic.Rect.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
* @param {Number} radius
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get width
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name getWidth
|
|
|
|
* @methodOf Kinetic.Rect.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get height
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name getHeight
|
|
|
|
* @methodOf Kinetic.Rect.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get corner radius
|
2012-06-14 17:19:51 +08:00
|
|
|
* @name getCornerRadius
|
|
|
|
* @methodOf Kinetic.Rect.prototype
|
2012-06-11 04:07:09 +08:00
|
|
|
*/
|