fabric.js/src/shapes/ellipse.class.js
2014-09-29 13:54:19 +02:00

211 lines
5.4 KiB
JavaScript

(function(global) {
'use strict';
var fabric = global.fabric || (global.fabric = { }),
piBy2 = Math.PI * 2,
extend = fabric.util.object.extend;
if (fabric.Ellipse) {
fabric.warn('fabric.Ellipse is already defined.');
return;
}
/**
* Ellipse class
* @class fabric.Ellipse
* @extends fabric.Object
* @return {fabric.Ellipse} thisArg
* @see {@link fabric.Ellipse#initialize} for constructor definition
*/
fabric.Ellipse = fabric.util.createClass(fabric.Object, /** @lends fabric.Ellipse.prototype */ {
/**
* Type of an object
* @type String
* @default
*/
type: 'ellipse',
/**
* Horizontal radius
* @type Number
* @default
*/
rx: 0,
/**
* Vertical radius
* @type Number
* @default
*/
ry: 0,
/**
* Constructor
* @param {Object} [options] Options object
* @return {fabric.Ellipse} thisArg
*/
initialize: function(options) {
options = options || { };
this.callSuper('initialize', options);
this.set('rx', options.rx || 0);
this.set('ry', options.ry || 0);
},
/**
* @private
* @param {String} key
* @param {Any} value
* @return {fabric.Ellipse} thisArg
*/
_set: function(key, value) {
this.callSuper('_set', key, value);
switch (key) {
case 'rx':
this.rx = value;
this.set('width', value * 2);
break;
case 'ry':
this.ry = value;
this.set('height', value * 2);
break;
}
return this;
},
/**
* Returns horizontal radius of an object (according to how an object is scaled)
* @return {Number}
*/
getRx: function() {
return this.get('rx') * this.get('scaleX');
},
/**
* Returns Vertical radius of an object (according to how an object is scaled)
* @return {Number}
*/
getRy: function() {
return this.get('ry') * this.get('scaleY');
},
/**
* 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 extend(this.callSuper('toObject', propertiesToInclude), {
rx: this.get('rx'),
ry: this.get('ry')
});
},
/* _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;
if (this.group && this.group.type === 'path-group') {
x = this.left + this.rx;
y = this.top + this.ry;
}
markup.push(
'<ellipse ',
'cx="', x, '" cy="', y, '" ',
'rx="', this.rx,
'" ry="', this.ry,
'" 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
* @param {Boolean} [noTransform] When true, context is not transformed
*/
_render: function(ctx, noTransform) {
ctx.beginPath();
ctx.save();
ctx.transform(1, 0, 0, this.ry/this.rx, 0, 0);
ctx.arc(
noTransform ? this.left + this.rx : 0,
noTransform ? (this.top + this.ry) * this.rx/this.ry : 0,
this.rx,
0,
piBy2,
false);
ctx.restore();
this._renderFill(ctx);
this._renderStroke(ctx);
},
/**
* Returns complexity of an instance
* @return {Number} complexity
*/
complexity: function() {
return 1;
}
});
/* _FROM_SVG_START_ */
/**
* List of attribute names to account for when parsing SVG element (used by {@link fabric.Ellipse.fromElement})
* @static
* @memberOf fabric.Ellipse
* @see http://www.w3.org/TR/SVG/shapes.html#EllipseElement
*/
fabric.Ellipse.ATTRIBUTE_NAMES = fabric.SHARED_ATTRIBUTES.concat('cx cy rx ry'.split(' '));
/**
* Returns {@link fabric.Ellipse} instance from an SVG element
* @static
* @memberOf fabric.Ellipse
* @param {SVGElement} element Element to parse
* @param {Object} [options] Options object
* @return {fabric.Ellipse}
*/
fabric.Ellipse.fromElement = function(element, options) {
options || (options = { });
var parsedAttributes = fabric.parseAttributes(element, fabric.Ellipse.ATTRIBUTE_NAMES);
parsedAttributes.left = parsedAttributes.left || 0;
parsedAttributes.top = parsedAttributes.top || 0;
var ellipse = new fabric.Ellipse(extend(parsedAttributes, options));
ellipse.top -= ellipse.ry;
ellipse.left -= ellipse.rx;
return ellipse;
};
/* _FROM_SVG_END_ */
/**
* Returns {@link fabric.Ellipse} instance from an object representation
* @static
* @memberOf fabric.Ellipse
* @param {Object} object Object to create an instance from
* @return {fabric.Ellipse}
*/
fabric.Ellipse.fromObject = function(object) {
return new fabric.Ellipse(object);
};
})(typeof exports !== 'undefined' ? exports : this);