2010-07-10 01:50:13 +00:00
|
|
|
//= require "object.class"
|
2010-06-11 23:37:06 +00:00
|
|
|
|
2010-06-10 17:57:59 +00:00
|
|
|
(function() {
|
2010-06-09 22:34:55 +00:00
|
|
|
|
2010-07-26 23:24:16 +00:00
|
|
|
var fabric = this.fabric || (this.fabric = { }),
|
2010-07-26 23:20:19 +00:00
|
|
|
piBy2 = Math.PI * 2,
|
|
|
|
|
extend = fabric.util.object.extend;
|
2010-06-09 22:34:55 +00:00
|
|
|
|
2010-07-09 23:43:50 +00:00
|
|
|
if (fabric.Circle) {
|
|
|
|
|
console.warn('fabric.Circle is already defined.');
|
2010-06-10 17:57:59 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2010-06-09 22:34:55 +00:00
|
|
|
|
2010-07-10 01:50:13 +00:00
|
|
|
fabric.Circle = fabric.util.createClass(fabric.Object, /** @lends fabric.Circle.prototype */ {
|
2010-06-09 22:34:55 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @field
|
|
|
|
|
*/
|
|
|
|
|
type: 'circle',
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @constructs
|
|
|
|
|
* @method initialize
|
|
|
|
|
* @param options {Object} options object
|
|
|
|
|
* @return {Object} thisArg
|
|
|
|
|
*/
|
|
|
|
|
initialize: function(options) {
|
|
|
|
|
options = options || { };
|
|
|
|
|
|
|
|
|
|
this.set('radius', options.radius || 0);
|
|
|
|
|
this.callSuper('initialize', options);
|
|
|
|
|
|
|
|
|
|
var radiusBy2ByScale = this.get('radius') * 2 * this.get('scaleX');
|
|
|
|
|
this.set('width', radiusBy2ByScale).set('height', radiusBy2ByScale);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns object representation of an instance
|
|
|
|
|
* @method toObject
|
|
|
|
|
* @return {Object} object representation of an instance
|
|
|
|
|
*/
|
|
|
|
|
toObject: function() {
|
2010-07-26 23:20:19 +00:00
|
|
|
return extend(this.callSuper('toObject'), {
|
2010-06-09 22:34:55 +00:00
|
|
|
radius: this.get('radius')
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @private
|
|
|
|
|
* @method _render
|
|
|
|
|
* @param ctx {CanvasRenderingContext2D} context to render on
|
|
|
|
|
*/
|
|
|
|
|
_render: function(ctx) {
|
|
|
|
|
ctx.beginPath();
|
2010-09-09 03:07:52 +00:00
|
|
|
ctx.arc(this.left, this.top, this.radius, 0, piBy2, false);
|
2010-08-02 18:50:07 +00:00
|
|
|
ctx.closePath();
|
2010-06-09 22:34:55 +00:00
|
|
|
if (this.fill) {
|
|
|
|
|
ctx.fill();
|
|
|
|
|
}
|
|
|
|
|
if (this.stroke) {
|
|
|
|
|
ctx.stroke();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns complexity of an instance
|
|
|
|
|
* @method complexity
|
|
|
|
|
* @return {Number} complexity of this instance
|
|
|
|
|
*/
|
|
|
|
|
complexity: function() {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see: http://www.w3.org/TR/SVG/shapes.html#CircleElement
|
|
|
|
|
*/
|
2010-07-09 23:43:50 +00:00
|
|
|
fabric.Circle.ATTRIBUTE_NAMES = 'cx cy r fill fill-opacity stroke stroke-width transform'.split(' ');
|
2010-06-09 22:34:55 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @static
|
2010-07-09 23:43:50 +00:00
|
|
|
* @method fabric.Circle.fromElement
|
2010-06-09 22:34:55 +00:00
|
|
|
* @param element {SVGElement} element to parse
|
|
|
|
|
* @param options {Object} options object
|
|
|
|
|
* @throws {Error} If value of `r` attribute is missing or invalid
|
2010-07-09 23:43:50 +00:00
|
|
|
* @return {Object} instance of fabric.Circle
|
2010-06-09 22:34:55 +00:00
|
|
|
*/
|
2010-07-09 23:43:50 +00:00
|
|
|
fabric.Circle.fromElement = function(element, options) {
|
2010-09-09 03:07:52 +00:00
|
|
|
options || (options = { });
|
2010-07-09 23:43:50 +00:00
|
|
|
var parsedAttributes = fabric.parseAttributes(element, fabric.Circle.ATTRIBUTE_NAMES);
|
2010-06-09 22:34:55 +00:00
|
|
|
if (!isValidRadius(parsedAttributes)) {
|
|
|
|
|
throw Error('value of `r` attribute is required and can not be negative');
|
|
|
|
|
}
|
2010-09-09 03:07:52 +00:00
|
|
|
if ('left' in parsedAttributes) {
|
|
|
|
|
parsedAttributes.left -= (options.width / 2) || 0;
|
|
|
|
|
}
|
|
|
|
|
if ('top' in parsedAttributes) {
|
|
|
|
|
parsedAttributes.top -= (options.height / 2) || 0;
|
|
|
|
|
}
|
2010-07-26 23:20:19 +00:00
|
|
|
return new fabric.Circle(extend(parsedAttributes, options));
|
2010-06-09 22:34:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
function isValidRadius(attributes) {
|
|
|
|
|
return (('radius' in attributes) && (attributes.radius > 0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @static
|
2010-07-09 23:43:50 +00:00
|
|
|
* @method fabric.Circle.fromObject
|
2010-06-09 22:34:55 +00:00
|
|
|
* @param object {Object} object to create an instance from
|
2010-07-09 23:43:50 +00:00
|
|
|
* @return {Object} instance of fabric.Circle
|
2010-06-09 22:34:55 +00:00
|
|
|
*/
|
2010-07-09 23:43:50 +00:00
|
|
|
fabric.Circle.fromObject = function(object) {
|
|
|
|
|
return new fabric.Circle(object);
|
2010-06-09 22:34:55 +00:00
|
|
|
}
|
|
|
|
|
})();
|