mirror of
https://github.com/konvajs/konva.git
synced 2025-04-05 20:48:28 +08:00
111 lines
3.2 KiB
JavaScript
111 lines
3.2 KiB
JavaScript
(function() {
|
|
// the 0.0001 offset fixes a bug in Chrome 27
|
|
var PIx2 = (Math.PI * 2) - 0.0001;
|
|
|
|
/**
|
|
* Ring constructor
|
|
* @constructor
|
|
* @augments Kinetic.Shape
|
|
* @param {Object} config
|
|
* @param {Number} config.innerRadius
|
|
* @param {Number} config.outerRadius
|
|
* @param {Boolean} [config.clockwise]
|
|
* @@shapeParams
|
|
* @@nodeParams
|
|
* @example
|
|
* var ring = new Kinetic.Ring({
|
|
* innerRadius: 40,
|
|
* outerRadius: 80,
|
|
* fill: 'red',
|
|
* stroke: 'black',
|
|
* strokeWidth: 5
|
|
* });
|
|
*/
|
|
Kinetic.Ring = function(config) {
|
|
this.___init(config);
|
|
};
|
|
|
|
Kinetic.Ring.prototype = {
|
|
___init: function(config) {
|
|
// call super constructor
|
|
Kinetic.Shape.call(this, config);
|
|
this.className = 'Ring';
|
|
this.sceneFunc(this._sceneFunc);
|
|
},
|
|
_sceneFunc: function(context) {
|
|
context.beginPath();
|
|
context.arc(0, 0, this.getInnerRadius(), 0, PIx2, false);
|
|
context.moveTo(this.getOuterRadius(), 0);
|
|
context.arc(0, 0, this.getOuterRadius(), PIx2, 0, true);
|
|
context.closePath();
|
|
context.fillStrokeShape(this);
|
|
},
|
|
// implements Shape.prototype.getWidth()
|
|
getWidth: function() {
|
|
return this.getOuterRadius() * 2;
|
|
},
|
|
// implements Shape.prototype.getHeight()
|
|
getHeight: function() {
|
|
return this.getOuterRadius() * 2;
|
|
},
|
|
// implements Shape.prototype.setWidth()
|
|
setWidth: function(width) {
|
|
Kinetic.Node.prototype.setWidth.call(this, width);
|
|
if (this.outerRadius() !== width / 2) {
|
|
this.setOuterRadius(width / 2);
|
|
}
|
|
},
|
|
// implements Shape.prototype.setHeight()
|
|
setHeight: function(height) {
|
|
Kinetic.Node.prototype.setHeight.call(this, height);
|
|
if (this.outerRadius() !== height / 2) {
|
|
this.setOuterRadius(height / 2);
|
|
}
|
|
},
|
|
setOuterRadius : function(val) {
|
|
this._setAttr('outerRadius', val);
|
|
this.setWidth(val * 2);
|
|
this.setHeight(val * 2);
|
|
}
|
|
};
|
|
Kinetic.Util.extend(Kinetic.Ring, Kinetic.Shape);
|
|
|
|
// add getters setters
|
|
Kinetic.Factory.addGetterSetter(Kinetic.Ring, 'innerRadius', 0);
|
|
|
|
/**
|
|
* get/set innerRadius
|
|
* @name innerRadius
|
|
* @method
|
|
* @memberof Kinetic.Ring.prototype
|
|
* @param {Number} innerRadius
|
|
* @returns {Number}
|
|
* @example
|
|
* // get inner radius
|
|
* var innerRadius = ring.innerRadius();
|
|
*
|
|
* // set inner radius
|
|
* ring.innerRadius(20);
|
|
*/
|
|
|
|
Kinetic.Factory.addGetter(Kinetic.Ring, 'outerRadius', 0);
|
|
Kinetic.Factory.addOverloadedGetterSetter(Kinetic.Ring, 'outerRadius');
|
|
|
|
/**
|
|
* get/set outerRadius
|
|
* @name outerRadius
|
|
* @method
|
|
* @memberof Kinetic.Ring.prototype
|
|
* @param {Number} outerRadius
|
|
* @returns {Number}
|
|
* @example
|
|
* // get outer radius
|
|
* var outerRadius = ring.outerRadius();
|
|
*
|
|
* // set outer radius
|
|
* ring.outerRadius(20);
|
|
*/
|
|
|
|
Kinetic.Collection.mapMethods(Kinetic.Ring);
|
|
})();
|