fabric.js/src/polyline.class.js
2012-12-18 11:46:51 +01:00

155 lines
No EOL
4.2 KiB
JavaScript

(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 Polyline
* @extends fabric.Object
*/
fabric.Polyline = fabric.util.createClass(fabric.Object, /** @scope fabric.Polyline.prototype */ {
/**
* Type of an object
* @property
* @type String
*/
type: 'polyline',
/**
* Constructor
* @method initialize
* @param {Array} points array of points
* @param {Object} [options] Options object
* @return {Object} thisArg
*/
initialize: function(points, options) {
options = options || { };
this.set('points', points);
this.callSuper('initialize', options);
this._calcDimensions();
},
/**
* @private
* @method _calcDimensions
*/
_calcDimensions: function() {
return fabric.Polygon.prototype._calcDimensions.call(this);
},
/**
* Returns object representation of an instance
* @method toObject
* @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
* @method toSVG
* @return {String} svg representation of an instance
*/
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), ' ');
}
return [
'<polyline ',
'points="', points.join(''), '" ',
'style="', this.getSvgStyles(), '" ',
'transform="', this.getSvgTransform(), '" ',
'/>'
].join('');
},
/**
* @private
* @method _render
* @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);
}
if (this.fill) {
ctx.fill();
}
if (this.stroke) {
ctx.stroke();
}
},
/**
* Returns complexity of an instance
* @method complexity
* @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
* @method fabric.Polyline.fromElement
* @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));
};
/**
* Returns fabric.Polyline instance from an object representation
* @static
* @method fabric.Polyline.fromObject
* @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);
};
})(typeof exports !== 'undefined' ? exports : this);