forked from fabricjs/fabric.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_group.class.js
239 lines (208 loc) · 5.91 KB
/
path_group.class.js
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
(function(global) {
"use strict";
var fabric = global.fabric || (global.fabric = { }),
extend = fabric.util.object.extend,
invoke = fabric.util.array.invoke,
parentToObject = fabric.Object.prototype.toObject,
camelize = fabric.util.string.camelize,
capitalize = fabric.util.string.capitalize;
if (fabric.PathGroup) {
fabric.warn('fabric.PathGroup is already defined');
return;
}
/**
* @class PathGroup
* @extends fabric.Path
*/
fabric.PathGroup = fabric.util.createClass(fabric.Path, /** @scope fabric.PathGroup.prototype */ {
/**
* @property
* @type String
*/
type: 'path-group',
/**
* @property
* @type String
*/
fill: '',
/**
* @property
* @type Boolean
*/
forceFillOverwrite: false,
/**
* Constructor
* @method initialize
* @param {Array} paths
* @param {Object} [options] Options object
* @return {fabric.PathGroup} thisArg
*/
initialize: function(paths, options) {
options = options || { };
this.paths = paths || [ ];
for (var i = this.paths.length; i--; ) {
this.paths[i].group = this;
}
this.setOptions(options);
this.setCoords();
if (options.sourcePath) {
this.setSourcePath(options.sourcePath);
}
},
/**
* Renders this group on a specified context
* @method render
* @param {CanvasRenderingContext2D} ctx Context to render this instance on
*/
render: function(ctx) {
ctx.save();
var m = this.transformMatrix;
if (m) {
ctx.transform(m[0], m[1], m[2], m[3], m[4], m[5]);
}
this.transform(ctx);
for (var i = 0, l = this.paths.length; i < l; ++i) {
this.paths[i].render(ctx, true);
}
if (this.active) {
this.drawBorders(ctx);
this.hideCorners || this.drawCorners(ctx);
}
ctx.restore();
},
/**
* Sets certain property to a certain value
* @method _set
* @param {String} prop
* @param {Any} value
* @return {fabric.PathGroup} thisArg
*/
_set: function(prop, value) {
if ((prop === 'fill' || prop === 'overlayFill') && value && this.isSameColor()) {
var i = this.paths.length;
while (i--) {
this.paths[i]._set(prop, value);
}
}
return this.callSuper('_set', prop, value);
},
/**
* Returns object representation of this path group
* @method toObject
* @return {Object} object representation of an instance
*/
toObject: function() {
return extend(parentToObject.call(this), {
paths: invoke(this.getObjects(), 'toObject'),
sourcePath: this.sourcePath
});
},
/**
* Returns dataless object representation of this path group
* @method toDatalessObject
* @return {Object} dataless object representation of an instance
*/
toDatalessObject: function() {
var o = this.toObject();
if (this.sourcePath) {
o.paths = this.sourcePath;
}
return o;
},
/**
* Returns svg representation of an instance
* @method toSVG
* @return {string} svg representation of an instance
*/
toSVG: function() {
var objects = this.getObjects();
var markup = [
'<g ',
'width="', this.width, '" ',
'height="', this.height, '" ',
'style="', this.getSvgStyles(), '" ',
'transform="', this.getSvgTransform(), '" ',
'>'
];
for (var i = 0, len = objects.length; i < len; i++) {
markup.push(objects[i].toSVG());
}
markup.push('</g>');
return markup.join('');
},
/**
* Returns a string representation of this path group
* @method toString
* @return {String} string representation of an object
*/
toString: function() {
return '#<fabric.PathGroup (' + this.complexity() +
'): { top: ' + this.top + ', left: ' + this.left + ' }>';
},
/**
* Returns true if all paths in this group are of same color
* @method isSameColor
* @return {Boolean} true if all paths are of the same color (`fill`)
*/
isSameColor: function() {
var firstPathFill = this.getObjects()[0].get('fill');
return this.getObjects().every(function(path) {
return path.get('fill') === firstPathFill;
});
},
/**
* Returns number representation of object's complexity
* @method complexity
* @return {Number} complexity
*/
complexity: function() {
return this.paths.reduce(function(total, path) {
return total + ((path && path.complexity) ? path.complexity() : 0);
}, 0);
},
/**
* Makes path group grayscale
* @method toGrayscale
* @return {fabric.PathGroup} thisArg
*/
toGrayscale: function() {
var i = this.paths.length;
while (i--) {
this.paths[i].toGrayscale();
}
return this;
},
/**
* Returns all paths in this path group
* @method getObjects
* @return {Array} array of path objects included in this path group
*/
getObjects: function() {
return this.paths;
}
});
/**
* @private
* @method instantiatePaths
*/
function instantiatePaths(paths) {
for (var i = 0, len = paths.length; i < len; i++) {
if (!(paths[i] instanceof fabric.Object)) {
var klassName = camelize(capitalize(paths[i].type));
paths[i] = fabric[klassName].fromObject(paths[i]);
}
}
return paths;
}
/**
* Creates fabric.PathGroup instance from an object representation
* @static
* @method fabric.PathGroup.fromObject
* @param {Object} object
* @return {fabric.PathGroup}
*/
fabric.PathGroup.fromObject = function(object) {
var paths = instantiatePaths(object.paths);
return new fabric.PathGroup(paths, object);
};
})(typeof exports !== 'undefined' ? exports : this);