Remarks
- It is possible to export only a part of the contents, see worldBounds and worldPoints.
- The size of the resulting image is controlled by the scale property. For a given target width or height, the appropriate scale can be calculated using calculateScaleForWidth and calculateScaleForHeight.
See Also
Developer's Guide
Members
Constructors
worldBounds are of little use. Instead, after creating the SvgExport a set of worldPoints should be set. The view coordinate bounds around those points then define the rectangular export area. Note that the projection property must also be set correctly for a projection to apply to the exported content.Parameters
- worldBounds: Rect
- the bounds of the content to export, see worldBounds
- scale?: number
- the scale, see scale
Examples
const exporter = new SvgExport(graphComponent.contentBounds)
// configure the export instance
exporter.encodeImagesBase64 = true
exporter.inlineSvgImages = true
// create the <svg> element
const element = exporter.exportSvg(graphComponent)Properties
Gets or sets the background color for the exported SVG.
For a value of null or TRANSPARENT, the exported SVG will have no background element. For all other values, the exported SVG will have a background element with corresponding fill.
The default value is null.
Gets or sets a value indicating whether children of the CanvasComponent's defs section that aren't known to the SvgDefsManager should be copied to the exported SVG.
false.If set to a non-empty string, this string will be added to the created SVG enclosed by a <style> element. If set to an empty string, no <style> element will be added to the created SVG. If set to null, the SvgExport will automatically extract all style sheets available to the document through the CSSStyleSheet API and filters all rules that don't match any elements in the created SVG.
Note that setting this property to null might affect the runtime performance of the export significantly. Copying and filtering the style rules has a runtime complexity of roughly the number of elements in the SVG times the number of CSS rules.
Note also, that all style sheets that cannot be accessed because of CORS policies are ignored.
The default value is "".
See Also
Developer's Guide
Gets or sets a value indicating whether all external images should be inlined and encoded as Base64 data URIs in exportSvgAsync.
Override shouldEncodeImageBase64 to control the encoding and embedding for each image element separately.
Note that images with blob URLs are invalid outside the current document and should be Base64-encoded when using the exported SVG in a different context.
The default value is false.
See Also
Developer's Guide
API
- exportSvgAsync, shouldEncodeImageBase64
See Also
Gets or sets a value indicating whether all SVG images should be inlined in the SVG element in exportSvgAsync.
Override shouldInlineSvgImage to control the inlining for each image separately.
The default value is false.
See Also
Developer's Guide
API
- exportSvgAsync, shouldInlineSvgImage
Gets or sets the margins for the exported image.
Gets or sets the projection to apply to the exported image.
Gets or sets a value indicating whether HtmlVisuals should be converted to bitmap images in the resulting SVG.
In the SVG document returned by exportSvg, HTML visuals will be included as nested <foreignObject> elements. For better portability of the exported SVG document, HTML visuals can be rendered as bitmaps, instead. This is achieved by rendering the individual <foreignObject>s as SVG images to a <canvas> and replacing the <foreignObject>s with an SVG image with the exported data url.
The default value is false
<foreignObject> children. This only affects rendering but the SVG document is still correct.See Also
Developer's Guide
A scale of 1 preserves the original size, a scale of 0.5 results in a target image with half the original size and so on.
This value has to be strictly greater than 0 and finite. Its default value is 1.0
false, i.e. a fallback image is used.Gets or sets the bounds of the content to export in world coordinates.
See Also
Developer's Guide
Gets or sets the points in world coordinates that are enclosed in the exported area.
See Also
Developer's Guide
Gets or sets the zoom property to use during the creation of the visualization.
In contrast to the scale property, which works on the output graphics, this property determines what zoom value is to be assumed on the canvas when creating the visual. This can affect the rendering of zoom-dependent visuals, especially level-of-detail rendering.
This value has to be strictly greater than 0. Its default value is 1.0
Methods
Parameters
- height: number
- the height of the target image
Return Value
- number
- the scale value to use
Parameters
- width: number
- the width of the target image
Return Value
- number
- the scale value to use
Returns a suitable clip to use for the visual for this exporter.
Returns an initialized IRenderContext and changes the given transform and the clip bounds such that a part of the given CanvasComponent instance can be exported to an image.
Parameters
- canvasComponent: CanvasComponent
- The CanvasComponent instance to export.
Return Value
- IRenderContext
- An IRenderContext to use.
Returns a suitable transform to use for the visual to paint in the world coordinate system for this export.
Exports the contents of the CanvasComponent to a single, standalone SVG element.
Parameters
- canvas: CanvasComponent
- The canvas to be exported.
Return Value
- SVGSVGElement
- An SVG element with the canvas contents.
See Also
Developer's Guide
exportSvgAsync
(canvas: CanvasComponent, renderCompletionCallback?: function): Promise<SVGSVGElement>Exports the contents of the CanvasComponent to a single, standalone SVG element.
exportSvgAsync
(canvas: CanvasComponent, renderCompletionCallback?: function): Promise<SVGSVGElement>In addition, images are inlined into the SVG using base64 encoding if encodeImagesBase64 is set to true. Image inlining can be controlled for each image individually by overwriting the predicate method shouldEncodeImageBase64. Note that images with blob URLs are invalid outside the current document and should be Base64-encoded when using the exported SVG in a different context.
SVG image elements are replaced with a copy of the referenced SVG element if inlineSvgImages is set to true. Image encoding can be controlled for each image individually by overwriting the predicate method shouldInlineSvgImage.
When rasterizeHtmlVisuals is enabled, HTML Visuals will be exported as bitmap images rather than wrapped in <foreignObject> elements.
The renderCompletionCallback parameter can be used to await any asynchronous rendering tasks to be finished before the optional post-processing steps are applied. This can be useful in case you are using styles that rely on frameworks like Angular or React, which render asynchronously by default, so you might need to wait for the framework to generate the final DOM structure.
Parameters
- canvas: CanvasComponent
- The canvas to be exported.
- renderCompletionCallback?: function
- An optional callback that will be called after the initial SVG is generated. The returned promise is awaited before any optional post-processing steps are applied.
Return Value
- Promise<SVGSVGElement>
- A
Promisethat will resolve to an SVG element with the canvas contents.
See Also
Developer's Guide
Fixes the viewBox attribute on SVG images that have been inlined for export.
viewBox attribute on SVG images that have been inlined for export.image tag should be inlined, but doesn't have a viewBox attribute, the resulting element might be rendered with wrong dimensions. This specific implementation sets the viewBox attribute to the natural dimensions of the original image.Parameters
- importedSvg: Element
- The root element of the SVG file that's referenced by the original image element.
- originalImage: any
- The original image element that has been inlined.
Return Value
- Promise<void>
- A Promise that will be fulfilled once the
viewBoxattribute has been set.
See Also
<image> element, this implementation copies the values of the x, y, width, height, and preserveAspectRatio attributes from the original image element to the imported SVG element, and wraps the SVG element in a g element to support the transform of the original image. For HTML <img> elements, the style and class attributes are copied to a wrapping <div> element. If the original image element has an id attribute, it is also copied to the new container element.Parameters
- importedSvg: Element
- The root element of the SVG file that's referenced by the original image element.
- originalImage: any
- The original image element that will be replaced.
Return Value
- Element
- The element that's actually placed in the DOM as a replacement of the
originalImage.
See Also
image.Parameters
- image: SVGImageElement
- The image element that refers to the image to be encoded.
Return Value
- boolean
- Whether the given image should be encoded.
See Also
Developer's Guide
image.Parameters
- image: SVGImageElement
- The image to be inlined.
Return Value
- boolean
- Whether the given image should be inlined.
See Also
Developer's Guide
Static Methods
Parameters
- svgXml: string
- A string containing the SVG document.
Return Value
- string
- A data URI of the SVG document.