circle.class.js
5.85 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
(function(global) {
'use strict';
var fabric = global.fabric || (global.fabric = { }),
pi = Math.PI;
if (fabric.Circle) {
fabric.warn('fabric.Circle is already defined.');
return;
}
/**
* 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
* deprectated type, this should be in degree, this was an oversight.
* probably will change to degrees in next major version
* @type Number
* @default 0
*/
startAngle: 0,
/**
* End angle of the circle
* deprectated type, this should be in degree, this was an oversight.
* probably will change to degrees in next major version
* @type Number
* @default 2Pi
*/
endAngle: pi * 2,
cacheProperties: fabric.Object.prototype.cacheProperties.concat('radius', 'startAngle', '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;
},
/**
* 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
* @return {Array} an array of strings with the specific svg representation
* of the instance
*/
_toSVG: function() {
var svgString, x = 0, y = 0,
angle = (this.endAngle - this.startAngle) % ( 2 * pi);
if (angle === 0) {
svgString = [
'<circle ', 'COMMON_PARTS',
'cx="' + x + '" cy="' + y + '" ',
'r="', this.radius,
'" />\n'
];
}
else {
var startX = fabric.util.cos(this.startAngle) * this.radius,
startY = fabric.util.sin(this.startAngle) * this.radius,
endX = fabric.util.cos(this.endAngle) * this.radius,
endY = fabric.util.sin(this.endAngle) * this.radius,
largeFlag = angle > pi ? '1' : '0';
svgString = [
'<path d="M ' + startX + ' ' + startY,
' A ' + this.radius + ' ' + this.radius,
' 0 ', +largeFlag + ' 1', ' ' + endX + ' ' + endY,
'" ', 'COMMON_PARTS', ' />\n'
];
}
return svgString;
},
/* _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._renderPaintInOrder(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 {Function} [callback] Options callback invoked after parsing is finished
* @param {Object} [options] Options object
* @throws {Error} If value of `r` attribute is missing or invalid
*/
fabric.Circle.fromElement = function(element, callback) {
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;
callback(new fabric.Circle(parsedAttributes));
};
/**
* @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);