2012-03-07 13:45:48 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
// Rect
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
/**
|
|
|
|
* Rect constructor
|
|
|
|
* @constructor
|
|
|
|
* @augments Kinetic.Shape
|
|
|
|
* @param {Object} config
|
|
|
|
*/
|
|
|
|
Kinetic.Rect = function(config) {
|
2012-04-06 14:48:58 +08:00
|
|
|
// default attrs
|
|
|
|
if(this.attrs === undefined) {
|
|
|
|
this.attrs = {};
|
|
|
|
}
|
|
|
|
this.attrs.width = 0;
|
|
|
|
this.attrs.height = 0;
|
2012-04-20 06:27:04 +08:00
|
|
|
this.attrs.cornerRadius = 0;
|
2012-04-06 14:48:58 +08:00
|
|
|
|
|
|
|
this.shapeType = "Rect";
|
|
|
|
|
2012-03-07 13:45:48 +08:00
|
|
|
config.drawFunc = function() {
|
|
|
|
var context = this.getContext();
|
|
|
|
context.beginPath();
|
2012-03-31 14:57:10 +08:00
|
|
|
this.applyLineJoin();
|
2012-04-20 06:27:04 +08:00
|
|
|
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);
|
|
|
|
}
|
2012-03-07 13:45:48 +08:00
|
|
|
context.closePath();
|
|
|
|
this.fillStroke();
|
|
|
|
};
|
|
|
|
// call super constructor
|
|
|
|
Kinetic.Shape.apply(this, [config]);
|
|
|
|
};
|
|
|
|
/*
|
|
|
|
* Rect methods
|
|
|
|
*/
|
|
|
|
Kinetic.Rect.prototype = {
|
|
|
|
/**
|
|
|
|
* set width
|
|
|
|
* @param {Number} width
|
|
|
|
*/
|
|
|
|
setWidth: function(width) {
|
2012-04-06 14:48:58 +08:00
|
|
|
this.attrs.width = width;
|
2012-03-07 13:45:48 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* get width
|
|
|
|
*/
|
|
|
|
getWidth: function() {
|
2012-04-06 14:48:58 +08:00
|
|
|
return this.attrs.width;
|
2012-03-07 13:45:48 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* set height
|
|
|
|
* @param {Number} height
|
|
|
|
*/
|
|
|
|
setHeight: function(height) {
|
2012-04-06 14:48:58 +08:00
|
|
|
this.attrs.height = height;
|
2012-03-07 13:45:48 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* get height
|
|
|
|
*/
|
|
|
|
getHeight: function() {
|
2012-04-06 14:48:58 +08:00
|
|
|
return this.attrs.height;
|
2012-03-07 13:45:48 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* set width and height
|
|
|
|
* @param {Number} width
|
|
|
|
* @param {Number} height
|
|
|
|
*/
|
|
|
|
setSize: function(width, height) {
|
2012-04-06 14:48:58 +08:00
|
|
|
this.attrs.width = width;
|
|
|
|
this.attrs.height = height;
|
2012-03-31 15:08:50 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* return rect size
|
|
|
|
*/
|
|
|
|
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-04-20 06:27:04 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* set corner radius
|
|
|
|
* @param {Number} radius
|
|
|
|
*/
|
|
|
|
setCornerRadius: function(radius) {
|
|
|
|
this.attrs.cornerRadius = radius;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* get corner radius
|
|
|
|
*/
|
|
|
|
getCornerRadius: function() {
|
|
|
|
return this.attrs.cornerRadius;
|
|
|
|
},
|
2012-03-07 13:45:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// extend Shape
|
2012-03-12 14:01:23 +08:00
|
|
|
Kinetic.GlobalObject.extend(Kinetic.Rect, Kinetic.Shape);
|