Skip to content

Commit 47a25a2

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 e0802b1 commit 47a25a2

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

src/lib/index.js

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

736+
var IS_IE9_OR_BELOW_REGEX = /MSIE [1-9]\./;
737+
lib.isIE9orBelow = function() {
738+
return lib.isIE() && IS_IE9_OR_BELOW_REGEX.test(window.navigator.userAgent);
739+
};
740+
736741
var IS_SAFARI_REGEX = /Version\/[\d\.]+.*Safari/;
737742
lib.isSafari = function() {
738743
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

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

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

8578
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)