forked from exupero/saveSvgAsPng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
106 lines (100 loc) · 3.99 KB
/
index.js
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
function handleFileSelect(evt) {
const $el = $('#filereader');
const files = evt.target.files;
const file = files.length > 0 ? files[0] : null;
if (file) {
const reader = new FileReader();
reader.onload = e => {
$el.find('.load-target').html(e.target.result);
svgAsPngUri($el.find('.load-target svg')[0], null, (uri, width, height) => {
$el.find('input').hide();
$el.find('.preview').html(
'<div>' +
'<img src="' + uri + '" />' +
'<div>width=' + width + ', height=' + height + '</div>' +
'</div>'
);
});
$el.find('.save').click(() => saveSvgAsPng($el.find('.load-target svg')[0], 'test.png'));
};
reader.readAsText(file);
}
}
if (window.File && window.FileReader && window.FileList && window.Blob) {
document.getElementById('file').addEventListener('change', handleFileSelect, false);
}
function inlineTest(title, $el, saveOptions, testOptions) {
const svg = $el.html();
const template = $('#inline-template').html();
const row = $el.html(template);
row.find('h2').text(title);
row.find('.canvas').html(svg);
const canvas = row.find(testOptions && testOptions.selector || 'svg')[0];
svgAsPngUri(canvas, saveOptions, (uri, width, height) => row.find('.preview').html(
'<div>' +
'<img src="' + uri + '" />' +
'<div>width=' + width + ', height=' + height + '</div>' +
'</div>'
));
row.find('.save').click(() => 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: s => s.replace('#selectors-prefixed ', '')
});
inlineTest('Modifying the style', $('#modified-style'), {
modifyStyle: s => s.replace('green', 'red')
});
inlineTest('Modifying the whole CSS rule', $('#modified-css'), {
modifyCss: (selector, properties) => {
selector = selector.replace('#selectors-prefixed ', '');
properties = properties.replace('green', 'blue');
return selector + '{' + properties + '}';
}
});
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('Transformed text', $('#transformed-text'));
inlineTest('With marker-end', $('#marker-end'));
inlineTest('SVG style attribute', $('#style-background'));
inlineTest('SVG within SVG', $('#svg-in-svg'));
inlineTest('With custom fonts', $('#custom-font'));
const $sandbox = $('#sandbox');
$sandbox.find('.render').click(() => {
$sandbox.find('.error').hide().text('');
$sandbox.find('.load-target').html($('#sandbox textarea').val());
const canvas = $sandbox.find('.load-target svg')[0];
try {
svgAsPngUri(canvas, null, (uri, width, height) => $sandbox.find('.preview').html(
'<div>' +
'<img src="' + uri + '" />' +
'<div>width=' + width + ', height=' + height + '</div>' +
'</div>'
));
$sandbox.find('.save').unbind('click').click(() => saveSvgAsPng(canvas, 'test.png'));
} catch(err) {
$sandbox.find('.error').show().text(err.message);
$sandbox.find('.preview').html('');
}
});