-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
447 lines (409 loc) · 13.2 KB
/
index.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
const puppeteer = require('puppeteer');
var xml2js = require('xml2js');
var parseString = require('xml2js').parseString;
// var parser = new xml2js.Parser({explicitArray: true, explicitChildren: true, preserveChildrenOrder: true});
// var parser = new xml2js.Parser();
var convert = require('xml-js');
var saveTemplate = require('./testTemplate');
var formatValue = require('./valueFormatter');
var http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs")
port = process.argv[2] || 8888;
http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname
, filename = path.join(process.cwd(), 'pages\\' + uri);
var contentTypesByExtension = {
'.html': "text/html",
'.css': "text/css",
'.js': "text/javascript"
};
fs.exists(filename, function(exists) {
console.log(filename)
if(!exists) {
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not Found\n");
response.end();
return;
}
if (fs.statSync(filename).isDirectory()) filename += '/index.html';
fs.readFile(filename, "binary", function(err, file) {
if(err) {
response.writeHead(500, {"Content-Type": "text/plain"});
response.write(err + "\n");
response.end();
return;
}
var headers = {};
var contentType = contentTypesByExtension[path.extname(filename)];
if (contentType) headers["Content-Type"] = contentType;
response.writeHead(200, headers);
response.write(file, "binary");
response.end();
});
});
}).listen(parseInt(port, 10));
console.log("Static file server running at\n => http://localhost:" + port + "/\nCTRL + C to shutdown");
// Animation Parser
const TOTAL_LOOPS = '1e10';
let parseFrame = (frameString) => {
return new Promise((resolve, reject)=> {
var options = {ignoreComment: true, alwaysChildren: true, compact: false, alwaysArray: true};
var result = convert.xml2js(frameString, options);
resolve(result)
})
}
const getTotalFrames = async (page) => {
const totalFrames = await page.evaluate(() => {
return anim.totalFrames
})
return totalFrames;
}
const getFrameRate = async (page) => {
const frameRate = await page.evaluate(() => {
return anim.frameRate
})
return frameRate;
}
let getFrameData = async (page) => {
let bodyHTML = await page.evaluate(() => {
var container = document.getElementById('lottie')
return container.innerHTML
})
return bodyHTML
}
let goToFrame = async (page, frame) => {
let bodyHTML = await page.evaluate((frame) => {
anim.goToAndStop(frame, true)
}, frame)
}
const emptyContent = (node) => {
return {
$: node.$
}
}
const getFrame = async (page) => {
const bodyHTML = await getFrameData(page)
const parsedFrame = await parseFrame(bodyHTML)
return parsedFrame
}
const buildContainer = async (frameData) => {
const container = {
elements: [
{
type: frameData.elements[0].type,
name: frameData.elements[0].name,
attributes: {
xmlns: frameData.elements[0].attributes.xmlns,
height: frameData.elements[0].attributes.height,
width: frameData.elements[0].attributes.width,
preserveAspectRatio: frameData.elements[0].attributes.preserveAspectRatio,
viewBox: frameData.elements[0].attributes.viewBox,
style: frameData.elements[0].attributes.style,
},
elements: [{name:'style', type:'element', elements:[{type:'text', text:''}]}, ...frameData.elements[0].elements]
}
]
}
return container
}
const buildElementsData = (frameData) => {
const elementsData = {}
const iterateElements = (elements, parentElement) => {
elements.forEach(element => {
if(element.attributes) {
let attributeKeys = Object.keys(element.attributes)
let attributes = attributeKeys.reduce((accumulator, key)=>{
accumulator[key] = {
changed: false,
values: []
}
return accumulator
}, {})
elementsData[element.attributes.__name] = {
changed: false,
attributes: attributes,
node: element,
parentNode: parentElement
}
if(element.elements) {
iterateElements(element.elements, element)
}
}
})
}
return new Promise((resolve, reject) => {
//I am iterating the third element of the container which contains all the frames.
// First element is the styles tag, second is defs and third is the group with the clipping url
iterateElements(frameData.elements[0].elements, frameData.elements[0])
resolve(elementsData)
})
}
const traverseFrames = async (page, totalFrames) => {
let currentFrame = 0
let frames = []
while(currentFrame < totalFrames) {
await goToFrame(page, currentFrame)
let parsedFrame = await getFrame(page)
frames.push(parsedFrame.elements[0].elements)
currentFrame += 1
}
return frames
}
const compareElements = async(elementsData, elements, index) => {
const iterateElements = elements => {
elements.forEach(element => {
let name = element.attributes.__name
if(name) {
const elementData = elementsData[name]
let attributes = element.attributes
let keys = Object.keys(attributes)
keys.forEach(key => {
let exists = true
if(!elementData.attributes[key]) {
let attribute = {
changed: false,
values: []
}
let i = 0
while(i <= index) {
attribute.values.push('')
i += 1
}
elementData.attributes[key] = attribute
exists = false
}
let values = elementData.attributes[key].values
let previousValue = values.length ? values[values.length - 1] : null
// Setting first non-empty value as default value for element
if(!elementData.node.attributes[key] && attributes[key] !== '') {
elementData.node.attributes[key] = attributes[key]
}
if(previousValue !== null
&& previousValue !== ''
&& previousValue !== attributes[key]) {
elementData.attributes[key].changed = true;
}
elementData.attributes[key].values.push(attributes[key])
})
if(element.elements) {
iterateElements(element.elements)
}
}
})
}
iterateElements(elements)
}
const includeFrames = async (elementsData, frames) => {
frames.forEach((elements, index) => {
compareElements(elementsData, elements, index)
})
}
const formatKey = (key) => {
if(key === 'style') {
return 'visibility';
}
return key;
}
const buildKeyframes = (elementKey, changedAttributes) => {
let keyframes = `@keyframes ${elementKey} {`
keyframes += `\r\n`
let properties = {}
changedAttributes.forEach(attribute => {
let values = attribute.values
let attributeKey = attribute.key
values.forEach((value, index) => {
if(index > 0 && values[index - 1] === value && values[index + 1] === value) {
// We are skipping
} else {
let percKey = Math.floor(index/(values.length-1) * 10000)
if(!properties[percKey]) {
properties[percKey] = {
value: '',
}
}
let currentProperty = properties[percKey];
currentProperty.value += `${formatKey(attributeKey)}:${formatValue(attributeKey, value)};`
}
})
})
let percentKeys = Object.keys(properties).sort((a,b)=>a-b)
percentKeys.forEach(key => {
keyframes += `${key / 100}% {`
keyframes += `${properties[key].value}`
keyframes += '}'
keyframes += `\r\n`
})
keyframes += '}'
keyframes += `\r\n`
return keyframes
}
const buildPaths = (values, elementName, animationDuration, stylesTextElement, extraAttributes) => {
values = values.map((path, index)=>{
return {
path: path,
index: index,
lastIndex: index,
canDelete: false,
total: values.length
}
})
let lastData
values.forEach((data, index) => {
if(lastData && data.path === lastData.path) {
lastData.lastIndex = index
data.canDelete = true
} else {
lastData = data
}
})
values = values.filter((data)=>!data.canDelete)
const paths = values.map((value, index) => {
const pathName = elementName + '__' + index
const animationValues = []
let i = 0
while (i < value.total) {
if (i < value.index || i > value.lastIndex) {
animationValues.push('hidden')
} else {
animationValues.push('inherit')
}
i += 1
}
const animationData = [{
key: 'visibility',
values: animationValues
}]
stylesTextElement.text += buildKeyframes(pathName, animationData)
return {
name: 'path',
type: 'element',
attributes: {
d: formatValue('d--static', value.path),
style: `animation: ${pathName} ${animationDuration}s steps(1) ${TOTAL_LOOPS};`,
...extraAttributes
}
}
})
return paths
}
const createAnimations = async (elementsData, animationDuration, stylesTextElement) => {
const keys = Object.keys(elementsData)
keys.forEach(elementKey => {
const elementData = elementsData[elementKey]
const node = elementData.node
const attributeKeys = Object.keys(elementData.attributes)
let hasAnimation = false
const changedAttributes = []
const pathAttribute = []
attributeKeys.forEach(attributeKey => {
const attribute = elementData.attributes[attributeKey]
if(attribute.changed) {
// If targetting Chrome, this first condition is not needed because it supports css path values
if (attributeKey === 'd' && false) {
const parentNode = elementData.parentNode
let extraAttributes = {}
if(parentNode && parentNode.name === 'clipPath') {
if(node.attributes['clip-rule'] && node.attributes['clip-rule'] !== 'nonzero') {
extraAttributes['clip-rule'] = node.attributes['clip-rule']
}
parentNode.elements = buildPaths(attribute.values, elementKey, animationDuration, stylesTextElement, extraAttributes)
} else {
node.name = 'g'
node.elements = buildPaths(attribute.values, elementKey, animationDuration, stylesTextElement, extraAttributes)
}
} else {
changedAttributes.push({
key: attributeKey,
values: attribute.values
})
hasAnimation = true
}
}
})
if(hasAnimation) {
let keyframes = buildKeyframes(elementKey, changedAttributes)
stylesTextElement.text += keyframes
changedAttributes.forEach(attribute => {
node.attributes[attribute.key] = null
})
// node.attributes.style = `animation: ${elementKey} ${animationDuration}s steps(1) ${TOTAL_LOOPS};`
// node.attributes.style += `animation-fill-mode: both;`
// If we set animation properties as global, this is the only property that the animation needs:
node.attributes.style = `animation-name: ${elementKey};`
}
})
stylesTextElement.text += '\r\n'
stylesTextElement.text += `*{animation-duration:${animationDuration}s;animation-timing-function:steps(1);animation-iteration-count: infinite;}`
}
const startAnimation = async(page) => {
return new Promise(async (resolve, reject) => {
await page.evaluate(() => {
loadAnimation()
})
let intervalId = setInterval(async()=>{
let isLoaded = await page.evaluate(() => {
return isLoaded
})
if(isLoaded) {
clearInterval(intervalId)
resolve()
}
}, 100)
})
}
const cleanUnneededAttributes = (elementsData) => {
let elementsKeys = Object.keys(elementsData)
elementsKeys.forEach(key => {
let element = elementsData[key]
let node = element.node
let attributesKeys = Object.keys(node.attributes)
attributesKeys.forEach(attributeKey => {
if (attributeKey === '__name'
|| attributeKey === 'style' && node.attributes[attributeKey] === 'display: block;'
|| attributeKey === 'stroke-linejoin' && node.attributes[attributeKey] === 'miter'
|| attributeKey === 'stroke-linecap' && node.attributes[attributeKey] === 'butt'
|| attributeKey === 'opacity' && node.attributes[attributeKey] === '1'
|| attributeKey === 'fill-opacity' && node.attributes[attributeKey] === '1'
|| attributeKey === 'stroke-opacity' && node.attributes[attributeKey] === '1'
) {
node.attributes[attributeKey] = null;
} else if (attributeKey === 'd' && node.attributes[attributeKey]) {
node.attributes[attributeKey] = formatValue(attributeKey + '--static', node.attributes[attributeKey])
} else if (attributeKey === 'transform' && node.attributes[attributeKey]) {
node.attributes[attributeKey] = formatValue(attributeKey, node.attributes[attributeKey])
} else if (attributeKey === 'style' && node.attributes[attributeKey] === 'display: none;') {
node.name = null
node.type = 'text'
node.text = ''
} else if (attributeKey === 'fill-opacity' && node.attributes[attributeKey] === '0') {
node.attributes[attributeKey] = null
node.attributes['fill'] = 'none'
}
})
})
}
(async () => {
try {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('http://localhost:8888/index.html', {waitUntil: 'networkidle2'});
await startAnimation(page)
const totalFrames = await getTotalFrames(page)
const frameRate = await getFrameRate(page)
const initialFrameData = await getFrame(page)
const container = await buildContainer(initialFrameData)
const elementsData = await buildElementsData(container)
const frames = await traverseFrames(page, totalFrames)
const content = await includeFrames(elementsData, frames)
await createAnimations(elementsData, Math.ceil(1000 * frames.length / frameRate) / 1000, container.elements[0].elements[0].elements[0])
await cleanUnneededAttributes(elementsData)
var result = convert.js2xml(container, {});
saveTemplate(result)
// console.log(result)
await browser.close();
} catch(err) {
console.log(err)
}
})();