fabric.js/src/polyline.class.js

114 lines
3 KiB
JavaScript
Raw Normal View History

//= require "object.class"
2010-06-09 22:34:55 +00:00
(function(){
var fabric = this.fabric || (this.fabric = { });
if (fabric.Polyline) {
fabric.warn('fabric.Polyline is already defined');
return;
}
2010-06-09 22:34:55 +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() {
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() {
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
var ATTRIBUTE_NAMES = 'fill fill-opacity stroke stroke-width transform'.split(' ');
2010-06-09 22:34:55 +00:00
/**
* @static
* @method fabric.Polyline.fromElement
2010-06-09 22:34:55 +00:00
* @param element {SVGElement} element to parse
* @param options {Object} 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, 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));
2010-06-09 22:34:55 +00:00
};
/**
* @static
* @method fabric.Polyline.fromObject
2010-06-09 22:34:55 +00:00
* @param object {Object} object to create an instance from
* @return {Object} 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);
2010-06-09 22:34:55 +00:00
}
})();