2010-10-22 02:54:00 +00:00
|
|
|
(function(global) {
|
2012-05-25 11:34:01 +00:00
|
|
|
|
2014-02-16 21:36:03 +00:00
|
|
|
'use strict';
|
2012-05-25 11:34:01 +00:00
|
|
|
|
2014-09-17 23:23:52 +00:00
|
|
|
var fabric = global.fabric || (global.fabric = { }),
|
2018-01-20 00:09:47 +00:00
|
|
|
pi = Math.PI;
|
2012-05-25 11:34:01 +00:00
|
|
|
|
2010-07-09 23:43:50 +00:00
|
|
|
if (fabric.Circle) {
|
2010-10-11 18:45:06 +00:00
|
|
|
fabric.warn('fabric.Circle is already defined.');
|
2010-06-10 17:57:59 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2010-10-14 21:42:39 +00:00
|
|
|
|
2012-05-25 11:34:01 +00:00
|
|
|
/**
|
2012-12-13 14:36:43 +00:00
|
|
|
* Circle class
|
2013-04-25 18:21:32 +00:00
|
|
|
* @class fabric.Circle
|
2010-10-14 21:42:39 +00:00
|
|
|
* @extends fabric.Object
|
2013-10-05 18:21:28 +00:00
|
|
|
* @see {@link fabric.Circle#initialize} for constructor definition
|
2010-10-14 21:42:39 +00:00
|
|
|
*/
|
2013-04-24 16:58:04 +00:00
|
|
|
fabric.Circle = fabric.util.createClass(fabric.Object, /** @lends fabric.Circle.prototype */ {
|
2012-05-25 11:34:01 +00:00
|
|
|
|
2010-10-19 20:27:24 +00:00
|
|
|
/**
|
2012-12-13 14:36:43 +00:00
|
|
|
* Type of an object
|
2010-10-19 20:27:24 +00:00
|
|
|
* @type String
|
2013-05-18 11:01:34 +00:00
|
|
|
* @default
|
2010-10-19 20:27:24 +00:00
|
|
|
*/
|
2010-06-09 22:34:55 +00:00
|
|
|
type: 'circle',
|
2012-05-25 11:34:01 +00:00
|
|
|
|
2014-01-23 15:48:37 +00:00
|
|
|
/**
|
|
|
|
|
* Radius of this circle
|
|
|
|
|
* @type Number
|
|
|
|
|
* @default
|
|
|
|
|
*/
|
|
|
|
|
radius: 0,
|
|
|
|
|
|
2014-09-17 23:23:52 +00:00
|
|
|
/**
|
|
|
|
|
* Start angle of the circle, moving clockwise
|
2018-03-31 11:50:51 +00:00
|
|
|
* deprectated type, this should be in degree, this was an oversight.
|
|
|
|
|
* probably will change to degrees in next major version
|
2014-09-17 23:23:52 +00:00
|
|
|
* @type Number
|
|
|
|
|
* @default 0
|
|
|
|
|
*/
|
|
|
|
|
startAngle: 0,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* End angle of the circle
|
2018-03-31 11:50:51 +00:00
|
|
|
* deprectated type, this should be in degree, this was an oversight.
|
|
|
|
|
* probably will change to degrees in next major version
|
2014-09-17 23:23:52 +00:00
|
|
|
* @type Number
|
|
|
|
|
* @default 2Pi
|
|
|
|
|
*/
|
2014-09-17 23:43:39 +00:00
|
|
|
endAngle: pi * 2,
|
2014-09-17 23:23:52 +00:00
|
|
|
|
2018-03-31 11:50:51 +00:00
|
|
|
cacheProperties: fabric.Object.prototype.cacheProperties.concat('radius', 'startAngle', 'endAngle'),
|
2016-12-03 21:53:51 +00:00
|
|
|
|
2013-08-30 09:51:08 +00:00
|
|
|
/**
|
|
|
|
|
* @private
|
|
|
|
|
* @param {String} key
|
2016-08-20 10:05:19 +00:00
|
|
|
* @param {*} value
|
2013-08-30 09:51:08 +00:00
|
|
|
* @return {fabric.Circle} thisArg
|
|
|
|
|
*/
|
|
|
|
|
_set: function(key, value) {
|
|
|
|
|
this.callSuper('_set', key, value);
|
|
|
|
|
|
|
|
|
|
if (key === 'radius') {
|
|
|
|
|
this.setRadius(value);
|
|
|
|
|
}
|
2012-05-25 11:34:01 +00:00
|
|
|
|
2013-08-30 09:51:08 +00:00
|
|
|
return this;
|
2010-06-09 22:34:55 +00:00
|
|
|
},
|
2012-05-25 11:34:01 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
|
|
|
|
* Returns object representation of an instance
|
2013-09-26 12:12:02 +00:00
|
|
|
* @param {Array} [propertiesToInclude] Any properties that you might want to additionally include in the output
|
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) {
|
2016-11-12 22:54:42 +00:00
|
|
|
return this.callSuper('toObject', ['radius', 'startAngle', 'endAngle'].concat(propertiesToInclude));
|
2010-06-09 22:34:55 +00:00
|
|
|
},
|
2012-05-25 11:34:01 +00:00
|
|
|
|
2013-05-09 18:19:06 +00:00
|
|
|
/* _TO_SVG_START_ */
|
2018-10-07 19:43:37 +00:00
|
|
|
|
2012-01-02 21:14:20 +00:00
|
|
|
/**
|
|
|
|
|
* Returns svg representation of an instance
|
2018-10-07 19:43:37 +00:00
|
|
|
* @return {Array} an array of strings with the specific svg representation
|
|
|
|
|
* of the instance
|
2012-01-02 21:14:20 +00:00
|
|
|
*/
|
2018-10-07 19:43:37 +00:00
|
|
|
_toSVG: function() {
|
|
|
|
|
var svgString, x = 0, y = 0,
|
2016-09-10 13:14:23 +00:00
|
|
|
angle = (this.endAngle - this.startAngle) % ( 2 * pi);
|
2014-09-17 23:23:52 +00:00
|
|
|
|
|
|
|
|
if (angle === 0) {
|
2018-10-07 19:43:37 +00:00
|
|
|
svgString = [
|
|
|
|
|
'<circle ', 'COMMON_PARTS',
|
2017-09-17 09:44:57 +00:00
|
|
|
'cx="' + x + '" cy="' + y + '" ',
|
|
|
|
|
'r="', this.radius,
|
2018-10-07 19:43:37 +00:00
|
|
|
'" />\n'
|
|
|
|
|
];
|
2014-09-17 23:43:39 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2018-02-18 14:56:27 +00:00
|
|
|
var startX = fabric.util.cos(this.startAngle) * this.radius,
|
|
|
|
|
startY = fabric.util.sin(this.startAngle) * this.radius,
|
|
|
|
|
endX = fabric.util.cos(this.endAngle) * this.radius,
|
|
|
|
|
endY = fabric.util.sin(this.endAngle) * this.radius,
|
2014-09-17 23:23:52 +00:00
|
|
|
largeFlag = angle > pi ? '1' : '0';
|
2018-10-07 19:43:37 +00:00
|
|
|
svgString = [
|
2014-09-17 23:23:52 +00:00
|
|
|
'<path d="M ' + startX + ' ' + startY,
|
|
|
|
|
' A ' + this.radius + ' ' + this.radius,
|
2016-09-10 13:14:23 +00:00
|
|
|
' 0 ', +largeFlag + ' 1', ' ' + endX + ' ' + endY,
|
2018-10-07 19:43:37 +00:00
|
|
|
'"', 'COMMON_PARTS', ' />\n'
|
|
|
|
|
];
|
2014-09-17 23:23:52 +00:00
|
|
|
}
|
2018-10-07 19:43:37 +00:00
|
|
|
return svgString;
|
2012-01-02 21:14:20 +00:00
|
|
|
},
|
2013-05-09 18:19:06 +00:00
|
|
|
/* _TO_SVG_END_ */
|
2012-05-25 11:34:01 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
|
|
|
|
* @private
|
2014-07-17 14:18:57 +00:00
|
|
|
* @param {CanvasRenderingContext2D} ctx context to render on
|
2010-06-09 22:34:55 +00:00
|
|
|
*/
|
2017-06-01 09:02:32 +00:00
|
|
|
_render: function(ctx) {
|
2010-06-09 22:34:55 +00:00
|
|
|
ctx.beginPath();
|
2017-09-17 20:47:35 +00:00
|
|
|
ctx.arc(
|
|
|
|
|
0,
|
2017-09-17 09:44:57 +00:00
|
|
|
0,
|
|
|
|
|
this.radius,
|
|
|
|
|
this.startAngle,
|
|
|
|
|
this.endAngle, false);
|
2017-09-17 20:47:35 +00:00
|
|
|
this._renderPaintInOrder(ctx);
|
2010-06-09 22:34:55 +00:00
|
|
|
},
|
2012-05-25 11:34:01 +00:00
|
|
|
|
2011-04-09 21:37:35 +00:00
|
|
|
/**
|
|
|
|
|
* Returns horizontal radius of an object (according to how an object is scaled)
|
|
|
|
|
* @return {Number}
|
|
|
|
|
*/
|
|
|
|
|
getRadiusX: function() {
|
|
|
|
|
return this.get('radius') * this.get('scaleX');
|
|
|
|
|
},
|
2012-05-25 11:34:01 +00:00
|
|
|
|
2011-04-09 21:37:35 +00:00
|
|
|
/**
|
|
|
|
|
* Returns vertical radius of an object (according to how an object is scaled)
|
|
|
|
|
* @return {Number}
|
|
|
|
|
*/
|
|
|
|
|
getRadiusY: function() {
|
|
|
|
|
return this.get('radius') * this.get('scaleY');
|
|
|
|
|
},
|
2012-05-25 11:34:01 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets radius of an object (and updates width accordingly)
|
2015-05-18 15:15:02 +00:00
|
|
|
* @return {fabric.Circle} thisArg
|
2012-05-25 11:34:01 +00:00
|
|
|
*/
|
|
|
|
|
setRadius: function(value) {
|
|
|
|
|
this.radius = value;
|
2015-05-18 15:15:02 +00:00
|
|
|
return this.set('width', value * 2).set('height', value * 2);
|
2012-05-25 11:34:01 +00:00
|
|
|
},
|
2010-06-09 22:34:55 +00:00
|
|
|
});
|
2012-05-25 11:34:01 +00:00
|
|
|
|
2013-05-30 19:53:49 +00:00
|
|
|
/* _FROM_SVG_START_ */
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
2010-10-22 02:54:00 +00:00
|
|
|
* List of attribute names to account for when parsing SVG element (used by {@link fabric.Circle.fromElement})
|
2010-10-14 21:42:39 +00:00
|
|
|
* @static
|
2013-08-08 16:31:26 +00:00
|
|
|
* @memberOf fabric.Circle
|
2010-06-09 22:34:55 +00:00
|
|
|
* @see: http://www.w3.org/TR/SVG/shapes.html#CircleElement
|
|
|
|
|
*/
|
2013-05-18 14:43:49 +00:00
|
|
|
fabric.Circle.ATTRIBUTE_NAMES = fabric.SHARED_ATTRIBUTES.concat('cx cy r'.split(' '));
|
2012-05-25 11:34:01 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
2010-10-22 02:54:00 +00:00
|
|
|
* Returns {@link fabric.Circle} instance from an SVG element
|
2010-06-09 22:34:55 +00:00
|
|
|
* @static
|
2013-08-08 16:31:26 +00:00
|
|
|
* @memberOf fabric.Circle
|
2012-12-13 14:36:43 +00:00
|
|
|
* @param {SVGElement} element Element to parse
|
2017-06-11 19:36:51 +00:00
|
|
|
* @param {Function} [callback] Options callback invoked after parsing is finished
|
2018-01-20 00:09:47 +00:00
|
|
|
* @param {Object} [options] Options object
|
2010-06-09 22:34:55 +00:00
|
|
|
* @throws {Error} If value of `r` attribute is missing or invalid
|
|
|
|
|
*/
|
2018-01-20 00:09:47 +00:00
|
|
|
fabric.Circle.fromElement = function(element, callback) {
|
2010-07-09 23:43:50 +00:00
|
|
|
var parsedAttributes = fabric.parseAttributes(element, fabric.Circle.ATTRIBUTE_NAMES);
|
2014-06-24 12:12:17 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
if (!isValidRadius(parsedAttributes)) {
|
2012-10-14 00:53:12 +00:00
|
|
|
throw new Error('value of `r` attribute is required and can not be negative');
|
2010-06-09 22:34:55 +00:00
|
|
|
}
|
2014-06-24 12:12:17 +00:00
|
|
|
|
2017-06-01 09:02:32 +00:00
|
|
|
parsedAttributes.left = (parsedAttributes.left || 0) - parsedAttributes.radius;
|
|
|
|
|
parsedAttributes.top = (parsedAttributes.top || 0) - parsedAttributes.radius;
|
2018-01-20 00:09:47 +00:00
|
|
|
callback(new fabric.Circle(parsedAttributes));
|
2010-06-09 22:34:55 +00:00
|
|
|
};
|
2012-05-25 11:34:01 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
function isValidRadius(attributes) {
|
2015-02-02 23:02:16 +00:00
|
|
|
return (('radius' in attributes) && (attributes.radius >= 0));
|
2010-06-09 22:34:55 +00:00
|
|
|
}
|
2013-05-30 19:53:49 +00:00
|
|
|
/* _FROM_SVG_END_ */
|
2012-05-25 11:34:01 +00:00
|
|
|
|
2010-06-09 22:34:55 +00:00
|
|
|
/**
|
2010-10-22 02:54:00 +00:00
|
|
|
* Returns {@link fabric.Circle} instance from an object representation
|
2010-06-09 22:34:55 +00:00
|
|
|
* @static
|
2013-08-08 16:31:26 +00:00
|
|
|
* @memberOf fabric.Circle
|
2010-10-14 21:42:39 +00:00
|
|
|
* @param {Object} object Object to create an instance from
|
2016-09-10 14:09:17 +00:00
|
|
|
* @param {function} [callback] invoked with new instance as first argument
|
2010-10-14 21:42:39 +00:00
|
|
|
* @return {Object} Instance of fabric.Circle
|
2010-06-09 22:34:55 +00:00
|
|
|
*/
|
2017-06-11 19:36:51 +00:00
|
|
|
fabric.Circle.fromObject = function(object, callback) {
|
|
|
|
|
return fabric.Object._fromObject('Circle', object, callback);
|
2011-02-06 07:58:32 +00:00
|
|
|
};
|
2012-05-25 11:34:01 +00:00
|
|
|
|
2013-04-22 13:16:58 +00:00
|
|
|
})(typeof exports !== 'undefined' ? exports : this);
|