fabric.js/src/shapes/polyline.class.js

185 lines
4.9 KiB
JavaScript
Raw Normal View History

(function(global) {
'use strict';
2014-09-13 14:48:38 +00:00
var fabric = global.fabric || (global.fabric = { });
if (fabric.Polyline) {
fabric.warn('fabric.Polyline is already defined');
return;
}
/**
2012-12-13 14:36:43 +00:00
* Polyline class
2013-04-25 18:21:32 +00:00
* @class fabric.Polyline
* @extends fabric.Object
* @see {@link fabric.Polyline#initialize} for constructor definition
*/
fabric.Polyline = fabric.util.createClass(fabric.Object, /** @lends fabric.Polyline.prototype */ {
/**
2012-12-02 10:53:38 +00:00
* Type of an object
* @type String
* @default
*/
2010-06-09 22:34:55 +00:00
type: 'polyline',
/**
* Points array
* @type Array
* @default
*/
points: null,
/**
* Minimum X from points values, necessary to offset points
* @type Number
* @default
*/
minX: 0,
/**
* Minimum Y from points values, necessary to offset points
* @type Number
* @default
*/
minY: 0,
2010-06-09 22:34:55 +00:00
/**
* Constructor
* @param {Array} points Array of points (where each point is an object with x and y)
* @param {Object} [options] Options object
* @param {Boolean} [skipOffset] Whether points offsetting should be skipped
2013-04-25 18:21:32 +00:00
* @return {fabric.Polyline} thisArg
* @example
* var poly = new fabric.Polyline([
* { x: 10, y: 10 },
* { x: 50, y: 30 },
* { x: 40, y: 70 },
* { x: 60, y: 50 },
* { x: 100, y: 150 },
* { x: 40, y: 100 }
* ], {
* stroke: 'red',
* left: 100,
* top: 100
* });
2010-06-09 22:34:55 +00:00
*/
2014-09-03 20:49:47 +00:00
initialize: function(points, options) {
return fabric.Polygon.prototype.initialize.call(this, points, options);
2010-06-09 22:34:55 +00:00
},
2010-06-09 22:34:55 +00:00
/**
* @private
*/
2014-09-03 20:49:47 +00:00
_calcDimensions: function() {
return fabric.Polygon.prototype._calcDimensions.call(this);
},
/**
* @private
*/
_applyPointOffset: function() {
return fabric.Polygon.prototype._applyPointOffset.call(this);
2010-06-09 22:34:55 +00:00
},
2010-06-09 22:34:55 +00:00
/**
* Returns object representation of an instance
* @param {Array} [propertiesToInclude] Any properties that you might want to additionally include in the output
* @return {Object} Object representation of an instance
2010-06-09 22:34:55 +00:00
*/
toObject: function(propertiesToInclude) {
return fabric.Polygon.prototype.toObject.call(this, propertiesToInclude);
2010-06-09 22:34:55 +00:00
},
/* _TO_SVG_START_ */
/**
2012-12-13 14:36:43 +00:00
* Returns SVG representation of an instance
* @param {Function} [reviver] Method for further parsing of svg representation.
2012-12-18 10:46:51 +00:00
* @return {String} svg representation of an instance
*/
toSVG: function(reviver) {
2014-09-13 14:39:40 +00:00
return fabric.Polygon.prototype.toSVG.call(this, reviver);
},
/* _TO_SVG_END_ */
2010-06-09 22:34:55 +00:00
/**
* @private
* @param {CanvasRenderingContext2D} ctx Context to render on
2010-06-09 22:34:55 +00:00
*/
_render: function(ctx) {
2015-02-03 13:28:37 +00:00
if (!fabric.Polygon.prototype.commonRender.call(this, ctx)) {
2015-02-02 23:02:16 +00:00
return;
}
this._renderFill(ctx);
this._renderStroke(ctx);
},
/**
* @private
* @param {CanvasRenderingContext2D} ctx Context to render on
*/
_renderDashedStroke: function(ctx) {
var p1, p2;
ctx.beginPath();
for (var i = 0, len = this.points.length; i < len; i++) {
2013-05-04 16:32:22 +00:00
p1 = this.points[i];
p2 = this.points[i + 1] || p1;
fabric.util.drawDashedLine(ctx, p1.x, p1.y, p2.x, p2.y, this.strokeDashArray);
2010-06-09 22:34:55 +00:00
}
},
2010-06-09 22:34:55 +00:00
/**
* Returns complexity of an instance
* @return {Number} complexity of this instance
2010-06-09 22:34:55 +00:00
*/
complexity: function() {
return this.get('points').length;
}
});
/* _FROM_SVG_START_ */
/**
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})
* @static
* @memberOf fabric.Polyline
* @see: http://www.w3.org/TR/SVG/shapes.html#PolylineElement
*/
fabric.Polyline.ATTRIBUTE_NAMES = fabric.SHARED_ATTRIBUTES.concat();
2010-06-09 22:34:55 +00:00
/**
* Returns fabric.Polyline instance from an SVG element
2010-06-09 22:34:55 +00:00
* @static
* @memberOf fabric.Polyline
* @param {SVGElement} element Element to parse
* @param {Object} [options] Options object
* @return {fabric.Polyline} Instance of fabric.Polyline
2010-06-09 22:34:55 +00:00
*/
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);
2014-09-03 20:49:47 +00:00
return new fabric.Polyline(points, fabric.util.object.extend(parsedAttributes, options));
2010-06-09 22:34:55 +00:00
};
/* _FROM_SVG_END_ */
2010-06-09 22:34:55 +00:00
/**
* Returns fabric.Polyline instance from an object representation
2010-06-09 22:34:55 +00:00
* @static
* @memberOf fabric.Polyline
2014-07-17 14:18:57 +00:00
* @param {Object} object Object to create an instance from
* @return {fabric.Polyline} Instance of fabric.Polyline
2010-06-09 22:34:55 +00:00
*/
fabric.Polyline.fromObject = function(object) {
2010-06-09 22:34:55 +00:00
var points = object.points;
return new fabric.Polyline(points, object, true);
};
})(typeof exports !== 'undefined' ? exports : this);