-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (38 loc) · 1.23 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
#!/usr/bin/env node
const fs = require('fs');
const jsdom = require('jsdom');
const { JSDOM } = jsdom;
const filepath = process.argv[2];
const newfilename = process.argv[3] || `${filepath.replace('.svg', '')}.json`;
const filecontents = fs.readFileSync(filepath).toString();
const dom = new JSDOM(filecontents);
const window = dom.window;
const document = window.document;
const circles = document.querySelectorAll('circle');
const data = [...circles].map(circle => {
return {
x: circle.getAttribute('cx'),
y: circle.getAttribute('cy')
}
});
const minMax = (arr, attr, type) => arr
.map(p => p[attr])
.reduce((a,b) => Math[type](a,b));
const xMax = minMax(data, 'x', 'max'),
xMin = minMax(data, 'x', 'min'),
yMax = minMax(data, 'y', 'max'),
yMin = minMax(data, 'y', 'min'),
xRange = xMax - xMin,
yRange = yMax - yMin;
const reduceDecimals = n => Math.round(n * 1000) / 1000;
const normalizedData = data.map(point => {
return {
x: reduceDecimals(1 / xRange * (point.x - xMin)),
y: reduceDecimals(1 / yRange * (point.y - yMin))
}
});
fs.writeFile(
newfilename,
JSON.stringify(normalizedData),
() => console.log(`Normalized svg circle positioning-data written to "${newfilename}".`)
);