-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
314 lines (244 loc) · 8.05 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>svg2lines</title>
<style>
html, body {
margin: 0 !important;
padding: 0 !important;
height: 100%;
}
.in {
box-shadow: inset 0 0 30px #8BC34A;
}
#btn-download {
position: fixed;
top: -9999999px;
}
#holder {
text-align: center;
padding-top: 100px;
color: #606c71;
font-size: 23px;
}
svg {
position: absolute;
top: 0;
left: 0;
z-index: -1;
}
svg line {
stroke: #E2B3E6;
stroke-width: 1px;
}
</style>
</head>
<body>
<a href="https://github.com/cvdlab/svg2lines">
<img style="position: fixed; top: 0; right: 0; border: 0;"
src="https://camo.githubusercontent.com/652c5b9acfaddf3a9c326fa6bde407b87f7be0f4/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6f72616e67655f6666373630302e706e67"
alt="Fork me on GitHub"
data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_orange_ff7600.png"></a>
<a href="#" id="btn-download">Download</a>
<div id="holder">Drag & Drop Your SVG File Here</div>
<svg id="preview" width="200" height="200" version="1.1" xmlns="http://www.w3.org/2000/svg">
</svg>
<script src="https://cdn.rawgit.com/jarek-foksa/path-data-polyfill.js/master/path-data-polyfill.js"></script>
<script>
var body = document.getElementsByTagName('body')[0];
var link = document.getElementById('btn-download');
var preview = document.getElementById('preview');
window.addEventListener("dragover", function (e) {
e = e || event;
e.preventDefault();
}, false);
window.addEventListener("drop", function (e) {
e = e || event;
e.preventDefault();
}, false);
body.addEventListener('dragenter', function (e) {
e.preventDefault();
console.log('entering');
body.classList.add('in');
return false;
});
body.addEventListener('dragleave', function (e) {
e.preventDefault();
console.log('leaving');
body.classList.remove('in');
return false;
});
body.addEventListener('drop', function (e) {
body.classList.remove('in');
e.preventDefault();
var file = e.dataTransfer.files[0];
var reader = new FileReader();
reader.onload = function (event) {
var text = event.target.result;
var parser = new DOMParser();
var doc = parser.parseFromString(text, "image/svg+xml");
try {
var output = svg2lines(doc.children[0], preview);
var filename = "output" + Date.now() + ".lines";
filename = window.prompt("Insert output filename", filename);
if (!filename) return;
var data = new Blob([output], {type: 'text/plain'});
var url = window.URL.createObjectURL(data);
link.setAttribute('download', filename);
link.href = url;
link.click();
} catch (e) {
alert(e.message);
console.error(e);
}
};
reader.readAsText(file);
return false;
});
window.converters = {
svg: function (attributes, node) {
var lines = [];
for (var i = 0; i < node.childNodes.length; i++) {
var children = node.childNodes[i];
if (children.nodeType !== Node.ELEMENT_NODE) continue;
var type = children.tagName;
if (!window.converters[type]) {
throw new Error("Sorry, but the node '" + type + "' is not supported");
} else {
window.converters[type](children.attributes, children).forEach(function (line) {
lines.push(line);
})
}
}
return lines;
},
g: function (attributes, node) {
return window.converters.svg(attributes, node);
},
line: function (attributes) {
return [
[
parseFloat(attributes.x1.value),
parseFloat(attributes.y1.value),
parseFloat(attributes.x2.value),
parseFloat(attributes.y2.value)
]
];
},
rect: function (attributes) {
var x = parseFloat(attributes.x.value), y = parseFloat(attributes.y.value);
var w = parseFloat(attributes.width.value), h = parseFloat(attributes.height.value);
return [
[x, y, x + w, y],
[x + w, y, x + w, y + h],
[x + w, y + h, x, y + h],
[x, y + h, x, y]
];
},
polyline: function (attributes, node) {
var points = node.points;
var lines = [];
for (var i = 0; i < points.length - 1; i++) {
lines.push([points[i].x, points[i].y, points[i + 1].x, points[i + 1].y]);
}
return lines;
},
polygon: function (attributes, node) {
var polylineConverter = window.converters.polyline;
var lines = polylineConverter(attributes, node);
var first = lines[0];
var last = lines[lines.length - 1];
lines.push([first[0], first[1], last[2], last[3]]);
return lines;
},
circle: function (attributes) {
var cx = parseFloat(attributes.cx.value);
var cy = parseFloat(attributes.cy.value);
var r = parseFloat(attributes.r.value);
var linesCount = 36;
var deltaAlfa = 2 * Math.PI / linesCount;
var points = [];
for (var j = 0; j < linesCount; j++) {
var alfa = j * deltaAlfa;
var x = r * Math.cos(alfa) + cx;
var y = r * Math.sin(alfa) + cy;
points.push([x, y]);
}
var lines = [];
for (var i = 0; i < points.length - 1; i++) {
lines.push([points[i][0], points[i][1], points[i + 1][0], points[i + 1][1]]);
}
var first = points[0];
var last = points[points.length - 1];
lines.push([first[0], first[1], last[0], last[1]]);
return lines;
},
path: function (attributes, node) {
var segmentLength = 10;
var lines = [];
var pathStructure = node.getPathData({normalize: true});
var currentPoint = {x: 0, y: 0};
var firstPoint = false;
var x, y;
for (var seg of pathStructure) {
if (seg.type === "M") {
currentPoint = {x: seg.values[0], y: seg.values[1]};
} else if (seg.type === "L") {
x = seg.values[0];
y = seg.values[1];
lines.push([currentPoint.x, currentPoint.y, x, y]);
currentPoint = {x: x, y: y};
} else if (seg.type === "Z") {
lines.push([firstPoint.x, firstPoint.y, currentPoint.x, currentPoint.y]);
break;
} else {
lines = false;
break;
}
if (!firstPoint) firstPoint = currentPoint;
}
if (Array.isArray(lines)) return lines; else lines = [];
var length = node.getTotalLength();
var points = [];
for (var d = 0; d < length; d += segmentLength) {
points.push(node.getPointAtLength(d));
}
points.push(node.getPointAtLength(length));
for (var i = 0; i < points.length - 1; i++) {
lines.push([points[i].x, points[i].y, points[i + 1].x, points[i + 1].y]);
}
return lines;
}
};
window.svg2lines = function (svg, previewArea) {
if (svg.tagName !== 'svg') {
throw new Error("Sorry, but this file is not supported. Please upload only SVG files.")
}
var lines = window.converters['svg'](svg.attributes, svg);
console.log('lines', lines);
var output = lines.map(function (row) {
return row
.map(function (number) {
return Number.isInteger(number) ? number.toFixed(1) : number.toString();
})
.join(',');
}).join('\n');
previewArea.innerHTML = '';
var width = 0, height = 0;
lines.forEach(function (row) {
var DOMLine = document.createElementNS("http://www.w3.org/2000/svg", "line");
DOMLine.setAttribute('x1', row[0]);
DOMLine.setAttribute('y1', row[1]);
DOMLine.setAttribute('x2', row[2]);
DOMLine.setAttribute('y2', row[3]);
width = Math.max(row[0], row[2], width);
height = Math.max(row[1], row[3], height);
previewArea.appendChild(DOMLine);
});
previewArea.setAttribute('width', width + 10);
previewArea.setAttribute('height', height + 10);
return output;
};
</script>
</body>
</html>