forked from ChromeGaming/Physi-c-Tech
-
Notifications
You must be signed in to change notification settings - Fork 0
/
desvg.html
153 lines (147 loc) · 4.99 KB
/
desvg.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Convert SVG to Condensed String</title>
<script>
function parseElement(el) {
var elString = '';
var elBits = el.split(' ');
switch (elBits[0]) {
case 'path':
elString += ";" + buildPath(elBits);
break;
case 'circle':
elString += ";" + buildCircle(elBits);
break;
case 'ellipse':
elString += ";" + buildEllipse(elBits);
break;
case 'polygon':
elString += ";" + buildPolygon(elBits);
break;
case 'polyline':
elString += ";" + buildPolygon(elBits);
break;
case 'rect':
elString += ";" + buildRect(elBits);
break;
case 'line':
elString += ";" + buildLine(elBits);
break;
default:
break;
}
return elString;
}
function buildPolygon(bits){
var partholder={};
var startPoints = false;
for(var i =0; i<bits.length; i++){
var parts = bits[i].split("=");
if (startPoints) partholder.points += ' ' + parts[0];
if (parts[1])
partholder[parts[0]] = parts[1].replace(/"/g,'');
if (parts[0] == 'points'){
startPoints = true;
}
}
return "y" + getHex(partholder.fill) + getHex(partholder.stroke) + condense(partholder.points);
}
function buildEllipse(bits){
var partholder={};
for(var i =0; i<bits.length; i++){
var parts = bits[i].split("=");
if (parts[1])
partholder[parts[0]] = parts[1].replace(/"/g,'');
}
return "e" + getTwoDigits(partholder.cx) + getTwoDigits(partholder.cy) + getTwoDigits(partholder.rx) + getTwoDigits(partholder.ry) + getHex(partholder.stroke) + getHex(partholder.fill);
}
function buildLine(bits){
var partholder={};
for(var i =0; i<bits.length; i++){
var parts = bits[i].split("=");
if (parts[1])
partholder[parts[0]] = parts[1].replace(/"/g,'');
}
return "i" + getTwoDigits(partholder.x1) + getTwoDigits(partholder.y1) + getTwoDigits(partholder.x2) + getTwoDigits(partholder.y2) + getHex(partholder.stroke) + getHex(partholder.fill) + getTwoDigits(partholder['stroke-width']);
}
function buildRect(bits){
var partholder={};
for(var i =0; i<bits.length; i++){
var parts = bits[i].split("=");
if (parts[1])
partholder[parts[0]] = parts[1].replace(/"/g,'');
}
return "r" + getTwoDigits(partholder.x) + getTwoDigits(partholder.y) + getTwoDigits(partholder.width) + getTwoDigits(partholder.height) + getHex(partholder.fill) + getHex(partholder.stroke) + getTwoDigits(partholder['stroke-width']);
}
function buildCircle(bits){
var partholder={};
for(var i =0; i<bits.length; i++){
var parts = bits[i].split("=");
if (parts[1])
partholder[parts[0]] = parts[1].replace(/"/g,'');
}
return "c" + getTwoDigits(partholder.cx) + getTwoDigits(partholder.cy) + getTwoDigits(partholder.r) + getHex(partholder.stroke) + getHex(partholder.fill) + getTwoDigits(partholder['stroke-width']);
}
function buildPath(bits){
var partholder={};
for(var i =0; i<bits.length; i++){
var parts = bits[i].split("=");
if (parts[1])
partholder[parts[0]] =parts[1].replace(/"/g,'');
}
return "p" + getHex(partholder.fill) + getHex(partholder.stroke) + getMinTwoDigits(partholder['stroke-width']) + partholder.d.replace(/"/g,'').replace(/\//,'');
}
function getHex(val) {
if (!val) return '000';
else if (val == 'none') return 'non';
else return val.replace(/#/,'');
}
function getTwoDigits(val){
if (!val) return '';
val = Math.round(parseFloat(val));
if (val < 0) return val;
else if (val < 10) return '0' + val;
else return parseInt(val);
}
function getMinTwoDigits(val){
return (getTwoDigits(val)) ? getTwoDigits(val) : '01';
}
function convert(ev){
ev.preventDefault();
var svg = document.getElementById('svg').value;
svg.replace(/\t/,'');
svg.replace(/\n/,'');
var svgElements = svg.split("><");
var svgString = '';
for (var i=0; i<svgElements.length; i++){
svgString += parseElement(svgElements[i]);
}
document.getElementById('string').value = svgString;
}
function condense(pointlist){
var points = pointlist.split(' ');
var ret = '';
for (var i=0; i<points.length;i++){
if (points[i] != '"/') {
[pointA, pointB] = points[i].split(',');
console.log(pointA + ',' + pointB);
ret += getTwoDigits(pointA) + '' + getTwoDigits(pointB);
}
}
return ret;
}
</script>
</head>
<body>
This takes an SVG with very specific formatting and returns a condensed string. The SVG data should have tabs and newlines removed first.<br><br>
Coordinates should all be two digits long - meaning your SVG can only be up to 99 pixels wide and 99 pixels tall.<br><br>
Colors should be web-safe, three-digit colors (#FF2211 should be #F21. #6AB794 should first be converted to something web-safe, like #7B9. Not an exact match by any means.)<br>
<form>
<textarea cols="120" rows="20" id="svg"></textarea><br>
<button onclick="convert(event)">Convert</button><br>
<textarea cols="120" rows="10" id="string"></textarea>
</form>
</body>
</html>