1 //= require "object.class"
  2 
  3 (function() {
  4   
  5   var fabric = this.fabric || (this.fabric = { });
  6   
  7   if (fabric.Polyline) {
  8     fabric.warn('fabric.Polyline is already defined');
  9     return;
 10   }
 11   
 12   /** 
 13    * @class Polyline
 14    * @extends fabric.Object
 15    */
 16   fabric.Polyline = fabric.util.createClass(fabric.Object, /** @scope fabric.Polyline.prototype */ {
 17     
 18     /**
 19      * @property
 20      * @type String
 21      */
 22     type: 'polyline',
 23     
 24     /**
 25      * Constructor
 26      * @method initialize
 27      * @param {Array} points array of points
 28      * @param {Object} [options] Options object
 29      * @return {Object} thisArg
 30      */
 31     initialize: function(points, options) {
 32       options = options || { };
 33       this.set('points', points);
 34       this.callSuper('initialize', options);
 35       this._calcDimensions();
 36     },
 37     
 38     /**
 39      * @private
 40      * @method _calcDimensions
 41      */
 42     _calcDimensions: function() {
 43       return fabric.Polygon.prototype._calcDimensions.call(this);
 44     },
 45     
 46     /**
 47      * Returns object representation of an instance
 48      * @method toObject
 49      * @return {Object} Object representation of an instance
 50      */
 51     toObject: function() {
 52       return fabric.Polygon.prototype.toObject.call(this);
 53     },
 54     
 55     /**
 56      * @private
 57      * @method _render
 58      * @param {CanvasRenderingContext2D} ctx Context to render on
 59      */
 60     _render: function(ctx) {
 61       var point;
 62       ctx.beginPath();
 63       for (var i = 0, len = this.points.length; i < len; i++) {
 64         point = this.points[i];
 65         ctx.lineTo(point.x, point.y);
 66       }
 67       if (this.fill) {
 68         ctx.fill();
 69       }
 70       if (this.stroke) {
 71         ctx.stroke();
 72       }
 73     },
 74     
 75     /**
 76      * Returns complexity of an instance
 77      * @method complexity
 78      * @return {Number} complexity
 79      */
 80     complexity: function() {
 81       return this.get('points').length;
 82     }
 83   });
 84   
 85   /**
 86    * List of attribute names to account for when parsing SVG element (used by `fabric.Polyline.fromElement`)
 87    * @static
 88    * @see: http://www.w3.org/TR/SVG/shapes.html#PolylineElement
 89    */
 90   var ATTRIBUTE_NAMES = 'fill fill-opacity stroke stroke-width transform'.split(' ');
 91   
 92   /**
 93    * Returns fabric.Polyline instance from an SVG element
 94    * @static
 95    * @method fabric.Polyline.fromElement
 96    * @param {SVGElement} element Element to parse
 97    * @param {Object} [options] Options object
 98    * @return {Object} instance of fabric.Polyline
 99    */
100   fabric.Polyline.fromElement = function(element, options) {
101     if (!element) {
102       return null;
103     }
104     options || (options = { });
105     
106     var points = fabric.parsePointsAttribute(element.getAttribute('points')),
107         parsedAttributes = fabric.parseAttributes(element, ATTRIBUTE_NAMES);
108     
109     for (var i = 0, len = points.length; i < len; i++) {
110       // normalize coordinates, according to containing box (dimensions of which are passed via `options`)
111       points[i].x -= (options.width / 2) || 0;
112       points[i].y -= (options.height / 2) || 0;
113     }
114             
115     return new fabric.Polyline(points, fabric.util.object.extend(parsedAttributes, options));
116   };
117   
118   /**
119    * Returns fabric.Polyline instance from an object representation
120    * @static
121    * @method fabric.Polyline.fromObject
122    * @param {Object} [object] Object to create an instance from
123    * @return {fabric.Polyline}
124    */
125   fabric.Polyline.fromObject = function(object) {
126     var points = object.points;
127     return new fabric.Polyline(points, object);
128   };
129   
130 })();