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