logo

X6

  • Tutorials
  • API
  • Examples
  • Q&A
  • Change Log
  • XFlow
  • Productsantv logo arrow
  • 2.x
  • Introduction
  • Quickstart
  • Basic
    • Graph
    • Nodes
    • Edges
    • Connection Pile
    • Interaction
    • Events
    • Data Serialization
  • Intermediate
    • Connection Points
    • Tools
    • Group
    • React Nodes
    • Vue Nodes
    • Angular Nodes
    • HTML Nodes
  • Plugin
    • Graphic Transformations
    • Snapline
    • Clipboard
    • Keyboard
    • History
    • Selection Box
    • Scroller
    • Dnd
    • Mini Map
    • Stencil
    • Export
  • Upgrade to Version 2.x
  • Developer Tools

Export

Previous
Stencil
Next
Upgrade to Version 2.x

Resource

Ant Design
Galacea Effects
Umi-React Application Framework
Dumi-Component doc generator
ahooks-React Hooks Library

Community

Ant Financial Experience Tech
seeconfSEE Conf-Experience Tech Conference

Help

GitHub
StackOverflow

more productsMore Productions

Ant DesignAnt Design-Enterprise UI design language
yuqueYuque-Knowledge creation and Sharing tool
EggEgg-Enterprise-class Node development framework
kitchenKitchen-Sketch Tool set
GalaceanGalacean-Interactive solution
xtechLiven Experience technology
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

By reading this section, you can learn about

  • How to export the canvas content in image format

Usage

We often need to export the content of a canvas as an image. We provide a standalone plugin package @antv/x6-plugin-export to use this feature.

# npm
$ npm install @antv/x6-plugin-export --save
# yarn
$ yarn add @antv/x6-plugin-export

Then we use it in the code like this:

import { Export } from '@antv/x6-plugin-export'
const graph = new Graph({
background: {
color: '#F2F7FA',
},
})
graph.use(new Export())

API

graph.exportSVG(...)

exportSVG(fileName?: string, options?: Export.ToSVGOptions): void

fileName is the name of the file, defaulting to chart. Export.ToSVGOptions is described as follows:

Property NameTypeDefault ValueRequiredDescription
preserveDimensionsboolean | Size-preserveDimensions controls the size of the exported SVG. If not set, width and height default to 100%; if set to true, width and height will be automatically calculated to the actual size of the graphic area.
viewBoxRectangle.RectangleLike-Sets the viewBox of the exported SVG.
copyStylesbooleantrueWhether to copy styles from external stylesheets, default is true. When copyStyles is enabled, all stylesheets will be disabled during the export process, which may cause a brief loss of styles on the page. If the effect is particularly poor, you can set copyStyles to false.
stylesheetstring-Custom stylesheet.
serializeImagesbooleantrueWhether to convert the xlink:href links of image elements to dataUri format.
beforeSerialize(this: Graph, svg: SVGSVGElement) => any-You can call beforeSerialize to modify the SVG string before exporting it.

graph.exportPNG(...)

exportPNG(fileName?: string, options?: Export.ToImageOptions): void

fileName is the name of the file, defaulting to chart. Export.ToImageOptions inherits from the above Export.ToSVGOptions and has the following additional configurations:

Property NameTypeDefault ValueRequiredDescription
widthnumber-Width of the exported image.
heightnumber-Height of the exported image.
backgroundColorstring-Background color of the exported image.
paddingNumberExt.SideOptions-Padding for the image.
qualitynumber-Image quality, which can be selected from a range of 0 to 1. If out of range, the default value of 0.92 will be used.

graph.exportJPEG(...)

exportJPEG(fileName?: string, options?: Export.ToImageOptions): void

graph.toSVG(...)

toSVG(callback: (dataUri: string) => any, options?: Export.ToSVGOptions): void

graph.toPNG(...)

toPNG(callback: (dataUri: string) => any, options?: Export.ToImageOptions): void

graph.toJPEG(...)

toJPEG(callback: (dataUri: string) => any, options?: Export.ToImageOptions): void