Canvas Operations
Previous
Data
Next
Element Operations
Loading...
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, 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 layergetLayers()
: Get all layersgetCamera()
: Get the camera instancegetRoot()
: Get the root nodesetCursor(cursor: string)
: Set the mouse cursor styleExample
// Get the canvas instanceconst canvas = graph.getCanvas();// Get the main layerconst mainLayer = canvas.getLayer('main');// Set the mouse cursor stylecanvas.setCursor('pointer');// Get the root node of the canvasconst root = canvas.getRoot();
Get the size of the current canvas container. Returns an array containing the width and height.
getSize(): [number, number];
Example
// Get the canvas sizeconst [width, height] = graph.getSize();console.log('Canvas width:', width);console.log('Canvas height:', height);// Use the size information for calculationsconst centerX = width / 2;const centerY = height / 2;
Set the size of the canvas container. This method will update both the canvas and container size.
setSize(width: number, height: number): void;
Parameters
Parameter | Description | Type | Default | Required |
---|---|---|---|---|
width | Canvas width (pixels) | number | - | ✓ |
height | Canvas height (pixels) | number | - | ✓ |
Example
// Set a fixed sizegraph.setSize(800, 600);