(function(global) { "use strict"; var fabric = global.fabric || (global.fabric = { }), toFixed = fabric.util.toFixed; if (fabric.Polyline) { fabric.warn('fabric.Polyline is already defined'); return; } /** * Polyline class * @class fabric.Polyline * @extends fabric.Object */ fabric.Polyline = fabric.util.createClass(fabric.Object, /** @lends fabric.Polyline.prototype */ { /** * Type of an object * @type String */ type: 'polyline', /** * Constructor * @param {Array} points array of points * @param {Object} [options] Options object * @param {Boolean} Whether points offsetting should be skipped * @return {fabric.Polyline} thisArg */ initialize: function(points, options, skipOffset) { options = options || { }; this.set('points', points); this.callSuper('initialize', options); this._calcDimensions(skipOffset); }, /** * @private */ _calcDimensions: function(skipOffset) { return fabric.Polygon.prototype._calcDimensions.call(this, skipOffset); }, /** * Returns object representation of an instance * @param {Array} propertiesToInclude * @return {Object} object representation of an instance */ toObject: function(propertiesToInclude) { return fabric.Polygon.prototype.toObject.call(this, propertiesToInclude); }, /** * Returns SVG representation of an instance * @return {String} svg representation of an instance */ toSVG: function() { var points = [], markup = []; for (var i = 0, len = this.points.length; i < len; i++) { points.push(toFixed(this.points[i].x, 2), ',', toFixed(this.points[i].y, 2), ' '); } if (this.fill && this.fill.toLive) { markup.push(this.fill.toSVG(this, false)); } if (this.stroke && this.stroke.toLive) { markup.push(this.stroke.toSVG(this, false)); } markup.push( '' ); return markup.join(''); }, /** * @private * @param {CanvasRenderingContext2D} ctx Context to render on */ _render: function(ctx) { var point; ctx.beginPath(); ctx.moveTo(this.points[0].x, this.points[0].y); for (var i = 0, len = this.points.length; i < len; i++) { point = this.points[i]; ctx.lineTo(point.x, point.y); } this._renderFill(ctx); this._removeShadow(ctx); if (this.stroke) { ctx.stroke(); } }, /** * Returns complexity of an instance * @return {Number} complexity */ complexity: function() { return this.get('points').length; } }); /** * List of attribute names to account for when parsing SVG element (used by {@link fabric.Polyline.fromElement}) * @static * @see: http://www.w3.org/TR/SVG/shapes.html#PolylineElement */ fabric.Polyline.ATTRIBUTE_NAMES = 'fill fill-opacity opacity stroke stroke-width transform'.split(' '); /** * Returns fabric.Polyline instance from an SVG element * @static * @param {SVGElement} element Element to parse * @param {Object} [options] Options object * @return {Object} instance of fabric.Polyline */ fabric.Polyline.fromElement = function(element, options) { if (!element) { return null; } options || (options = { }); var points = fabric.parsePointsAttribute(element.getAttribute('points')), parsedAttributes = fabric.parseAttributes(element, fabric.Polyline.ATTRIBUTE_NAMES); for (var i = 0, len = points.length; i < len; i++) { // normalize coordinates, according to containing box (dimensions of which are passed via `options`) points[i].x -= (options.width / 2) || 0; points[i].y -= (options.height / 2) || 0; } return new fabric.Polyline(points, fabric.util.object.extend(parsedAttributes, options), true); }; /** * Returns fabric.Polyline instance from an object representation * @static * @param {Object} [object] Object to create an instance from * @return {fabric.Polyline} */ fabric.Polyline.fromObject = function(object) { var points = object.points; return new fabric.Polyline(points, object, true); }; })(typeof exports !== 'undefined' ? exports : this);