2010-10-22 02:54:00 +00:00
|
|
|
(function(global) {
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2014-02-16 21:36:03 +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
|
2013-04-25 18:21:32 +00:00
|
|
|
* @class fabric.Polygon
|
2010-10-14 21:42:39 +00:00
|
|
|
* @extends fabric.Object
|
2013-10-05 18:21:28 +00:00
|
|
|
* @see {@link fabric.Polygon#initialize} for constructor definition
|
2010-10-14 21:42:39 +00:00
|
|
|
*/
|
2013-04-24 16:58:04 +00:00
|
|
|
fabric.Polygon = fabric.util.createClass(fabric.Object, /** @lends 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
|
|
|
* @type String
|
2013-05-18 11:01:34 +00:00
|
|
|
* @default
|
2010-10-15 02:16:24 +00:00
|
|
|
*/
|
2010-06-09 22:34:55 +00:00
|
|
|
type: 'polygon',
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2014-01-23 15:48:37 +00:00
|
|
|
/**
|
|
|
|
|
* Points array
|
|
|
|
|
* @type Array
|
|
|
|
|
* @default
|
|
|
|
|
*/
|
|
|
|
|
points: null,
|
|
|
|
|
|
2014-09-04 20:37:25 +00:00
|
|
|
/**
|
|
|
|
|
* Minimum X from points values, necessary to offset points
|
|
|
|
|
* @type Number
|
|
|
|
|
* @default
|
|
|
|
|
*/
|
|
|
|
|
minX: 0,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Minimum Y from points values, necessary to offset points
|
|
|
|
|
* @type Number
|
|
|
|
|
* @default
|
|
|
|
|
*/
|
|
|
|
|
minY: 0,
|
|
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
2010-10-14 21:42:39 +00:00
|
|
|
* Constructor
|
|
|
|
|
* @param {Array} points Array of points
|
2012-12-13 14:36:43 +00:00
|
|
|
* @param {Object} [options] Options object
|
2010-10-14 21:42:39 +00:00
|
|
|
* @return {fabric.Polygon} thisArg
|
2010-06-09 22:34:55 +00:00
|
|
|
*/
|
2014-09-03 20:49:04 +00:00
|
|
|
initialize: function(points, options) {
|
2010-06-09 22:34:55 +00:00
|
|
|
options = options || { };
|
2015-02-02 23:02:16 +00:00
|
|
|
this.points = points || [ ];
|
2010-06-09 22:34:55 +00:00
|
|
|
this.callSuper('initialize', options);
|
2014-09-03 20:49:04 +00:00
|
|
|
this._calcDimensions();
|
2014-09-05 21:04:12 +00:00
|
|
|
if (!('top' in options)) {
|
2014-09-04 20:37:25 +00:00
|
|
|
this.top = this.minY;
|
|
|
|
|
}
|
2014-09-05 21:04:12 +00:00
|
|
|
if (!('left' in options)) {
|
2014-09-04 20:37:25 +00:00
|
|
|
this.left = this.minX;
|
|
|
|
|
}
|
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
|
|
|
|
|
*/
|
2014-09-03 20:49:04 +00:00
|
|
|
_calcDimensions: function() {
|
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
|
|
|
|
2015-02-02 23:02:16 +00:00
|
|
|
this.width = (maxX - minX) || 0;
|
|
|
|
|
this.height = (maxY - minY) || 0;
|
2012-09-02 03:22:01 +00:00
|
|
|
|
2015-02-02 23:02:16 +00:00
|
|
|
this.minX = minX || 0,
|
|
|
|
|
this.minY = minY || 0;
|
2014-09-03 20:49:04 +00:00
|
|
|
},
|
2013-02-11 12:21:33 +00:00
|
|
|
|
2014-09-03 20:49:04 +00:00
|
|
|
/**
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
_applyPointOffset: function() {
|
2013-02-11 12:21:33 +00:00
|
|
|
// change points to offset polygon into a bounding box
|
2014-09-03 20:49:04 +00:00
|
|
|
// executed one time
|
2013-02-11 12:21:33 +00:00
|
|
|
this.points.forEach(function(p) {
|
2014-09-04 20:37:25 +00:00
|
|
|
p.x -= (this.minX + this.width / 2);
|
|
|
|
|
p.y -= (this.minY + this.height / 2);
|
2013-02-11 12:21:33 +00:00
|
|
|
}, 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
|
2013-05-23 18:02:44 +00:00
|
|
|
* @param {Array} [propertiesToInclude] Any properties that you might want to additionally include in the output
|
|
|
|
|
* @return {Object} Object representation of an instance
|
2010-06-09 22:34:55 +00:00
|
|
|
*/
|
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
|
|
|
|
2013-05-09 18:19:06 +00:00
|
|
|
/* _TO_SVG_START_ */
|
2012-01-02 21:14:20 +00:00
|
|
|
/**
|
|
|
|
|
* Returns svg representation of an instance
|
2013-09-29 07:22:44 +00:00
|
|
|
* @param {Function} [reviver] Method for further parsing of svg representation.
|
2012-12-13 14:36:43 +00:00
|
|
|
* @return {String} svg representation of an instance
|
2012-01-02 21:14:20 +00:00
|
|
|
*/
|
2013-09-29 07:22:44 +00:00
|
|
|
toSVG: function(reviver) {
|
2013-02-23 16:02:52 +00:00
|
|
|
var points = [],
|
2013-07-25 17:39:21 +00:00
|
|
|
markup = this._createBaseSVGMarkup();
|
2013-02-23 16:02:52 +00:00
|
|
|
|
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
|
|
|
markup.push(
|
2014-09-13 14:38:42 +00:00
|
|
|
'<', this.type, ' ',
|
2013-02-23 16:02:52 +00:00
|
|
|
'points="', points.join(''),
|
|
|
|
|
'" style="', this.getSvgStyles(),
|
|
|
|
|
'" transform="', this.getSvgTransform(),
|
2014-08-05 10:56:08 +00:00
|
|
|
' ', this.getSvgTransformMatrix(),
|
|
|
|
|
'"/>\n'
|
2013-02-23 16:02:52 +00:00
|
|
|
);
|
|
|
|
|
|
2013-09-29 07:22:44 +00:00
|
|
|
return reviver ? reviver(markup.join('')) : markup.join('');
|
2012-01-02 21:14:20 +00:00
|
|
|
},
|
2013-05-09 18:19:06 +00:00
|
|
|
/* _TO_SVG_END_ */
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
|
|
|
|
* @private
|
2013-05-23 18:02:44 +00:00
|
|
|
* @param {CanvasRenderingContext2D} ctx Context to render on
|
2010-06-09 22:34:55 +00:00
|
|
|
*/
|
|
|
|
|
_render: function(ctx) {
|
2015-02-03 13:28:23 +00:00
|
|
|
if (!this.commonRender(ctx)) {
|
2015-02-02 23:02:16 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2014-09-13 14:38:42 +00:00
|
|
|
this._renderFill(ctx);
|
|
|
|
|
if (this.stroke || this.strokeDashArray) {
|
|
|
|
|
ctx.closePath();
|
|
|
|
|
this._renderStroke(ctx);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @private
|
|
|
|
|
* @param {CanvasRenderingContext2D} ctx Context to render on
|
|
|
|
|
*/
|
|
|
|
|
commonRender: function(ctx) {
|
2015-02-02 23:02:16 +00:00
|
|
|
var point, len = this.points.length;
|
|
|
|
|
|
|
|
|
|
if (!len || isNaN(this.points[len - 1].y)) {
|
|
|
|
|
// do not draw if no points or odd points
|
|
|
|
|
// NaN comes from parseFloat of a empty string in parser
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
ctx.beginPath();
|
2014-09-03 20:49:04 +00:00
|
|
|
|
|
|
|
|
if (this._applyPointOffset) {
|
|
|
|
|
if (!(this.group && this.group.type === 'path-group')) {
|
|
|
|
|
this._applyPointOffset();
|
|
|
|
|
}
|
|
|
|
|
this._applyPointOffset = null;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-14 12:03:20 +00:00
|
|
|
ctx.moveTo(this.points[0].x, this.points[0].y);
|
2015-02-02 23:02:16 +00:00
|
|
|
for (var i = 0; i < len; i++) {
|
2010-06-09 22:34:55 +00:00
|
|
|
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
|
|
|
}
|
2015-02-02 23:02:16 +00:00
|
|
|
return true;
|
2010-06-09 22:34:55 +00:00
|
|
|
},
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2013-05-04 16:25:57 +00:00
|
|
|
/**
|
|
|
|
|
* @private
|
2013-05-23 18:02:44 +00:00
|
|
|
* @param {CanvasRenderingContext2D} ctx Context to render on
|
2013-05-04 16:25:57 +00:00
|
|
|
*/
|
|
|
|
|
_renderDashedStroke: function(ctx) {
|
2014-09-13 14:38:42 +00:00
|
|
|
fabric.Polyline.prototype._renderDashedStroke.call(this, ctx);
|
2013-05-04 16:25:57 +00:00
|
|
|
ctx.closePath();
|
|
|
|
|
},
|
|
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
|
|
|
|
* Returns complexity of an instance
|
|
|
|
|
* @return {Number} complexity of this instance
|
|
|
|
|
*/
|
|
|
|
|
complexity: function() {
|
|
|
|
|
return this.points.length;
|
|
|
|
|
}
|
|
|
|
|
});
|
2012-06-26 14:42:45 +00:00
|
|
|
|
2013-05-30 19:53:49 +00:00
|
|
|
/* _FROM_SVG_START_ */
|
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
|
2013-08-08 16:31:26 +00:00
|
|
|
* @memberOf fabric.Polygon
|
2010-10-14 21:42:39 +00:00
|
|
|
* @see: http://www.w3.org/TR/SVG/shapes.html#PolygonElement
|
|
|
|
|
*/
|
2013-05-18 14:43:49 +00:00
|
|
|
fabric.Polygon.ATTRIBUTE_NAMES = fabric.SHARED_ATTRIBUTES.concat();
|
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
|
2013-08-08 16:31:26 +00:00
|
|
|
* @memberOf fabric.Polygon
|
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
|
2013-05-23 18:02:44 +00:00
|
|
|
* @return {fabric.Polygon} Instance of 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;
|
|
|
|
|
}
|
2014-08-05 10:56:08 +00:00
|
|
|
|
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')),
|
2013-10-26 18:20:48 +00:00
|
|
|
parsedAttributes = fabric.parseAttributes(element, fabric.Polygon.ATTRIBUTE_NAMES);
|
2013-05-23 18:02:44 +00:00
|
|
|
|
2014-09-03 20:49:04 +00:00
|
|
|
return new fabric.Polygon(points, extend(parsedAttributes, options));
|
2010-06-09 22:34:55 +00:00
|
|
|
};
|
2013-05-30 19:53:49 +00:00
|
|
|
/* _FROM_SVG_END_ */
|
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
|
2013-08-08 16:31:26 +00:00
|
|
|
* @memberOf fabric.Polygon
|
2014-07-17 14:18:57 +00:00
|
|
|
* @param {Object} object Object to create an instance from
|
2013-05-23 18:02:44 +00:00
|
|
|
* @return {fabric.Polygon} Instance of 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
|
|
|
};
|
|
|
|
|
|
2013-04-22 13:16:58 +00:00
|
|
|
})(typeof exports !== 'undefined' ? exports : this);
|