mirror of
https://github.com/Hopiu/fabric.js.git
synced 2026-03-17 14:30:24 +00:00
132 lines
No EOL
3.5 KiB
JavaScript
132 lines
No EOL
3.5 KiB
JavaScript
//= require "object.class"
|
|
|
|
(function(global) {
|
|
|
|
"use strict";
|
|
|
|
var fabric = global.fabric || (global.fabric = { });
|
|
|
|
if (fabric.Polyline) {
|
|
fabric.warn('fabric.Polyline is already defined');
|
|
return;
|
|
}
|
|
|
|
/**
|
|
* @class Polyline
|
|
* @extends fabric.Object
|
|
*/
|
|
fabric.Polyline = fabric.util.createClass(fabric.Object, /** @scope fabric.Polyline.prototype */ {
|
|
|
|
/**
|
|
* @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
|
|
* @return {Object} Object representation of an instance
|
|
*/
|
|
toObject: function() {
|
|
return fabric.Polygon.prototype.toObject.call(this);
|
|
},
|
|
|
|
/**
|
|
* @private
|
|
* @method _render
|
|
* @param {CanvasRenderingContext2D} ctx Context to render on
|
|
*/
|
|
_render: function(ctx) {
|
|
var point;
|
|
ctx.beginPath();
|
|
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 `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); |