1 | function handleFileSelect(evt) {
|
2 | const $el = $('#filereader');
|
3 | const files = evt.target.files;
|
4 | const file = files.length > 0 ? files[0] : null;
|
5 | if (file) {
|
6 | const reader = new FileReader();
|
7 | reader.onload = e => {
|
8 | $el.find('.load-target').html(e.target.result);
|
9 | svgAsPngUri($el.find('.load-target svg')[0], null, (uri, width, height) => {
|
10 | $el.find('input').hide();
|
11 | $el.find('.preview').html(
|
12 | '<div>' +
|
13 | '<img src="' + uri + '" />' +
|
14 | '<div>width=' + width + ', height=' + height + '</div>' +
|
15 | '</div>'
|
16 | );
|
17 | });
|
18 | $el.find('.save').click(() => saveSvgAsPng($el.find('.load-target svg')[0], 'test.png'));
|
19 | };
|
20 | reader.readAsText(file);
|
21 | }
|
22 | }
|
23 |
|
24 | if (window.File && window.FileReader && window.FileList && window.Blob) {
|
25 | document.getElementById('file').addEventListener('change', handleFileSelect, false);
|
26 | }
|
27 |
|
28 | function inlineTest(title, $el, saveOptions, testOptions) {
|
29 | const svg = $el.html();
|
30 | const template = $('#inline-template').html();
|
31 | const row = $el.html(template);
|
32 | row.find('h2').text(title);
|
33 | row.find('.canvas').html(svg);
|
34 |
|
35 | const canvas = row.find(testOptions && testOptions.selector || 'svg')[0];
|
36 | svgAsPngUri(canvas, saveOptions, (uri, width, height) => row.find('.preview').html(
|
37 | '<div>' +
|
38 | '<img src="' + uri + '" />' +
|
39 | '<div>width=' + width + ', height=' + height + '</div>' +
|
40 | '</div>'
|
41 | ));
|
42 |
|
43 | row.find('.save').click(() => saveSvgAsPng(canvas, 'test.png', saveOptions));
|
44 | }
|
45 |
|
46 | inlineTest('Directly in the HTML', $('#inline'));
|
47 | inlineTest('With linked PNG image', $('#embedded-png'));
|
48 | inlineTest('With linked SVG image', $('#embedded-svg'));
|
49 | inlineTest('Sized with pixels', $('#sized-with-pixels'));
|
50 | inlineTest('Sized with style', $('#sized-with-style'));
|
51 | inlineTest('Sized with CSS', $('#sized-with-css'));
|
52 | inlineTest('At a higher resolution', $('#scaling'), {scale: 2});
|
53 | inlineTest('When CSS styling selectors are prefixed', $('#selectors-prefixed'), {
|
54 | selectorRemap: s => s.replace('#selectors-prefixed ', '')
|
55 | });
|
56 | inlineTest('Modifying the style', $('#modified-style'), {
|
57 | modifyStyle: s => s.replace('green', 'red')
|
58 | });
|
59 | inlineTest('Modifying the whole CSS rule', $('#modified-css'), {
|
60 | modifyCss: (selector, properties) => {
|
61 | selector = selector.replace('#selectors-prefixed ', '');
|
62 | properties = properties.replace('green', 'blue');
|
63 | return selector + '{' + properties + '}';
|
64 | }
|
65 | });
|
66 | inlineTest('Exporting a group within an SVG', $('#group'), null, {
|
67 | selector: '#sub-group'
|
68 | });
|
69 | inlineTest('Percentage Height and Width', $('#percentage-size'));
|
70 | inlineTest('Background color', $('#background-color'), {backgroundColor: 'lightblue'});
|
71 | inlineTest('Pan and Zoom', $('#pan-and-zoom'), {
|
72 | left: -50,
|
73 | top: -50,
|
74 | width: 300,
|
75 | height: 300
|
76 | });
|
77 | inlineTest('With Unicode characters', $('#unicode'));
|
78 | inlineTest('With gradients', $('#gradient'));
|
79 | inlineTest('With foreign objects', $('#foreign-object'));
|
80 | inlineTest('With opacity', $('#opacity'));
|
81 | inlineTest('When setting xmlns on foreign object children', $('#xmlns-override'));
|
82 | inlineTest('When using HTML entites', $('#entities'));
|
83 | inlineTest('Transformed text', $('#transformed-text'));
|
84 | inlineTest('With marker-end', $('#marker-end'));
|
85 | inlineTest('SVG style attribute', $('#style-background'));
|
86 | inlineTest('With custom fonts', $('#custom-font'));
|
87 |
|
88 | const $sandbox = $('#sandbox');
|
89 | $sandbox.find('.render').click(() => {
|
90 | $sandbox.find('.error').hide().text('');
|
91 | $sandbox.find('.load-target').html($('#sandbox textarea').val());
|
92 | const canvas = $sandbox.find('.load-target svg')[0];
|
93 | try {
|
94 | svgAsPngUri(canvas, null, (uri, width, height) => $sandbox.find('.preview').html(
|
95 | '<div>' +
|
96 | '<img src="' + uri + '" />' +
|
97 | '<div>width=' + width + ', height=' + height + '</div>' +
|
98 | '</div>'
|
99 | ));
|
100 | $sandbox.find('.save').unbind('click').click(() => saveSvgAsPng(canvas, 'test.png'));
|
101 | } catch(err) {
|
102 | $sandbox.find('.error').show().text(err.message);
|
103 | $sandbox.find('.preview').html('');
|
104 | }
|
105 | });
|