mirror of
https://github.com/konvajs/konva.git
synced 2025-06-27 21:54:37 +08:00
82 lines
2.0 KiB
JavaScript
82 lines
2.0 KiB
JavaScript
///////////////////////////////////////////////////////////////////////
|
|
// Star
|
|
///////////////////////////////////////////////////////////////////////
|
|
/**
|
|
* Star constructor
|
|
* @constructor
|
|
* @augments Kinetic.Shape
|
|
* @param {Object} config
|
|
*/
|
|
Kinetic.Star = function(config) {
|
|
this.setDefaultAttrs({
|
|
numPoints: 0,
|
|
innerRadius: 0,
|
|
outerRadius: 0
|
|
});
|
|
|
|
this.shapeType = "Star";
|
|
config.drawFunc = function() {
|
|
var context = this.getContext();
|
|
context.beginPath();
|
|
this.applyLineJoin();
|
|
context.moveTo(0, 0 - this.attrs.outerRadius);
|
|
|
|
for(var n = 1; n < this.attrs.numPoints * 2; n++) {
|
|
var radius = n % 2 === 0 ? this.attrs.outerRadius : this.attrs.innerRadius;
|
|
var x = radius * Math.sin(n * Math.PI / this.attrs.numPoints);
|
|
var y = -1 * radius * Math.cos(n * Math.PI / this.attrs.numPoints);
|
|
context.lineTo(x, y);
|
|
}
|
|
context.closePath();
|
|
this.fillStroke();
|
|
};
|
|
// call super constructor
|
|
Kinetic.Shape.apply(this, [config]);
|
|
};
|
|
/*
|
|
* Star methods
|
|
*/
|
|
Kinetic.Star.prototype = {
|
|
/**
|
|
* set number of points
|
|
* @param {Integer} points
|
|
*/
|
|
setNumPoints: function(numPoints) {
|
|
this.attrs.numPoints = numPoints;
|
|
},
|
|
/**
|
|
* get number of points
|
|
*/
|
|
getNumPoints: function() {
|
|
return this.attrs.numPoints;
|
|
},
|
|
/**
|
|
* set outer radius
|
|
* @param {Number} radius
|
|
*/
|
|
setOuterRadius: function(radius) {
|
|
this.attrs.outerRadius = radius;
|
|
},
|
|
/**
|
|
* get outer radius
|
|
*/
|
|
getOuterRadius: function() {
|
|
return this.attrs.outerRadius;
|
|
},
|
|
/**
|
|
* set inner radius
|
|
* @param {Number} radius
|
|
*/
|
|
setInnerRadius: function(radius) {
|
|
this.attrs.innerRadius = radius;
|
|
},
|
|
/**
|
|
* get inner radius
|
|
*/
|
|
getInnerRadius: function() {
|
|
return this.attrs.innerRadius;
|
|
}
|
|
};
|
|
// extend Shape
|
|
Kinetic.GlobalObject.extend(Kinetic.Star, Kinetic.Shape);
|