Skip to content

Commit b013719

Browse files
authored
Merge pull request #1145 from ecomfe/fix/svg/encodeBase64-compatibility
fix(svg): enhance SVG encodeBase64 compatibility
2 parents 93d315f + 4df3af4 commit b013719

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

src/svg/helper.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
import { MatrixArray } from '../core/matrix';
44
import Transformable, { TransformProp } from '../core/Transformable';
5-
import { RADIAN_TO_DEGREE, retrieve2, logError, isFunction } from '../core/util';
5+
import { RADIAN_TO_DEGREE, retrieve2, logError } from '../core/util';
66
import Displayable from '../graphic/Displayable';
77
import { GradientObject } from '../graphic/Gradient';
88
import { LinearGradientObject } from '../graphic/LinearGradient';
99
import Path from '../graphic/Path';
1010
import { ImagePatternObject, PatternObject, SVGPatternObject } from '../graphic/Pattern';
1111
import { RadialGradientObject } from '../graphic/RadialGradient';
1212
import { parse } from '../tool/color';
13-
import env from '../core/env';
1413

1514
const mathRound = Math.round;
1615

@@ -173,14 +172,14 @@ export function getSRTTransformString(
173172
}
174173

175174
export const encodeBase64 = (function () {
176-
if (env.hasGlobalWindow && isFunction(window.btoa)) {
175+
if (typeof Buffer !== 'undefined' && typeof Buffer.from === 'function') {
177176
return function (str: string) {
178-
return window.btoa(unescape(encodeURIComponent(str)));
177+
return Buffer.from(str).toString('base64');
179178
};
180179
}
181-
if (typeof Buffer !== 'undefined') {
180+
if (typeof btoa === 'function' && typeof unescape === 'function' && typeof encodeURIComponent === 'function') {
182181
return function (str: string) {
183-
return Buffer.from(str).toString('base64');
182+
return btoa(unescape(encodeURIComponent(str)));
184183
};
185184
}
186185
return function (str: string): string {

test/svg-ssr.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,38 @@
268268
document.getElementById('main').innerHTML = svg;
269269
console.timeEnd('render');
270270

271+
// base64
272+
console.log('window SSR base64:');
273+
console.log(zr.painter.toDataURL(true));
274+
275+
// worker base64
276+
const workerSource = `
277+
importScripts('${location.origin}/dist/zrender.js')
278+
const zr = zrender.init(null, {
279+
renderer: 'svg',
280+
ssr: true,
281+
width: 1200,
282+
height: 1200
283+
});
284+
const el = new zrender.Circle({
285+
x: 50,
286+
y: 50,
287+
shape: {
288+
cx: 0,
289+
cy: 0,
290+
r: 25
291+
},
292+
style: {
293+
fill: 'red'
294+
}
295+
});
296+
zr.add(el);
297+
console.log('worker SSR base64:');
298+
console.log(zr.painter.toDataURL(true));
299+
zr.dispose();
300+
`;
301+
new Worker(URL.createObjectURL(new Blob([workerSource], { type: 'application/javascript' })));
302+
271303
// var blob = new Blob([svg], { type: 'image/svg+xml' });
272304
// var a = document.createElement('a');
273305
// a.download = 'aaa.svg';

0 commit comments

Comments
 (0)