2010-07-10 01:50:13 +00:00
|
|
|
//= require "object.class"
|
2010-06-11 23:37:06 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
(function(){
|
|
|
|
|
|
2010-07-09 23:43:50 +00:00
|
|
|
var fabric = this.fabric || (this.fabric = { });
|
2010-06-11 14:26:51 +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;
|
|
|
|
|
}
|
2010-06-09 22:34:55 +00:00
|
|
|
|
2010-07-10 01:50:13 +00:00
|
|
|
fabric.Polyline = fabric.util.createClass(fabric.Object, {
|
2010-06-09 22:34:55 +00:00
|
|
|
|
|
|
|
|
type: 'polyline',
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @constructor
|
|
|
|
|
* @method initialize
|
|
|
|
|
* @param points {Array} array of points
|
|
|
|
|
* @param options {Object} 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() {
|
2010-07-09 23:43:50 +00:00
|
|
|
return fabric.Polygon.prototype._calcDimensions.call(this);
|
2010-06-09 22:34:55 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns object representation of an instance
|
|
|
|
|
* @method toObject
|
|
|
|
|
* @return {Object} object representation of an instance
|
|
|
|
|
*/
|
|
|
|
|
toObject: function() {
|
2010-07-09 23:43:50 +00:00
|
|
|
return fabric.Polygon.prototype.toObject.call(this);
|
2010-06-09 22:34:55 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @private
|
|
|
|
|
* @method _render
|
|
|
|
|
* @param ctx {CanvasRenderingContext2D} 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();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @method complexity
|
|
|
|
|
* @return {Number} complexity
|
|
|
|
|
*/
|
|
|
|
|
complexity: function() {
|
|
|
|
|
return this.get('points').length;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// http://www.w3.org/TR/SVG/shapes.html#PolylineElement
|
2010-06-11 14:26:51 +00:00
|
|
|
var ATTRIBUTE_NAMES = 'fill fill-opacity stroke stroke-width transform'.split(' ');
|
2010-06-09 22:34:55 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @static
|
2010-07-09 23:43:50 +00:00
|
|
|
* @method fabric.Polyline.fromElement
|
2010-06-09 22:34:55 +00:00
|
|
|
* @param element {SVGElement} element to parse
|
|
|
|
|
* @param options {Object} 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 = { });
|
|
|
|
|
|
2010-07-09 23:43:50 +00:00
|
|
|
var points = fabric.parsePointsAttribute(element.getAttribute('points')),
|
|
|
|
|
parsedAttributes = fabric.parseAttributes(element, ATTRIBUTE_NAMES);
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-10 01:50:13 +00:00
|
|
|
return new fabric.Polyline(points, fabric.util.object.extend(parsedAttributes, options));
|
2010-06-09 22:34:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @static
|
2010-07-09 23:43:50 +00:00
|
|
|
* @method fabric.Polyline.fromObject
|
2010-06-09 22:34:55 +00:00
|
|
|
* @param object {Object} object to create an instance from
|
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.fromObject = function(object) {
|
2010-06-09 22:34:55 +00:00
|
|
|
var points = object.points;
|
2010-07-09 23:43:50 +00:00
|
|
|
return new fabric.Polyline(points, object);
|
2010-06-09 22:34:55 +00:00
|
|
|
}
|
|
|
|
|
})();
|