mirror of
https://github.com/Hopiu/fabric.js.git
synced 2026-05-24 21:23:44 +00:00
230 lines
6.5 KiB
JavaScript
230 lines
6.5 KiB
JavaScript
(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;
|
|
}
|
|
|
|
var cacheProperties = fabric.Object.prototype.cacheProperties.concat();
|
|
cacheProperties.push(
|
|
'radius'
|
|
);
|
|
|
|
/**
|
|
* Circle class
|
|
* @class fabric.Circle
|
|
* @extends fabric.Object
|
|
* @see {@link fabric.Circle#initialize} for constructor definition
|
|
*/
|
|
fabric.Circle = fabric.util.createClass(fabric.Object, /** @lends fabric.Circle.prototype */ {
|
|
|
|
/**
|
|
* Type of an object
|
|
* @type String
|
|
* @default
|
|
*/
|
|
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,
|
|
|
|
cacheProperties: cacheProperties,
|
|
|
|
/**
|
|
* Constructor
|
|
* @param {Object} [options] Options object
|
|
* @return {fabric.Circle} thisArg
|
|
*/
|
|
initialize: function(options) {
|
|
this.callSuper('initialize', options);
|
|
this.set('radius', options && options.radius || 0);
|
|
},
|
|
|
|
/**
|
|
* @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;
|
|
},
|
|
|
|
/**
|
|
* Returns object representation of an instance
|
|
* @param {Array} [propertiesToInclude] Any properties that you might want to additionally include in the output
|
|
* @return {Object} object representation of an instance
|
|
*/
|
|
toObject: function(propertiesToInclude) {
|
|
return this.callSuper('toObject', ['radius', 'startAngle', 'endAngle'].concat(propertiesToInclude));
|
|
},
|
|
|
|
/* _TO_SVG_START_ */
|
|
/**
|
|
* Returns svg representation of an instance
|
|
* @param {Function} [reviver] Method for further parsing of svg representation.
|
|
* @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) {
|
|
markup.push(
|
|
'<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_ */
|
|
|
|
/**
|
|
* @private
|
|
* @param {CanvasRenderingContext2D} ctx context to render on
|
|
*/
|
|
_render: function(ctx) {
|
|
ctx.beginPath();
|
|
ctx.arc(0,
|
|
0,
|
|
this.radius,
|
|
this.startAngle,
|
|
this.endAngle, false);
|
|
this._renderFill(ctx);
|
|
this._renderStroke(ctx);
|
|
},
|
|
|
|
/**
|
|
* 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);
|
|
},
|
|
});
|
|
|
|
/* _FROM_SVG_START_ */
|
|
/**
|
|
* List of attribute names to account for when parsing SVG element (used by {@link fabric.Circle.fromElement})
|
|
* @static
|
|
* @memberOf fabric.Circle
|
|
* @see: http://www.w3.org/TR/SVG/shapes.html#CircleElement
|
|
*/
|
|
fabric.Circle.ATTRIBUTE_NAMES = fabric.SHARED_ATTRIBUTES.concat('cx cy r'.split(' '));
|
|
|
|
/**
|
|
* Returns {@link fabric.Circle} instance from an SVG element
|
|
* @static
|
|
* @memberOf fabric.Circle
|
|
* @param {SVGElement} element Element to parse
|
|
* @param {Object} [options] Options object
|
|
* @param {Function} [callback] Options callback invoked after parsing is finished
|
|
* @throws {Error} If value of `r` attribute is missing or invalid
|
|
*/
|
|
fabric.Circle.fromElement = function(element, callback, options) {
|
|
options || (options = { });
|
|
|
|
var parsedAttributes = fabric.parseAttributes(element, fabric.Circle.ATTRIBUTE_NAMES);
|
|
|
|
if (!isValidRadius(parsedAttributes)) {
|
|
throw new Error('value of `r` attribute is required and can not be negative');
|
|
}
|
|
|
|
parsedAttributes.left = (parsedAttributes.left || 0) - parsedAttributes.radius;
|
|
parsedAttributes.top = (parsedAttributes.top || 0) - parsedAttributes.radius;
|
|
parsedAttributes.originX = 'left';
|
|
parsedAttributes.originY = 'top';
|
|
callback(new fabric.Circle(extend(parsedAttributes, options)));
|
|
};
|
|
|
|
/**
|
|
* @private
|
|
*/
|
|
function isValidRadius(attributes) {
|
|
return (('radius' in attributes) && (attributes.radius >= 0));
|
|
}
|
|
/* _FROM_SVG_END_ */
|
|
|
|
/**
|
|
* Returns {@link fabric.Circle} instance from an object representation
|
|
* @static
|
|
* @memberOf fabric.Circle
|
|
* @param {Object} object Object to create an instance from
|
|
* @param {function} [callback] invoked with new instance as first argument
|
|
* @return {Object} Instance of fabric.Circle
|
|
*/
|
|
fabric.Circle.fromObject = function(object, callback) {
|
|
return fabric.Object._fromObject('Circle', object, callback);
|
|
};
|
|
|
|
})(typeof exports !== 'undefined' ? exports : this);
|