-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathicons-v2.html
252 lines (201 loc) · 8.79 KB
/
icons-v2.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
<html>
<head>
<!-- start at 16-06 12:25 -->
<title>Cordova image creator</title>
<meta charset="UTF-8">
<script type="text/javascript" src="jszip.min.js"></script>
<script type="text/javascript" src="FileSaver.min.js"></script>
<script type="text/javascript">
let configSet = false;
let sourceIOSSet = false;
let sourceAndroidFSet = false;
let sourceAndroidBSet = false;
let imagesIOS = [];
let imagesAndroidF = [];
let imagesAndroidB = [];
let sourceIOS = null;
let sourceAndroidF = null;
let sourceAndroidB = null;
let colorXMLPath = null;
window.onload = function() {
var configInput = document.getElementById('config');
configInput.addEventListener("change", parseConfig);
var sourceInputIOS = document.getElementById('image-ios');
sourceInputIOS.addEventListener("change", parseImageIOS);
var sourceInputAndroidF = document.getElementById('image-androidF');
sourceInputAndroidF.addEventListener("change", parseImageAndroidF);
var sourceInputAndroidB = document.getElementById('image-androidB');
sourceInputAndroidB.addEventListener("change", parseImageAndroidB);
var zipButton = document.getElementById('zip');
zipButton.addEventListener("click", createImages);
};
function createZip() {
var zip = new JSZip();
if(colorXMLPath) {
zip.file(colorXMLPath, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<resources><color name=\"background\">"+document.getElementById("background").value+"</color><color name=\"cdv_splashscreen_background\">"+document.getElementById("background").value+"</color></resources>");
}
for(let i=0; i< imagesIOS.length; i++) {
let image = imagesIOS[i];
zip.file(image.src, image.blob.split(",")[1], {base64: true});
}
uniqueImagesAndroidF = imagesAndroidF.filter(function(item, pos) {
return imagesAndroidF.indexOf(item) == pos;
})
uniqueImagesAndroidB = imagesAndroidB.filter(function(item, pos) {
return imagesAndroidB.indexOf(item) == pos;
})
for(let i=0; i< uniqueImagesAndroidF.length; i++) {
let image = uniqueImagesAndroidF[i];
zip.file(image, sourceAndroidF);
}
for(let i=0; i< uniqueImagesAndroidB.length; i++) {
let image = uniqueImagesAndroidB[i];
zip.file(image, sourceAndroidB);
}
zip.generateAsync({type:"blob"}).then(function(content) {
// see FileSaver.js
saveAs(content, "icons.zip");
});
}
function createImages(e) {
if(configSet != true || sourceIOSSet != true || sourceAndroidFSet != true || sourceAndroidBSet != true) {
alert("Not all data is set");
return;
}
let n = 0;
for(let i=0; i< imagesIOS.length; i++) {
let image = imagesIOS[i];
let canvas = document.createElement('canvas');
let context = canvas.getContext('2d');
context.imageSmoothingEnabled = true;
canvas.width = image.width;
canvas.height = image.height;
context.width = image.width;
context.height = image.height;
let img = new Image();
img.onload = function() {
context.drawImage(img, 0, 0, image.width, image.height);
image.blob = canvas.toDataURL("image/png");
n++;
if(n == imagesIOS.length) {
createZip();
}
}
img.src = sourceIOS;
}
}
function parseConfig(e) {
if(e.target.files[0].type != "text/xml") {
return false;
}
var reader = new FileReader();
reader.onload = (theFile) => {
images = [];
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(theFile.target.result, "text/xml");
var resourceTag = xmlDoc.getElementsByTagName("resource-file");
for(let i=0; i<resourceTag.length; i++) {
if(resourceTag[i].getAttribute("src")) {
colorXMLPath = resourceTag[i].getAttribute("src");
}
break;
}
var iconSizes = xmlDoc.getElementsByTagName("icon");
for(let i=0; i<iconSizes.length; i++) {
if(iconSizes[i].getAttribute("src"))
{
let image = {
src: iconSizes[i].getAttribute("src"),
width: 0,
height: 0,
blob: null
};
if(iconSizes[i].getAttribute("density")) {
switch(iconSizes[i].getAttribute("density")) {
case "ldpi":
image.width = 36;
image.height = 36;
break;
case "mdpi":
image.width = 48;
image.height = 48;
break;
case "hdpi":
image.width = 72;
image.height = 72;
break;
case "xhdpi":
image.width = 96;
image.height = 96;
break;
case "xxhdpi":
image.width = 144;
image.height = 144;
break;
case "xxxhdpi":
image.width = 192;
image.height = 192;
break;
}
if(image.width != 0 && image.height != 0) {
imagesIOS.push(image);
}
} else if(iconSizes[i].getAttribute("width") && iconSizes[i].getAttribute("height")) {
image.width = + iconSizes[i].getAttribute("width");
image.height = + iconSizes[i].getAttribute("height");
if(image.width != 0 && image.height != 0) {
imagesIOS.push(image);
}
}
}
if(iconSizes[i].getAttribute("background"))
{
imagesAndroidB.push(iconSizes[i].getAttribute("background"));
}
if(iconSizes[i].getAttribute("foreground"))
{
imagesAndroidF.push(iconSizes[i].getAttribute("foreground"));
}
}
configSet = (imagesAndroidF.length + imagesIOS.length != 0);
};
reader.readAsText(e.target.files[0]);
return true;
}
function parseImageIOS(e) {
var url = URL.createObjectURL(e.target.files[0]);
sourceIOS = url;
sourceIOSSet = true;
}
function parseImageAndroidF(e) {
var url = e.target.files[0];
sourceAndroidF = url;
sourceAndroidFSet = true;
}
function parseImageAndroidB(e) {
var url = e.target.files[0];
sourceAndroidB = url;
sourceAndroidBSet = true;
}
</script>
</head>
<body>
<h1>SVG to PNG demo</h1>
<h3>Select cordova config file</h3>
<input type="file" id="config" /> <br/>
<h2>iPhone</h2>
<h3>Select svg source image</h3>
<input type="file" id="image-ios" /> <br/>
<h2>Android</h2>
<h3>Select avd source image</h3>
Create it with <a href="https://shapeshifter.design/">https://shapeshifter.design/</a>:
<br/>
<h4>Foreground:</h4>
<input type="file" id="image-androidF" /> <br/>
<h4>Background:</h4>
<input type="file" id="image-androidB" /> <br/>
<h2>Run code</h2>
<input type="button" id="zip" value="Create images" /> <br/>
<h2>Canvas:</h2>
</body>
<html>