diff --git a/src/svg/Painter.ts b/src/svg/Painter.ts index c159f722f..51a4d8ca7 100644 --- a/src/svg/Painter.ts +++ b/src/svg/Painter.ts @@ -20,7 +20,7 @@ import { createSVGVNode } from './core'; import { normalizeColor, encodeBase64 } from './helper'; -import { defaults, extend, keys, logError, map } from '../core/util'; +import { extend, keys, logError, map, retrieve2 } from '../core/util'; import Path from '../graphic/Path'; import patch, { updateAttrs } from './patch'; import { getSize } from '../canvas/helper'; @@ -119,7 +119,8 @@ class SVGPainter implements PainterBase { renderToVNode(opts?: { animation?: boolean willUpdate?: boolean - compress?: boolean + compress?: boolean, + useViewBox?: boolean }) { opts = opts || {}; @@ -176,24 +177,27 @@ class SVGPainter implements PainterBase { } } - return createSVGVNode(width, height, children); + return createSVGVNode(width, height, children, opts.useViewBox); } renderToString(opts?: { /** * If add css animation. + * @default true */ - cssAnimation: boolean + cssAnimation?: boolean + /** + * If use viewBox + * @default true + */ + useViewBox?: boolean }) { - const defaultOpts = { - cssAnimation: true - }; - opts = defaults(opts || {}, defaultOpts); - + opts = opts || {}; return vNodeToString(this.renderToVNode({ - animation: defaultOpts.cssAnimation, + animation: retrieve2(opts.cssAnimation, true), willUpdate: false, - compress: true + compress: true, + useViewBox: retrieve2(opts.useViewBox, true) }), { newline: true }); } diff --git a/src/svg/core.ts b/src/svg/core.ts index 511b65a13..412038a44 100644 --- a/src/svg/core.ts +++ b/src/svg/core.ts @@ -174,7 +174,12 @@ export function createBrushScope(zrId: string): BrushScope { }; } -export function createSVGVNode(width?: number | string, height?: number | string, children?: SVGVNode[]) { +export function createSVGVNode( + width: number | string, + height: number | string, + children?: SVGVNode[], + useViewBox?: boolean +) { return createVNode( 'svg', 'root', @@ -184,7 +189,8 @@ export function createSVGVNode(width?: number | string, height?: number | string 'xmlns': SVGNS, 'xmlns:xlink': XLINKNS, 'version': '1.1', - 'baseProfile': 'full' + 'baseProfile': 'full', + 'viewBox': useViewBox ? `0 0 ${width} ${height}` : false }, children );