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

Canvas Operations

Previous
Data
Next
Element Operations

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 Canvas Operations

G6 provides a series of canvas operation APIs to control and obtain basic information about the canvas. With these APIs, you can:

  • Get the canvas instance
  • Get and set the canvas size
  • Operate the canvas renderer and layers

API Reference

Graph.getCanvas()

Get the canvas instance, which can be used for low-level canvas operations.

getCanvas(): Canvas;

Return Value Description

The Canvas instance includes the following main functions:

  • getLayer(name?: string): Get the specified layer
  • getLayers(): Get all layers
  • getCamera(): Get the camera instance
  • getRoot(): Get the root node
  • setCursor(cursor: string): Set the mouse cursor style

Example

// Get the canvas instance
const canvas = graph.getCanvas();
// Get the main layer
const mainLayer = canvas.getLayer('main');
// Set the mouse cursor style
canvas.setCursor('pointer');
// Get the root node of the canvas
const root = canvas.getRoot();

Graph.getSize()

Get the size of the current canvas container. Returns an array containing the width and height.

getSize(): [number, number];

Example

// Get the canvas size
const [width, height] = graph.getSize();
console.log('Canvas width:', width);
console.log('Canvas height:', height);
// Use the size information for calculations
const centerX = width / 2;
const centerY = height / 2;

Graph.setSize(width, height)

Set the size of the canvas container. This method will update both the canvas and container size.

setSize(width: number, height: number): void;

Parameters

ParameterDescriptionTypeDefaultRequired
widthCanvas width (pixels)number-✓
heightCanvas height (pixels)number-✓

Example

// Set a fixed size
graph.setSize(800, 600);