Skip to content

Commit 575c47b

Browse files
committed
use createObjectURL during svgToImg
- which speeds up things for loooong SVG strings - must (still) use encodeSVG in Safari and IE9
1 parent 4e24ed2 commit 575c47b

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

src/lib/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,14 @@ lib.isIE = function() {
733733
return typeof window.navigator.msSaveBlob !== 'undefined';
734734
};
735735

736+
lib.isIE9orBelow = function() {
737+
return (
738+
lib.isIE() &&
739+
typeof window.navigator !== 'undefined' &&
740+
/MSIE [1-9]\./.test(window.navigator.userAgent)
741+
);
742+
};
743+
736744
var IS_SAFARI_REGEX = /Version\/[\d\.]+.*Safari/;
737745
lib.isSafari = function() {
738746
return IS_SAFARI_REGEX.test(window.navigator.userAgent);

src/snapshot/filesaver.js

+1-8
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function fileSaver(url, name, format) {
3131
var blob;
3232
var objectUrl;
3333

34-
if(isIE9orBelow()) {
34+
if(Lib.isIE9orBelow()) {
3535
reject(new Error('IE < 10 unsupported'));
3636
}
3737

@@ -73,12 +73,5 @@ function fileSaver(url, name, format) {
7373
return promise;
7474
}
7575

76-
function isIE9orBelow() {
77-
return (
78-
Lib.isIE() &&
79-
typeof window.navigator !== 'undefined' &&
80-
/MSIE [1-9]\./.test(window.navigator.userAgent)
81-
);
82-
}
8376

8477
module.exports = fileSaver;

src/snapshot/svgtoimg.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,24 @@ function svgToImg(opts) {
4343

4444
var ctx = canvas.getContext('2d');
4545
var img = new Image();
46+
var svgBlob, url;
4647

47-
// for Safari support, eliminate createObjectURL
48-
// this decision could cause problems if content
49-
// is not restricted to svg
50-
var url = helpers.encodeSVG(svg);
48+
if(format === 'svg' || Lib.isIE9orBelow() || Lib.isSafari()) {
49+
url = helpers.encodeSVG(svg);
50+
} else {
51+
svgBlob = helpers.createBlob(svg, 'svg');
52+
url = helpers.createObjectURL(svgBlob);
53+
}
5154

5255
canvas.width = w1;
5356
canvas.height = h1;
5457

5558
img.onload = function() {
5659
var imgData;
5760

61+
svgBlob = null;
62+
helpers.revokeObjectURL(url);
63+
5864
// don't need to draw to canvas if svg
5965
// save some time and also avoid failure on IE
6066
if(format !== 'svg') {
@@ -92,6 +98,9 @@ function svgToImg(opts) {
9298
};
9399

94100
img.onerror = function(err) {
101+
svgBlob = null;
102+
helpers.revokeObjectURL(url);
103+
95104
reject(err);
96105
// eventually remove the ev
97106
// in favor of promises

0 commit comments

Comments
 (0)