mirror of
https://github.com/konvajs/konva.git
synced 2025-11-18 17:21:36 +08:00
53 lines
1.5 KiB
JavaScript
53 lines
1.5 KiB
JavaScript
///////////////////////////////////////////////////////////////////////
|
|
// Polygon
|
|
///////////////////////////////////////////////////////////////////////
|
|
/**
|
|
* Polygon constructor. Polygons are defined by an array of points
|
|
* @constructor
|
|
* @augments Kinetic.Shape
|
|
* @param {Object} config
|
|
*/
|
|
Kinetic.Polygon = function(config) {
|
|
this._initPolygon(config);
|
|
};
|
|
|
|
Kinetic.Polygon.prototype = {
|
|
_initPolygon: function(config) {
|
|
this.setDefaultAttrs({
|
|
points: []
|
|
});
|
|
|
|
this.shapeType = "Polygon";
|
|
config.drawFunc = this.drawFunc;
|
|
// call super constructor
|
|
Kinetic.Shape.call(this, config);
|
|
},
|
|
drawFunc: function(context) {
|
|
context.beginPath();
|
|
context.moveTo(this.attrs.points[0].x, this.attrs.points[0].y);
|
|
for(var n = 1; n < this.attrs.points.length; n++) {
|
|
context.lineTo(this.attrs.points[n].x, this.attrs.points[n].y);
|
|
}
|
|
context.closePath();
|
|
this.fill(context);
|
|
this.stroke(context);
|
|
}
|
|
};
|
|
Kinetic.Global.extend(Kinetic.Polygon, Kinetic.Shape);
|
|
|
|
// add getters setters
|
|
Kinetic.Node.addGettersSetters(Kinetic.Polygon, ['points']);
|
|
|
|
/**
|
|
* set points array
|
|
* @name setPoints
|
|
* @methodOf Kinetic.Polygon.prototype
|
|
* @param {Array} points can be an array of point objects or an array
|
|
* of Numbers. e.g. [{x:1,y:2},{x:3,y:4}] or [1,2,3,4]
|
|
*/
|
|
|
|
/**
|
|
* get points array
|
|
* @name getPoints
|
|
* @methodOf Kinetic.Polygon.prototype
|
|
*/ |