-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Copy pathSVGShapeElement.js
366 lines (342 loc) · 12.4 KB
/
SVGShapeElement.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import {
extendPrototype,
} from '../../utils/functionExtensions';
import { getLocationHref } from '../../main';
import ShapePropertyFactory from '../../utils/shapes/ShapeProperty';
import BaseElement from '../BaseElement';
import TransformElement from '../helpers/TransformElement';
import SVGBaseElement from './SVGBaseElement';
import HierarchyElement from '../helpers/HierarchyElement';
import FrameElement from '../helpers/FrameElement';
import RenderableDOMElement from '../helpers/RenderableDOMElement';
import getBlendMode from '../../utils/helpers/blendModes';
import Matrix from '../../3rd_party/transformation-matrix';
import IShapeElement from '../ShapeElement';
import TransformPropertyFactory from '../../utils/TransformProperty';
import { ShapeModifiers } from '../../utils/shapes/ShapeModifiers';
import {
lineCapEnum,
lineJoinEnum,
} from '../../utils/helpers/shapeEnums';
import SVGShapeData from '../helpers/shapes/SVGShapeData';
import SVGStyleData from '../helpers/shapes/SVGStyleData';
import SVGStrokeStyleData from '../helpers/shapes/SVGStrokeStyleData';
import SVGFillStyleData from '../helpers/shapes/SVGFillStyleData';
import SVGNoStyleData from '../helpers/shapes/SVGNoStyleData';
import SVGGradientFillStyleData from '../helpers/shapes/SVGGradientFillStyleData';
import SVGGradientStrokeStyleData from '../helpers/shapes/SVGGradientStrokeStyleData';
import ShapeGroupData from '../helpers/shapes/ShapeGroupData';
import SVGTransformData from '../helpers/shapes/SVGTransformData';
import SVGElementsRenderer from '../helpers/shapes/SVGElementsRenderer';
function SVGShapeElement(data, globalData, comp) {
// List of drawable elements
this.shapes = [];
// Full shape data
this.shapesData = data.shapes;
// List of styles that will be applied to shapes
this.stylesList = [];
// List of modifiers that will be applied to shapes
this.shapeModifiers = [];
// List of items in shape tree
this.itemsData = [];
// List of items in previous shape tree
this.processedElements = [];
// List of animated components
this.animatedContents = [];
this.initElement(data, globalData, comp);
// Moving any property that doesn't get too much access after initialization because of v8 way of handling more than 10 properties.
// List of elements that have been created
this.prevViewData = [];
// Moving any property that doesn't get too much access after initialization because of v8 way of handling more than 10 properties.
}
extendPrototype([BaseElement, TransformElement, SVGBaseElement, IShapeElement, HierarchyElement, FrameElement, RenderableDOMElement], SVGShapeElement);
SVGShapeElement.prototype.initSecondaryElement = function () {
};
SVGShapeElement.prototype.identityMatrix = new Matrix();
SVGShapeElement.prototype.buildExpressionInterface = function () {};
SVGShapeElement.prototype.createContent = function () {
this.searchShapes(this.shapesData, this.itemsData, this.prevViewData, this.layerElement, 0, [], true);
this.filterUniqueShapes();
};
/*
This method searches for multiple shapes that affect a single element and one of them is animated
*/
SVGShapeElement.prototype.filterUniqueShapes = function () {
var i;
var len = this.shapes.length;
var shape;
var j;
var jLen = this.stylesList.length;
var style;
var tempShapes = [];
var areAnimated = false;
for (j = 0; j < jLen; j += 1) {
style = this.stylesList[j];
areAnimated = false;
tempShapes.length = 0;
for (i = 0; i < len; i += 1) {
shape = this.shapes[i];
if (shape.styles.indexOf(style) !== -1) {
tempShapes.push(shape);
areAnimated = shape._isAnimated || areAnimated;
}
}
if (tempShapes.length > 1 && areAnimated) {
this.setShapesAsAnimated(tempShapes);
}
}
};
SVGShapeElement.prototype.setShapesAsAnimated = function (shapes) {
var i;
var len = shapes.length;
for (i = 0; i < len; i += 1) {
shapes[i].setAsAnimated();
}
};
SVGShapeElement.prototype.createStyleElement = function (data, level) {
// TODO: prevent drawing of hidden styles
var elementData;
var styleOb = new SVGStyleData(data, level);
var pathElement = styleOb.pElem;
if (data.ty === 'st') {
elementData = new SVGStrokeStyleData(this, data, styleOb);
} else if (data.ty === 'fl') {
elementData = new SVGFillStyleData(this, data, styleOb);
} else if (data.ty === 'gf' || data.ty === 'gs') {
var GradientConstructor = data.ty === 'gf' ? SVGGradientFillStyleData : SVGGradientStrokeStyleData;
elementData = new GradientConstructor(this, data, styleOb);
this.globalData.defs.appendChild(elementData.gf);
if (elementData.maskId) {
this.globalData.defs.appendChild(elementData.ms);
this.globalData.defs.appendChild(elementData.of);
pathElement.setAttribute('mask', 'url(' + getLocationHref() + '#' + elementData.maskId + ')');
}
} else if (data.ty === 'no') {
elementData = new SVGNoStyleData(this, data, styleOb);
}
if (data.ty === 'st' || data.ty === 'gs') {
pathElement.setAttribute('stroke-linecap', lineCapEnum[data.lc || 2]);
pathElement.setAttribute('stroke-linejoin', lineJoinEnum[data.lj || 2]);
pathElement.setAttribute('fill-opacity', '0');
if (data.lj === 1) {
pathElement.setAttribute('stroke-miterlimit', data.ml);
}
}
if (data.r === 2) {
pathElement.setAttribute('fill-rule', 'evenodd');
}
if (data.ln) {
pathElement.setAttribute('id', data.ln);
}
if (data.cl) {
pathElement.setAttribute('class', data.cl);
}
if (data.bm) {
pathElement.style['mix-blend-mode'] = getBlendMode(data.bm);
}
this.stylesList.push(styleOb);
this.addToAnimatedContents(data, elementData);
return elementData;
};
SVGShapeElement.prototype.createGroupElement = function (data) {
var elementData = new ShapeGroupData();
if (data.ln) {
elementData.gr.setAttribute('id', data.ln);
}
if (data.cl) {
elementData.gr.setAttribute('class', data.cl);
}
if (data.bm) {
elementData.gr.style['mix-blend-mode'] = getBlendMode(data.bm);
}
return elementData;
};
SVGShapeElement.prototype.createTransformElement = function (data, container) {
var transformProperty = TransformPropertyFactory.getTransformProperty(this, data, this);
var elementData = new SVGTransformData(transformProperty, transformProperty.o, container);
this.addToAnimatedContents(data, elementData);
return elementData;
};
SVGShapeElement.prototype.createShapeElement = function (data, ownTransformers, level) {
var ty = 4;
if (data.ty === 'rc') {
ty = 5;
} else if (data.ty === 'el') {
ty = 6;
} else if (data.ty === 'sr') {
ty = 7;
}
var shapeProperty = ShapePropertyFactory.getShapeProp(this, data, ty, this);
var elementData = new SVGShapeData(ownTransformers, level, shapeProperty);
this.shapes.push(elementData);
this.addShapeToModifiers(elementData);
this.addToAnimatedContents(data, elementData);
return elementData;
};
SVGShapeElement.prototype.addToAnimatedContents = function (data, element) {
var i = 0;
var len = this.animatedContents.length;
while (i < len) {
if (this.animatedContents[i].element === element) {
return;
}
i += 1;
}
this.animatedContents.push({
fn: SVGElementsRenderer.createRenderFunction(data),
element: element,
data: data,
});
};
SVGShapeElement.prototype.setElementStyles = function (elementData) {
var arr = elementData.styles;
var j;
var jLen = this.stylesList.length;
for (j = 0; j < jLen; j += 1) {
if (arr.indexOf(this.stylesList[j]) === -1 && !this.stylesList[j].closed) {
arr.push(this.stylesList[j]);
}
}
};
SVGShapeElement.prototype.reloadShapes = function () {
this._isFirstFrame = true;
var i;
var len = this.itemsData.length;
for (i = 0; i < len; i += 1) {
this.prevViewData[i] = this.itemsData[i];
}
this.searchShapes(this.shapesData, this.itemsData, this.prevViewData, this.layerElement, 0, [], true);
this.filterUniqueShapes();
len = this.dynamicProperties.length;
for (i = 0; i < len; i += 1) {
this.dynamicProperties[i].getValue();
}
this.renderModifiers();
};
SVGShapeElement.prototype.searchShapes = function (arr, itemsData, prevViewData, container, level, transformers, render) {
var ownTransformers = [].concat(transformers);
var i;
var len = arr.length - 1;
var j;
var jLen;
var ownStyles = [];
var ownModifiers = [];
var currentTransform;
var modifier;
var processedPos;
for (i = len; i >= 0; i -= 1) {
processedPos = this.searchProcessedElement(arr[i]);
if (!processedPos) {
arr[i]._render = render;
} else {
itemsData[i] = prevViewData[processedPos - 1];
}
if (arr[i].ty === 'fl' || arr[i].ty === 'st' || arr[i].ty === 'gf' || arr[i].ty === 'gs' || arr[i].ty === 'no') {
if (!processedPos) {
itemsData[i] = this.createStyleElement(arr[i], level);
} else {
itemsData[i].style.closed = arr[i].hd;
}
if (arr[i]._render) {
if (itemsData[i].style.pElem.parentNode !== container) {
container.appendChild(itemsData[i].style.pElem);
}
}
ownStyles.push(itemsData[i].style);
} else if (arr[i].ty === 'gr') {
if (!processedPos) {
itemsData[i] = this.createGroupElement(arr[i]);
} else {
jLen = itemsData[i].it.length;
for (j = 0; j < jLen; j += 1) {
itemsData[i].prevViewData[j] = itemsData[i].it[j];
}
}
this.searchShapes(arr[i].it, itemsData[i].it, itemsData[i].prevViewData, itemsData[i].gr, level + 1, ownTransformers, render);
if (arr[i]._render) {
if (itemsData[i].gr.parentNode !== container) {
container.appendChild(itemsData[i].gr);
}
}
} else if (arr[i].ty === 'tr') {
if (!processedPos) {
itemsData[i] = this.createTransformElement(arr[i], container);
}
currentTransform = itemsData[i].transform;
ownTransformers.push(currentTransform);
} else if (arr[i].ty === 'sh' || arr[i].ty === 'rc' || arr[i].ty === 'el' || arr[i].ty === 'sr') {
if (!processedPos) {
itemsData[i] = this.createShapeElement(arr[i], ownTransformers, level);
}
this.setElementStyles(itemsData[i]);
} else if (arr[i].ty === 'tm' || arr[i].ty === 'rd' || arr[i].ty === 'ms' || arr[i].ty === 'pb' || arr[i].ty === 'zz' || arr[i].ty === 'op') {
if (!processedPos) {
modifier = ShapeModifiers.getModifier(arr[i].ty);
modifier.init(this, arr[i]);
itemsData[i] = modifier;
this.shapeModifiers.push(modifier);
} else {
modifier = itemsData[i];
modifier.closed = false;
}
ownModifiers.push(modifier);
} else if (arr[i].ty === 'rp') {
if (!processedPos) {
modifier = ShapeModifiers.getModifier(arr[i].ty);
itemsData[i] = modifier;
modifier.init(this, arr, i, itemsData);
this.shapeModifiers.push(modifier);
render = false;
} else {
modifier = itemsData[i];
modifier.closed = true;
}
ownModifiers.push(modifier);
}
this.addProcessedElement(arr[i], i + 1);
}
len = ownStyles.length;
for (i = 0; i < len; i += 1) {
ownStyles[i].closed = true;
}
len = ownModifiers.length;
for (i = 0; i < len; i += 1) {
ownModifiers[i].closed = true;
}
};
SVGShapeElement.prototype.renderInnerContent = function () {
this.renderModifiers();
var i;
var len = this.stylesList.length;
for (i = 0; i < len; i += 1) {
this.stylesList[i].reset();
}
this.renderShape();
for (i = 0; i < len; i += 1) {
if (this.stylesList[i]._mdf || this._isFirstFrame) {
if (this.stylesList[i].msElem) {
this.stylesList[i].msElem.setAttribute('d', this.stylesList[i].d);
// Adding M0 0 fixes same mask bug on all browsers
this.stylesList[i].d = 'M0 0' + this.stylesList[i].d;
}
this.stylesList[i].pElem.setAttribute('d', this.stylesList[i].d || 'M0 0');
}
}
};
SVGShapeElement.prototype.renderShape = function () {
var i;
var len = this.animatedContents.length;
var animatedContent;
for (i = 0; i < len; i += 1) {
animatedContent = this.animatedContents[i];
if ((this._isFirstFrame || animatedContent.element._isAnimated) && animatedContent.data !== true) {
animatedContent.fn(animatedContent.data, animatedContent.element, this._isFirstFrame);
}
}
};
SVGShapeElement.prototype.destroy = function () {
this.destroyBaseElement();
this.shapesData = null;
this.itemsData = null;
};
export default SVGShapeElement;