logo

G6

  • Docs
  • API
  • Playground
  • Community
  • Productsantv logo arrow
  • 5.0.45
  • Data
  • Canvas Operations
  • Element Operations
  • Graph Instance
  • Drawing and Rendering
  • Viewport Operations
  • Layout
  • Graph Options
  • Behavior
  • Plugin
  • Theme
  • Data Transformation
  • Event Listening
  • Coordinate Transformation
  • Export Image

Export Image

Previous
Coordinate Transformation

Resources

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-互动图形解决方案
xtechLiven Experience technology
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

Overview of Image Export

G6 provides the functionality to export the graph as an image, allowing you to export the current canvas content as a DataURL format. This is convenient for saving, sharing, or further processing. The exported image will retain all visible elements on the canvas, including nodes, edges, combos, and other custom graphics.

API Reference

Graph.toDataURL(options)

Export the current canvas as an image in DataURL format.

toDataURL(options?: Partial<DataURLOptions>): Promise<string>;

Parameters

ParameterDescriptionTypeDefaultRequired
optionsExport image configurationPartial<DataURLOptions>-

Return Value

Returns a Promise that resolves to a DataURL string representing the image.

DataURLOptions Type Definition

ParameterTypeRequiredDescription
mode'viewport' | 'overall'NoExport mode
- viewport: Export viewport content
- overall: Export entire canvas
type'image/png' | 'image/jpeg' | 'image/webp'NoImage type
- image/png: PNG format
- image/jpeg: JPEG format
- image/webp: WebP format
encoderOptionsnumberNoImage quality, only effective for image/jpeg and image/webp, range 0 ~ 1

Download Image

G6 5.0 only provides an API to export the canvas as a Base64 image (toDataURL). If you need to download the image, you can use the following method:

async function downloadImage() {
const dataURL = await graph.toDataURL();
const [head, content] = dataURL.split(',');
const contentType = head.match(/:(.*?);/)![1];
const bstr = atob(content);
let length = bstr.length;
const u8arr = new Uint8Array(length);
while (length--) {
u8arr[length] = bstr.charCodeAt(length);
}
const blob = new Blob([u8arr], { type: contentType });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'graph.png';
a.click();
}

Note

The exported image content may not include the complete canvas content. The export range only includes the content within the Graph canvas. Some plugins use custom containers, canvases, etc., which will not appear in the exported image.