ellipse.class.js
4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
(function(global) {
'use strict';
var fabric = global.fabric || (global.fabric = { }),
piBy2 = Math.PI * 2;
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,
cacheProperties: fabric.Object.prototype.cacheProperties.concat('rx', 'ry'),
/**
* Constructor
* @param {Object} [options] Options object
* @return {fabric.Ellipse} thisArg
*/
initialize: function(options) {
this.callSuper('initialize', options);
this.set('rx', options && options.rx || 0);
this.set('ry', options && options.ry || 0);
},
/**
* @private
* @param {String} key
* @param {*} 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 this.callSuper('toObject', ['rx', 'ry'].concat(propertiesToInclude));
},
/* _TO_SVG_START_ */
/**
* Returns svg representation of an instance
* @return {Array} an array of strings with the specific svg representation
* of the instance
*/
_toSVG: function() {
return [
'<ellipse ', 'COMMON_PARTS',
'cx="0" cy="0" ',
'rx="', this.rx,
'" ry="', this.ry,
'" />\n'
];
},
/* _TO_SVG_END_ */
/**
* @private
* @param {CanvasRenderingContext2D} ctx context to render on
*/
_render: function(ctx) {
ctx.beginPath();
ctx.save();
ctx.transform(1, 0, 0, this.ry / this.rx, 0, 0);
ctx.arc(
0,
0,
this.rx,
0,
piBy2,
false);
ctx.restore();
this._renderPaintInOrder(ctx);
},
});
/* _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 {Function} [callback] Options callback invoked after parsing is finished
* @return {fabric.Ellipse}
*/
fabric.Ellipse.fromElement = function(element, callback) {
var parsedAttributes = fabric.parseAttributes(element, fabric.Ellipse.ATTRIBUTE_NAMES);
parsedAttributes.left = (parsedAttributes.left || 0) - parsedAttributes.rx;
parsedAttributes.top = (parsedAttributes.top || 0) - parsedAttributes.ry;
callback(new fabric.Ellipse(parsedAttributes));
};
/* _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
* @param {function} [callback] invoked with new instance as first argument
* @return {fabric.Ellipse}
*/
fabric.Ellipse.fromObject = function(object, callback) {
return fabric.Object._fromObject('Ellipse', object, callback);
};
})(typeof exports !== 'undefined' ? exports : this);