konva/src/shapes/Rect.js

105 lines
3.1 KiB
JavaScript
Raw Normal View History

///////////////////////////////////////////////////////////////////////
// Rect
///////////////////////////////////////////////////////////////////////
Kinetic.Rect = Kinetic.Shape.extend({
/**
* Rect constructor
* @constructor
* @augments Kinetic.Shape
* @param {Object} config
*/
init: function(config) {
this.setDefaultAttrs({
width: 0,
height: 0,
cornerRadius: 0
});
this.shapeType = "Rect";
config.drawFunc = function() {
var context = this.getContext();
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();
this.stroke();
};
// call super constructor
this._super(config);
},
/**
* set width and height
*/
setSize: function() {
var size = Kinetic.GlobalObject._getSize(Array.prototype.slice.call(arguments));
this.setAttrs(size);
},
/**
* return rect size
*/
getSize: function() {
return {
width: this.attrs.width,
height: this.attrs.height
};
}
});
// add getters setters
Kinetic.Node.addGettersSetters(Kinetic.Rect, ['width', 'height', 'cornerRadius']);
/**
* set width
2012-06-14 17:19:51 +08:00
* @name setWidth
* @methodOf Kinetic.Rect.prototype
* @param {Number} width
*/
/**
* set height
2012-06-14 17:19:51 +08:00
* @name setHeight
* @methodOf Kinetic.Rect.prototype
* @param {Number} height
*/
/**
* set corner radius
2012-06-14 17:19:51 +08:00
* @name setCornerRadius
* @methodOf Kinetic.Rect.prototype
* @param {Number} radius
*/
/**
* get width
2012-06-14 17:19:51 +08:00
* @name getWidth
* @methodOf Kinetic.Rect.prototype
*/
/**
* get height
2012-06-14 17:19:51 +08:00
* @name getHeight
* @methodOf Kinetic.Rect.prototype
*/
/**
* get corner radius
2012-06-14 17:19:51 +08:00
* @name getCornerRadius
* @methodOf Kinetic.Rect.prototype
*/