forked from exupero/saveSvgAsPng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
369 lines (316 loc) · 9.93 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
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
<!DOCTYPE html>
<meta http-equiv="content-type" content="text/html; charset=UTF8">
<title>saveSvgAsPng</title>
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400italic,400,300,600' rel='stylesheet' type='text/css' />
<link rel=stylesheet href=bootstrap.min.css />
<style>
@font-face {
font-family: 'Stalemate';
font-style: normal;
font-weight: 400;
src: url(stalemate.ttf) format('truetype');
}
input[type="number"]::-webkit-inner-spin-button, input[type="number"]::-webkit-outer-spin-button {
padding: 15px;
}
h2, h3 {
margin-top: 0;
}
h3 .btn {
margin-top: -8px;
}
ul {
list-style-type: none;
padding: 0;
}
ul li {
padding: 30px 20px;
border-bottom: 1px dashed gray;
}
svg, img {
border: 1px solid lightgray;
}
textarea {
width: 100%;
height: 100px;
}
.error {
border: 1px solid red;
border-radius: 10px;
color: red;
padding: 8px 10px;
}
#sized-with-css svg {
width: 200px;
height: 200px;
}
#selectors-prefixed svg rect {
fill: blue;
}
rect.css-styled {
fill: green !important;
}
/* Invalid selectors */
[ng\:cloak] {
display: block;
}
ng\:form {
display: block;
}
</style>
<script type=text/template id=inline-template>
<div class=row>
<div class=col-md-6>
<h2></h2>
</div>
<div class=col-md-6>
<h3>Preview <button class="save btn">Save as PNG</button></h3>
</div>
</div>
<div class=row>
<div class="canvas col-md-6">
</div>
<div class=col-md-6>
<div class=preview></div>
</div>
</div>
</script>
<div class=container>
<h1>saveSvgAsPng</h1>
<p>This page tests various features of saveSvgAsPng.</p>
<p>You can test your own SVG code in the Sandbox. If something doesn't work as expected, you can <a href="https://github.com/exupero/saveSvgAsPng/issues">file an issue on GitHub</a>.</p>
<ul>
<li id=sandbox>
<h2>Sandbox</h2>
<p>Paste you SVG below to see how it renders.</p>
<textarea><svg></svg></textarea>
<br/>
<button class="render btn">Preview</button>
<div class=load-target style="margin-top:20px;"></div>
<br/>
<h3>Preview <button class="save btn">Save as PNG</button></h3>
<span class=error style="display:none;"></span>
<div class=preview></div>
</li>
<li id=filereader>
<div class=row>
<div class=col-md-6>
<h2>Load from your hard drive</h2>
</div>
<div class=col-md-6>
<h3>Preview <button class="save btn">Save as PNG</button></h3>
</div>
</div>
<div class=row>
<div class=col-md-6>
<input type=file id=file name="files[]" />
<div class=load-target></div>
</div>
<div class=col-md-6>
<div class=preview>No file selected.</div>
</div>
</div>
</li>
<li id=inline>
<svg width=200 height=200>
<rect x=50 y=50 width=100 height=100></rect>
</svg>
</li>
<li id=embedded-png>
<svg width=200 height=200>
<image xlink:href=image.png x=50 y=50 width=100 height=100></image>
</svg>
</li>
<li id=embedded-svg>
<svg width=200 height=200>
<image xlink:href=test.svg x=50 y=50 width=100 height=100></image>
</svg>
</li>
<li id=sized-with-pixels>
<svg width="200px" height="200px">
<rect x=50 y=50 width=100 height=100></rect>
</svg>
</li>
<li id=sized-with-style>
<svg style="width:200px;height:200px;">
<rect x=50 y=50 width=100 height=100></rect>
</svg>
</li>
<li id=sized-with-css>
<svg>
<rect x=50 y=50 width=100 height=100></rect>
</svg>
</li>
<li id=scaling>
<svg width=200 height=200>
<rect width=100 height=100></rect>
<image xlink:href=image.png x=50 y=50 width=100 height=100></image>
</svg>
</li>
<li id=selectors-prefixed>
<svg width=200 height=200>
<rect x=0 y=50 width=100 height=100></rect>
<rect class=css-styled x=100 y=50 width=100 height=100></rect>
</svg>
</li>
<li id=group>
<svg width=200 height=200>
<g id=sub-group transform="translate(40,40)">
<rect x=10 y=10 width=100 height=100></rect>
</g>
</svg>
</li>
<li id=percentage-size>
<svg width="100%" height="100%">
<rect x=25 y=25 width=100 height=100></rect>
</svg>
</li>
<li id=background-color>
<svg width=200 height=200>
<g id=sub-group transform="translate(40,40)">
<rect x=10 y=10 width=100 height=100></rect>
</g>
</svg>
</li>
<li id=pan-and-zoom>
<svg width=200 height=200>
<g transform="scale(2)">
<rect x=10 y=10 width=100 height=100></rect>
</g>
</svg>
</li>
<li id=unicode>
<svg width=200 height=200>
<text x=100 y=100 text-anchor=middle dy=14>ö,i,ç,ğ</text>
</svg>
</li>
<li id=gradient>
<svg width=200 height=200>
<defs>
<linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:rgb(255,0,255);stop-opacity:1" />
<stop offset="100%" style="stop-color:rgb(0,255,255);stop-opacity:1" />
</linearGradient>
</defs>
<line x2="200" y2="200" stroke="url(#grad1)" stroke-width="5px" />
</svg>
</li>
<li id=foreign-object>
<svg width=200 height=200>
<foreignobject x=50 y=50 width=50 height=100>
<div>Foreign Object</div>
</foreignobject>
</svg>
</li>
<li id=xmlns-override>
<svg width=200 height=200>
<foreignobject x=50 y=50 width=50 height=100>
<div xml:xmlns="http://www.w3.org/2000/xmlns/">Foreign Object</div>
</foreignobject>
</svg>
</li>
<li id=opacity>
<svg width=200 height=200>
<rect x=50 y=50 width=100 height=100 fill="green"></rect>
<rect x=60 y=60 width=100 height=100 fill="blue" opacity="0.5"></rect>
</svg>
</li>
<li id=entities>
<svg width=200 height=200>
<text x=50 y=100>"&¢ £»¼Ç</text>
</svg>
</li>
<li id=custom-font>
<div style="color:red">Custom fonts are not currently supported.</div>
<svg width=200 height=200>
<text x=100 y=100 text-anchor=middle dy=14 style="font-family:'Stalemate';font-size:36pt;">Custom Fonts</text>
</svg>
</li>
</ul>
</div>
<script src=http://code.jquery.com/jquery-latest.js></script>
<script src=saveSvgAsPng.js></script>
<script>
function handleFileSelect(evt) {
var $el = $('#filereader');
var files = evt.target.files;
for (var i = 0, f; f = files[i]; i++) {
var reader = new FileReader();
reader.onload = (function(file) {
return function(e) {
$el.find('.load-target').html(e.target.result);
svgAsPngUri($el.find('.load-target svg')[0], null, function(uri) {
$el.find('input').hide()
$el.find('.preview').html('<img src="' + uri + '" />');
});
$el.find('.save').click(function() {
saveSvgAsPng($el.find('.load-target svg')[0], 'test.png');
});
}
})(f);
reader.readAsText(f);
}
}
if (window.File && window.FileReader && window.FileList && window.Blob) {
document.getElementById('file').addEventListener('change', handleFileSelect, false);
}
function inlineTest(title, $el, saveOptions, testOptions) {
var svg = $el.html();
var template = $('#inline-template').html();
var row = $el.html(template);
row.find('h2').text(title);
row.find('.canvas').html(svg);
var canvas = row.find(testOptions && testOptions.selector || 'svg')[0];
svgAsPngUri(canvas, saveOptions, function(uri) {
row.find('.preview').html('<img src="' + uri + '" />');
});
row.find('.save').click(function() {
saveSvgAsPng(canvas, 'test.png', saveOptions);
});
}
inlineTest('Directly in the HTML', $('#inline'));
inlineTest('With linked PNG image', $('#embedded-png'));
inlineTest('With linked SVG image', $('#embedded-svg'));
inlineTest('Sized with pixels', $('#sized-with-pixels'));
inlineTest('Sized with style', $('#sized-with-style'));
inlineTest('Sized with CSS', $('#sized-with-css'));
inlineTest('At a higher resolution', $('#scaling'), {scale: 2});
inlineTest('When CSS styling selectors are prefixed', $('#selectors-prefixed'), {
selectorRemap: function(s) {return s.replace('#selectors-prefixed ', '')}
});
inlineTest('Exporting a group within an SVG', $('#group'), null, {
selector: '#sub-group'
});
inlineTest('Percentage Height and Width', $('#percentage-size'));
inlineTest('Background color', $('#background-color'), {backgroundColor: 'lightblue'});
inlineTest('Pan and Zoom', $('#pan-and-zoom'), {
left: -50,
top: -50,
width: 300,
height: 300
});
inlineTest('With Unicode characters', $('#unicode'));
inlineTest('With gradients', $('#gradient'));
inlineTest('With foreign objects', $('#foreign-object'));
inlineTest('With opacity', $('#opacity'));
inlineTest('When setting xmlns on foreign object children', $('#xmlns-override'));
inlineTest('When using HTML entites', $('#entities'));
inlineTest('With custom fonts', $('#custom-font'));
var $sandbox = $('#sandbox');
$sandbox.find('.render').click(function() {
$sandbox.find('.error').hide().text('');
$sandbox.find('.load-target').html($('#sandbox textarea').val());
var canvas = $sandbox.find('.load-target svg')[0];
try {
svgAsPngUri(canvas, null, function(uri) {
$sandbox.find('.preview').html('<img src="' + uri + '" />');
});
$sandbox.find('.save').unbind('click').click(function() {
saveSvgAsPng(canvas, 'test.png');
});
} catch(err) {
$sandbox.find('.error').show().text(err.message);
$sandbox.find('.preview').html('');
}
});
</script>