2010-10-22 02:54:00 +00:00
|
|
|
(function(global) {
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-10-22 02:54:00 +00:00
|
|
|
"use strict";
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-10-22 02:54:00 +00:00
|
|
|
var fabric = global.fabric || (global.fabric = { }),
|
2010-07-26 23:20:19 +00:00
|
|
|
extend = fabric.util.object.extend,
|
|
|
|
|
min = fabric.util.array.min,
|
2012-01-02 21:14:20 +00:00
|
|
|
max = fabric.util.array.max,
|
|
|
|
|
toFixed = fabric.util.toFixed;
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-07-09 23:43:50 +00:00
|
|
|
if (fabric.Polygon) {
|
2010-10-11 18:45:06 +00:00
|
|
|
fabric.warn('fabric.Polygon is already defined');
|
2010-06-10 17:57:59 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2012-06-26 14:42:45 +00:00
|
|
|
|
|
|
|
|
/**
|
2012-12-13 14:36:43 +00:00
|
|
|
* Polygon class
|
2010-10-14 21:42:39 +00:00
|
|
|
* @class Polygon
|
|
|
|
|
* @extends fabric.Object
|
|
|
|
|
*/
|
|
|
|
|
fabric.Polygon = fabric.util.createClass(fabric.Object, /** @scope fabric.Polygon.prototype */ {
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-10-15 02:16:24 +00:00
|
|
|
/**
|
2012-12-02 10:53:38 +00:00
|
|
|
* Type of an object
|
2010-10-15 02:16:24 +00:00
|
|
|
* @property
|
|
|
|
|
* @type String
|
|
|
|
|
*/
|
2010-06-09 22:34:55 +00:00
|
|
|
type: 'polygon',
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
2010-10-14 21:42:39 +00:00
|
|
|
* Constructor
|
2010-06-09 22:34:55 +00:00
|
|
|
* @method initialize
|
2010-10-14 21:42:39 +00:00
|
|
|
* @param {Array} points Array of points
|
2012-12-13 14:36:43 +00:00
|
|
|
* @param {Object} [options] Options object
|
2013-02-11 12:21:33 +00:00
|
|
|
* @param {Boolean} Whether points offsetting should be skipped
|
2010-10-14 21:42:39 +00:00
|
|
|
* @return {fabric.Polygon} 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.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
|
|
|
},
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
|
|
|
|
* @private
|
|
|
|
|
* @method _calcDimensions
|
|
|
|
|
*/
|
2013-02-11 12:21:33 +00:00
|
|
|
_calcDimensions: function(skipOffset) {
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
var points = this.points,
|
2010-07-26 23:20:19 +00:00
|
|
|
minX = min(points, 'x'),
|
|
|
|
|
minY = min(points, 'y'),
|
|
|
|
|
maxX = max(points, 'x'),
|
|
|
|
|
maxY = max(points, 'y');
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2012-09-02 03:22:01 +00:00
|
|
|
this.width = (maxX - minX) || 1;
|
|
|
|
|
this.height = (maxY - minY) || 1;
|
|
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
this.minX = minX;
|
|
|
|
|
this.minY = minY;
|
2013-02-11 12:21:33 +00:00
|
|
|
|
|
|
|
|
if (skipOffset) return;
|
|
|
|
|
|
|
|
|
|
var halfWidth = this.width / 2,
|
|
|
|
|
halfHeight = this.height / 2;
|
|
|
|
|
|
|
|
|
|
// change points to offset polygon into a bounding box
|
|
|
|
|
this.points.forEach(function(p) {
|
|
|
|
|
p.x -= halfWidth;
|
|
|
|
|
p.y -= halfHeight;
|
|
|
|
|
}, this);
|
2010-06-09 22:34:55 +00:00
|
|
|
},
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
|
|
|
|
* Returns object representation of an instance
|
|
|
|
|
* @method toObject
|
2012-12-01 12:57:27 +00:00
|
|
|
* @param {Array} propertiesToInclude
|
2010-06-09 22:34:55 +00:00
|
|
|
* @return {Object} object representation of an instance
|
|
|
|
|
*/
|
2012-11-30 22:46:09 +00:00
|
|
|
toObject: function(propertiesToInclude) {
|
|
|
|
|
return extend(this.callSuper('toObject', propertiesToInclude), {
|
2010-06-27 02:57:02 +00:00
|
|
|
points: this.points.concat()
|
2010-06-09 22:34:55 +00:00
|
|
|
});
|
|
|
|
|
},
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2012-01-02 21:14:20 +00:00
|
|
|
/**
|
|
|
|
|
* Returns svg representation of an instance
|
|
|
|
|
* @method toSVG
|
2012-12-13 14:36:43 +00:00
|
|
|
* @return {String} svg representation of an instance
|
2012-01-02 21:14:20 +00:00
|
|
|
*/
|
|
|
|
|
toSVG: function() {
|
2013-02-23 16:02:52 +00:00
|
|
|
var points = [],
|
|
|
|
|
markup = [];
|
|
|
|
|
|
2012-01-02 21:14:20 +00:00
|
|
|
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), ' ');
|
|
|
|
|
}
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2013-02-23 16:02:52 +00:00
|
|
|
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(
|
2012-01-02 21:14:20 +00:00
|
|
|
'<polygon ',
|
2013-02-23 16:02:52 +00:00
|
|
|
'points="', points.join(''),
|
|
|
|
|
'" style="', this.getSvgStyles(),
|
|
|
|
|
'" transform="', this.getSvgTransform(),
|
|
|
|
|
'"/>'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return markup.join('');
|
2012-01-02 21:14:20 +00:00
|
|
|
},
|
2012-06-26 14:42:45 +00:00
|
|
|
|
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();
|
2012-08-14 12:03:20 +00:00
|
|
|
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];
|
2012-08-14 12:03:20 +00:00
|
|
|
ctx.lineTo(point.x, point.y);
|
2010-06-09 22:34:55 +00:00
|
|
|
}
|
|
|
|
|
if (this.fill) {
|
|
|
|
|
ctx.fill();
|
|
|
|
|
}
|
2013-02-04 19:47:02 +00:00
|
|
|
this._removeShadow(ctx);
|
2010-06-09 22:34:55 +00:00
|
|
|
if (this.stroke) {
|
|
|
|
|
ctx.closePath();
|
|
|
|
|
ctx.stroke();
|
|
|
|
|
}
|
|
|
|
|
},
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
|
|
|
|
* Returns complexity of an instance
|
|
|
|
|
* @method complexity
|
|
|
|
|
* @return {Number} complexity of this instance
|
|
|
|
|
*/
|
|
|
|
|
complexity: function() {
|
|
|
|
|
return this.points.length;
|
|
|
|
|
}
|
|
|
|
|
});
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-10-14 21:42:39 +00:00
|
|
|
/**
|
|
|
|
|
* List of attribute names to account for when parsing SVG element (used by `fabric.Polygon.fromElement`)
|
|
|
|
|
* @static
|
|
|
|
|
* @see: http://www.w3.org/TR/SVG/shapes.html#PolygonElement
|
|
|
|
|
*/
|
2011-06-14 21:28:54 +00:00
|
|
|
fabric.Polygon.ATTRIBUTE_NAMES = 'fill fill-opacity opacity stroke stroke-width transform'.split(' ');
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
2012-12-13 14:36:43 +00:00
|
|
|
* Returns {@link fabric.Polygon} instance from an SVG element
|
2010-06-09 22:34:55 +00:00
|
|
|
* @static
|
2010-07-09 23:43:50 +00:00
|
|
|
* @method fabric.Polygon.fromElement
|
2010-10-14 21:42:39 +00:00
|
|
|
* @param {SVGElement} element Element to parse
|
2012-12-13 14:36:43 +00:00
|
|
|
* @param {Object} [options] Options object
|
2010-10-14 21:42:39 +00:00
|
|
|
* @return {fabric.Polygon}
|
2010-06-09 22:34:55 +00:00
|
|
|
*/
|
2010-07-09 23:43:50 +00:00
|
|
|
fabric.Polygon.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 = { });
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-07-09 23:43:50 +00:00
|
|
|
var points = fabric.parsePointsAttribute(element.getAttribute('points')),
|
|
|
|
|
parsedAttributes = fabric.parseAttributes(element, fabric.Polygon.ATTRIBUTE_NAMES);
|
2012-06-26 14:42:45 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2013-02-11 12:21:33 +00:00
|
|
|
return new fabric.Polygon(points, extend(parsedAttributes, options), true);
|
2010-06-09 22:34:55 +00:00
|
|
|
};
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
2010-10-14 21:42:39 +00:00
|
|
|
* Returns fabric.Polygon instance from an object representation
|
2010-06-09 22:34:55 +00:00
|
|
|
* @static
|
2010-07-09 23:43:50 +00:00
|
|
|
* @method fabric.Polygon.fromObject
|
2010-10-14 21:42:39 +00:00
|
|
|
* @param {Object} object Object to create an instance from
|
|
|
|
|
* @return {fabric.Polygon}
|
2010-06-09 22:34:55 +00:00
|
|
|
*/
|
2010-07-09 23:43:50 +00:00
|
|
|
fabric.Polygon.fromObject = function(object) {
|
2013-02-13 18:22:19 +00:00
|
|
|
return new fabric.Polygon(object.points, object, true);
|
2011-08-05 23:00:26 +00:00
|
|
|
};
|
|
|
|
|
|
2012-10-14 00:53:12 +00:00
|
|
|
})(typeof exports !== 'undefined' ? exports : this);
|