2010-10-22 02:54:00 +00:00
|
|
|
(function(global) {
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-10-22 02:54:00 +00:00
|
|
|
"use strict";
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2012-01-02 21:14:20 +00:00
|
|
|
var fabric = global.fabric || (global.fabric = { }),
|
|
|
|
|
toFixed = fabric.util.toFixed;
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-07-09 23:43:50 +00:00
|
|
|
if (fabric.Polyline) {
|
2010-10-11 18:45:06 +00:00
|
|
|
fabric.warn('fabric.Polyline is already defined');
|
2010-06-10 17:57:59 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2012-06-26 14:42:45 +00:00
|
|
|
|
|
|
|
|
/**
|
2012-12-13 14:36:43 +00:00
|
|
|
* Polyline class
|
2010-10-14 21:42:39 +00:00
|
|
|
* @class Polyline
|
|
|
|
|
* @extends fabric.Object
|
|
|
|
|
*/
|
|
|
|
|
fabric.Polyline = fabric.util.createClass(fabric.Object, /** @scope fabric.Polyline.prototype */ {
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-10-15 02:16:24 +00:00
|
|
|
/**
|
2012-12-02 10:53:38 +00:00
|
|
|
* Type of an object
|
2010-10-15 02:16:24 +00:00
|
|
|
* @property
|
|
|
|
|
* @type String
|
|
|
|
|
*/
|
2010-06-09 22:34:55 +00:00
|
|
|
type: 'polyline',
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
2010-10-14 21:42:39 +00:00
|
|
|
* Constructor
|
2010-06-09 22:34:55 +00:00
|
|
|
* @method initialize
|
2010-10-14 21:42:39 +00:00
|
|
|
* @param {Array} points array of points
|
|
|
|
|
* @param {Object} [options] Options object
|
2013-02-11 12:21:33 +00:00
|
|
|
* @param {Boolean} Whether points offsetting should be skipped
|
2010-06-09 22:34:55 +00:00
|
|
|
* @return {Object} thisArg
|
|
|
|
|
*/
|
2013-02-11 12:21:33 +00:00
|
|
|
initialize: function(points, options, skipOffset) {
|
2010-06-09 22:34:55 +00:00
|
|
|
options = options || { };
|
|
|
|
|
this.set('points', points);
|
|
|
|
|
this.callSuper('initialize', options);
|
2013-02-11 12:21:33 +00:00
|
|
|
this._calcDimensions(skipOffset);
|
2010-06-09 22:34:55 +00:00
|
|
|
},
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
|
|
|
|
* @private
|
|
|
|
|
* @method _calcDimensions
|
|
|
|
|
*/
|
2013-02-11 12:21:33 +00:00
|
|
|
_calcDimensions: function(skipOffset) {
|
|
|
|
|
return fabric.Polygon.prototype._calcDimensions.call(this, skipOffset);
|
2010-06-09 22:34:55 +00:00
|
|
|
},
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
|
|
|
|
* Returns object representation of an instance
|
|
|
|
|
* @method toObject
|
2012-12-01 12:57:27 +00:00
|
|
|
* @param {Array} propertiesToInclude
|
|
|
|
|
* @return {Object} object representation of an instance
|
2010-06-09 22:34:55 +00:00
|
|
|
*/
|
2012-11-30 22:46:09 +00:00
|
|
|
toObject: function(propertiesToInclude) {
|
|
|
|
|
return fabric.Polygon.prototype.toObject.call(this, propertiesToInclude);
|
2010-06-09 22:34:55 +00:00
|
|
|
},
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2012-01-02 21:14:20 +00:00
|
|
|
/**
|
2012-12-13 14:36:43 +00:00
|
|
|
* Returns SVG representation of an instance
|
2012-01-02 21:14:20 +00:00
|
|
|
* @method toSVG
|
2012-12-18 10:46:51 +00:00
|
|
|
* @return {String} svg representation of an instance
|
2012-01-02 21:14:20 +00:00
|
|
|
*/
|
|
|
|
|
toSVG: function() {
|
|
|
|
|
var points = [];
|
|
|
|
|
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), ' ');
|
|
|
|
|
}
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2012-01-02 21:14:20 +00:00
|
|
|
return [
|
|
|
|
|
'<polyline ',
|
|
|
|
|
'points="', points.join(''), '" ',
|
|
|
|
|
'style="', this.getSvgStyles(), '" ',
|
|
|
|
|
'transform="', this.getSvgTransform(), '" ',
|
|
|
|
|
'/>'
|
|
|
|
|
].join('');
|
|
|
|
|
},
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
|
|
|
|
* @private
|
|
|
|
|
* @method _render
|
2010-10-14 21:42:39 +00:00
|
|
|
* @param {CanvasRenderingContext2D} ctx Context to render on
|
2010-06-09 22:34:55 +00:00
|
|
|
*/
|
|
|
|
|
_render: function(ctx) {
|
|
|
|
|
var point;
|
|
|
|
|
ctx.beginPath();
|
2012-06-28 19:12:30 +00:00
|
|
|
ctx.moveTo(this.points[0].x, this.points[0].y);
|
2010-06-09 22:34:55 +00:00
|
|
|
for (var i = 0, len = this.points.length; i < len; i++) {
|
|
|
|
|
point = this.points[i];
|
|
|
|
|
ctx.lineTo(point.x, point.y);
|
|
|
|
|
}
|
|
|
|
|
if (this.fill) {
|
|
|
|
|
ctx.fill();
|
|
|
|
|
}
|
2013-02-04 19:47:02 +00:00
|
|
|
this._removeShadow(ctx);
|
2010-06-09 22:34:55 +00:00
|
|
|
if (this.stroke) {
|
|
|
|
|
ctx.stroke();
|
|
|
|
|
}
|
|
|
|
|
},
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
2010-10-14 21:42:39 +00:00
|
|
|
* Returns complexity of an instance
|
2010-06-09 22:34:55 +00:00
|
|
|
* @method complexity
|
|
|
|
|
* @return {Number} complexity
|
|
|
|
|
*/
|
|
|
|
|
complexity: function() {
|
|
|
|
|
return this.get('points').length;
|
|
|
|
|
}
|
|
|
|
|
});
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-10-14 21:42:39 +00:00
|
|
|
/**
|
2012-11-23 12:38:13 +00:00
|
|
|
* List of attribute names to account for when parsing SVG element (used by {@link fabric.Polyline.fromElement})
|
2010-10-14 21:42:39 +00:00
|
|
|
* @static
|
|
|
|
|
* @see: http://www.w3.org/TR/SVG/shapes.html#PolylineElement
|
|
|
|
|
*/
|
2011-06-14 21:28:54 +00:00
|
|
|
fabric.Polyline.ATTRIBUTE_NAMES = 'fill fill-opacity opacity stroke stroke-width transform'.split(' ');
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
2010-10-14 21:42:39 +00:00
|
|
|
* Returns fabric.Polyline instance from an SVG element
|
2010-06-09 22:34:55 +00:00
|
|
|
* @static
|
2010-07-09 23:43:50 +00:00
|
|
|
* @method fabric.Polyline.fromElement
|
2010-10-14 21:42:39 +00:00
|
|
|
* @param {SVGElement} element Element to parse
|
|
|
|
|
* @param {Object} [options] Options object
|
2010-07-09 23:43:50 +00:00
|
|
|
* @return {Object} instance of fabric.Polyline
|
2010-06-09 22:34:55 +00:00
|
|
|
*/
|
2010-07-09 23:43:50 +00:00
|
|
|
fabric.Polyline.fromElement = function(element, options) {
|
2010-06-11 14:26:51 +00:00
|
|
|
if (!element) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2010-09-08 20:39:51 +00:00
|
|
|
options || (options = { });
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-07-09 23:43:50 +00:00
|
|
|
var points = fabric.parsePointsAttribute(element.getAttribute('points')),
|
2010-10-19 20:31:39 +00:00
|
|
|
parsedAttributes = fabric.parseAttributes(element, fabric.Polyline.ATTRIBUTE_NAMES);
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-09-08 20:39:51 +00:00
|
|
|
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;
|
|
|
|
|
}
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2013-02-11 12:21:33 +00:00
|
|
|
return new fabric.Polyline(points, fabric.util.object.extend(parsedAttributes, options), true);
|
2010-06-09 22:34:55 +00:00
|
|
|
};
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
2010-10-14 21:42:39 +00:00
|
|
|
* Returns fabric.Polyline instance from an object representation
|
2010-06-09 22:34:55 +00:00
|
|
|
* @static
|
2010-07-09 23:43:50 +00:00
|
|
|
* @method fabric.Polyline.fromObject
|
2010-10-14 21:42:39 +00:00
|
|
|
* @param {Object} [object] Object to create an instance from
|
|
|
|
|
* @return {fabric.Polyline}
|
2010-06-09 22:34:55 +00:00
|
|
|
*/
|
2010-07-09 23:43:50 +00:00
|
|
|
fabric.Polyline.fromObject = function(object) {
|
2010-06-09 22:34:55 +00:00
|
|
|
var points = object.points;
|
2013-02-13 18:22:19 +00:00
|
|
|
return new fabric.Polyline(points, object, true);
|
2010-10-14 21:42:39 +00:00
|
|
|
};
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2012-10-14 00:53:12 +00:00
|
|
|
})(typeof exports !== 'undefined' ? exports : this);
|