UNPKG

4.04 kBJavaScriptView Raw
1function 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
24if (window.File && window.FileReader && window.FileList && window.Blob) {
25 document.getElementById('file').addEventListener('change', handleFileSelect, false);
26}
27
28function 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
46inlineTest('Directly in the HTML', $('#inline'));
47inlineTest('With linked PNG image', $('#embedded-png'));
48inlineTest('With linked SVG image', $('#embedded-svg'));
49inlineTest('Sized with pixels', $('#sized-with-pixels'));
50inlineTest('Sized with style', $('#sized-with-style'));
51inlineTest('Sized with CSS', $('#sized-with-css'));
52inlineTest('At a higher resolution', $('#scaling'), {scale: 2});
53inlineTest('When CSS styling selectors are prefixed', $('#selectors-prefixed'), {
54 selectorRemap: s => s.replace('#selectors-prefixed ', '')
55});
56inlineTest('Modifying the style', $('#modified-style'), {
57 modifyStyle: s => s.replace('green', 'red')
58});
59inlineTest('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});
66inlineTest('Exporting a group within an SVG', $('#group'), null, {
67 selector: '#sub-group'
68});
69inlineTest('Percentage Height and Width', $('#percentage-size'));
70inlineTest('Background color', $('#background-color'), {backgroundColor: 'lightblue'});
71inlineTest('Pan and Zoom', $('#pan-and-zoom'), {
72 left: -50,
73 top: -50,
74 width: 300,
75 height: 300
76});
77inlineTest('With Unicode characters', $('#unicode'));
78inlineTest('With gradients', $('#gradient'));
79inlineTest('With foreign objects', $('#foreign-object'));
80inlineTest('With opacity', $('#opacity'));
81inlineTest('When setting xmlns on foreign object children', $('#xmlns-override'));
82inlineTest('When using HTML entites', $('#entities'));
83inlineTest('Transformed text', $('#transformed-text'));
84inlineTest('With marker-end', $('#marker-end'));
85inlineTest('SVG style attribute', $('#style-background'));
86inlineTest('With custom fonts', $('#custom-font'));
87
88const $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});