fabric.js/src/shapes/circle.class.js

248 lines
6.8 KiB
JavaScript
Raw Normal View History

(function(global) {
'use strict';
var fabric = global.fabric || (global.fabric = { }),
pi = Math.PI,
extend = fabric.util.object.extend;
if (fabric.Circle) {
fabric.warn('fabric.Circle is already defined.');
return;
}
/**
2012-12-13 14:36:43 +00:00
* Circle class
2013-04-25 18:21:32 +00:00
* @class fabric.Circle
* @extends fabric.Object
2013-10-05 18:21:28 +00:00
* @see {@link fabric.Circle#initialize} for constructor definition
*/
fabric.Circle = fabric.util.createClass(fabric.Object, /** @lends fabric.Circle.prototype */ {
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
* @default
2010-10-19 20:27:24 +00:00
*/
2010-06-09 22:34:55 +00:00
type: 'circle',
/**
* Radius of this circle
* @type Number
* @default
*/
radius: 0,
/**
* Start angle of the circle, moving clockwise
* @type Number
* @default 0
*/
startAngle: 0,
/**
* End angle of the circle
* @type Number
* @default 2Pi
*/
endAngle: pi * 2,
2010-06-09 22:34:55 +00:00
/**
* Constructor
* @param {Object} [options] Options object
* @return {fabric.Circle} thisArg
2010-06-09 22:34:55 +00:00
*/
initialize: function(options) {
options = options || { };
2010-06-09 22:34:55 +00:00
this.callSuper('initialize', options);
2014-07-26 13:21:48 +00:00
this.set('radius', options.radius || 0);
this.startAngle = options.startAngle || this.startAngle;
this.endAngle = options.endAngle || this.endAngle;
},
/**
* @private
* @param {String} key
* @param {*} value
* @return {fabric.Circle} thisArg
*/
_set: function(key, value) {
this.callSuper('_set', key, value);
if (key === 'radius') {
this.setRadius(value);
}
return this;
2010-06-09 22:34:55 +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
*/
toObject: function(propertiesToInclude) {
return extend(this.callSuper('toObject', propertiesToInclude), {
radius: this.get('radius'),
2014-09-17 23:39:15 +00:00
startAngle: this.startAngle,
endAngle: this.endAngle
2010-06-09 22:34:55 +00:00
});
},
/* _TO_SVG_START_ */
/**
* Returns svg representation of an instance
* @param {Function} [reviver] Method for further parsing of svg representation.
2012-12-15 16:05:23 +00:00
* @return {String} svg representation of an instance
*/
toSVG: function(reviver) {
var markup = this._createBaseSVGMarkup(), x = 0, y = 0,
angle = (this.endAngle - this.startAngle) % ( 2 * pi);
if (angle === 0) {
if (this.group && this.group.type === 'path-group') {
x = this.left + this.radius;
y = this.top + this.radius;
}
markup.push(
2016-05-21 13:07:04 +00:00
'<circle ', this.getSvgId(),
'cx="' + x + '" cy="' + y + '" ',
'r="', this.radius,
'" style="', this.getSvgStyles(),
'" transform="', this.getSvgTransform(),
' ', this.getSvgTransformMatrix(),
'"/>\n'
);
}
else {
var startX = Math.cos(this.startAngle) * this.radius,
startY = Math.sin(this.startAngle) * this.radius,
endX = Math.cos(this.endAngle) * this.radius,
endY = Math.sin(this.endAngle) * this.radius,
largeFlag = angle > pi ? '1' : '0';
markup.push(
'<path d="M ' + startX + ' ' + startY,
' A ' + this.radius + ' ' + this.radius,
' 0 ', +largeFlag + ' 1', ' ' + endX + ' ' + endY,
'" style="', this.getSvgStyles(),
'" transform="', this.getSvgTransform(),
' ', this.getSvgTransformMatrix(),
'"/>\n'
);
}
return reviver ? reviver(markup.join('')) : markup.join('');
},
/* _TO_SVG_END_ */
2010-06-09 22:34:55 +00:00
/**
* @private
2014-07-17 14:18:57 +00:00
* @param {CanvasRenderingContext2D} ctx context to render on
* @param {Boolean} [noTransform] When true, context is not transformed
2010-06-09 22:34:55 +00:00
*/
_render: function(ctx, noTransform) {
2010-06-09 22:34:55 +00:00
ctx.beginPath();
ctx.arc(noTransform ? this.left + this.radius : 0,
noTransform ? this.top + this.radius : 0,
this.radius,
this.startAngle,
this.endAngle, false);
this._renderFill(ctx);
this._renderStroke(ctx);
2010-06-09 22:34:55 +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');
},
/**
* Returns vertical radius of an object (according to how an object is scaled)
* @return {Number}
*/
getRadiusY: function() {
return this.get('radius') * this.get('scaleY');
},
/**
* Sets radius of an object (and updates width accordingly)
* @return {fabric.Circle} thisArg
*/
setRadius: function(value) {
this.radius = value;
return this.set('width', value * 2).set('height', value * 2);
},
2010-06-09 22:34:55 +00:00
/**
* Returns complexity of an instance
* @return {Number} complexity of this instance
*/
complexity: function() {
return 1;
}
});
/* _FROM_SVG_START_ */
2010-06-09 22:34:55 +00:00
/**
* List of attribute names to account for when parsing SVG element (used by {@link fabric.Circle.fromElement})
* @static
* @memberOf fabric.Circle
2010-06-09 22:34:55 +00:00
* @see: http://www.w3.org/TR/SVG/shapes.html#CircleElement
*/
fabric.Circle.ATTRIBUTE_NAMES = fabric.SHARED_ATTRIBUTES.concat('cx cy r'.split(' '));
2010-06-09 22:34:55 +00:00
/**
* Returns {@link fabric.Circle} instance from an SVG element
2010-06-09 22:34:55 +00:00
* @static
* @memberOf fabric.Circle
2012-12-13 14:36:43 +00:00
* @param {SVGElement} element Element to parse
* @param {Object} [options] Options object
2010-06-09 22:34:55 +00:00
* @throws {Error} If value of `r` attribute is missing or invalid
* @return {fabric.Circle} Instance of fabric.Circle
2010-06-09 22:34:55 +00:00
*/
fabric.Circle.fromElement = function(element, options) {
options || (options = { });
2014-06-24 12:12:17 +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)) {
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
parsedAttributes.left = parsedAttributes.left || 0;
parsedAttributes.top = parsedAttributes.top || 0;
2014-06-24 12:12:17 +00:00
var obj = new fabric.Circle(extend(parsedAttributes, options));
obj.left -= obj.radius;
obj.top -= obj.radius;
return obj;
2010-06-09 22:34:55 +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
}
/* _FROM_SVG_END_ */
2010-06-09 22:34:55 +00:00
/**
* Returns {@link fabric.Circle} instance from an object representation
2010-06-09 22:34:55 +00:00
* @static
* @memberOf fabric.Circle
* @param {Object} object Object to create an instance from
* @return {Object} Instance of fabric.Circle
2010-06-09 22:34:55 +00:00
*/
fabric.Circle.fromObject = function(object) {
return new fabric.Circle(object);
};
})(typeof exports !== 'undefined' ? exports : this);