fabric.js/src/polyline.class.js

172 lines
4.8 KiB
JavaScript
Raw Normal View History

(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;
}
/**
2012-12-13 14:36:43 +00:00
* Polyline class
2013-04-25 18:21:32 +00:00
* @class fabric.Polyline
* @extends fabric.Object
*/
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
*/
2010-06-09 22:34:55 +00:00
type: 'polyline',
2010-06-09 22:34:55 +00:00
/**
* Constructor
* @param {Array} points array of points
* @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
2010-06-09 22:34:55 +00:00
*/
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
},
2010-06-09 22:34:55 +00:00
/**
* @private
* @param {Boolean} skipOffset Whether points offsetting should be skipped
2010-06-09 22:34:55 +00:00
*/
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
},
2010-06-09 22:34:55 +00:00
/**
* Returns object representation of an instance
* @param {Array} propertiesToInclude
* @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
},
/**
2012-12-13 14:36:43 +00:00
* Returns SVG representation of an instance
2012-12-18 10:46:51 +00:00
* @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(
'<polyline ',
'points="', points.join(''),
'" style="', this.getSvgStyles(),
'" transform="', this.getSvgTransform(),
'"/>'
);
return markup.join('');
},
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) {
var point;
ctx.beginPath();
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);
}
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
2010-06-09 22:34:55 +00:00
* @return {Number} complexity
*/
complexity: function() {
return this.get('points').length;
}
});
/**
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
* @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(' ');
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
* @param {SVGElement} element Element to parse
* @param {Object} [options] Options object
* @return {Object} 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);
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;
}
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
};
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
* @param {Object} [object] Object to create an instance from
* @return {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);