logo

G

  • Tutorials
  • API
  • Examples
  • Plugins
  • Productsantv logo arrow
  • 6.1.26
  • Canvas
    • Introduction
    • Options
    • Built-in objects
    • Coordinate system
    • Scenegraph & Lifecycle
    • Event
    • OffscreenCanvas & Server-side Rendering
    • CustomElementRegistry
    • Frequently Asked Questions
  • Renderer
    • Introduction
    • Canvas Renderer
    • Canvaskit Renderer
    • SVG Renderer
    • WebGL Renderer
    • WebGPU Renderer
    • Custom Renderer
  • Camera
    • Introduction
    • Camera Parameters
    • Camera action
    • Camera animation
  • Event
    • Introduction
    • Event Object
    • Gesture & Drag'n'Drop
    • Frequently Asked Questions
  • Animation
    • Web Animations API
    • Lottie
  • Basic Shapes
    • Basic Concepts
    • DisplayObject
    • Group
    • Text
    • Circle
    • Ellipse
    • Rect
    • Image
    • Line
    • Polygon
    • Polyline
    • Path
    • HTML
  • Style System
    • Introduction
    • CSS Typed OM
    • Inheritance
    • CSS Properties & Values API
    • CSS Layout API
    • Pattern
    • Gradient
  • 3D
    • 材质
    • 几何
    • Mesh
    • 光源
    • 雾
    • 交互
  • Built-in Objects
    • EventTarget
    • Node
    • Element
    • Document
    • MutationObserver
    • Utils
  • GPGPU
    • Introduction
    • Programming Model
    • Kernel API
    • Principles of classical GPGPU implementation
    • webgpu-graph
  • Declarative programming
    • 使用 Web Components
  • Devtools
    • G 开发者工具
    • 内置的渲染统计信息
    • 第三方开发调试工具

Scenegraph & Lifecycle

Previous
Coordinate system
Next
Event

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...

Add/remove scene graph nodes

Since canvas does not inherit from Node, it does not have node manipulation capability by itself. However, we have added some shortcuts and the following node operations are essentially done on the root node, e.g. the following two writes are equivalent:

canvas.appendChild(circle);
canvas.document.documentElement.appendChild(circle);

appendChild

Adds the object to be rendered to the canvas. If the object has children, they are also added together.

const circle = new Circle({ style: { r: 10 } });
canvas.appendChild(circle);
// or canvas.document.documentElement.appendChild(circle);

removeChild

Removes the object from the canvas. If the object has children, they are removed as well.

canvas.removeChild(circle);
// or canvas.document.documentElement.removeChild(circle);

To be consistent with the DOM API, just removing the object does not destroy it. If you want to destroy it, you need to call destroy().

removeChildren

Removes all objects in the canvas.

canvas.removeChildren();
// or canvas.document.documentElement.removeChildren();

destroyChildren

Removes and destroys all objects in the canvas.

canvas.destroyChildren();

Lifecycle

The initialization logic is performed upon instantiation, and the following lifecycle methods can be called afterwards.

ready

When initialization is complete, a Promise is returned that is equivalent to listening for the CanvasEvent.READY event.

await canvas.ready;
// or
import { CanvasEvent } from '@antv/g';
canvas.addEventListener(CanvasEvent.READY, () => {});

render

Rendering the canvas, since the renderer has auto-rendering enabled by default, there is no need to call it manually in most cases. However, some scenes require manual control of rendering timing, in which case rendering-on-demand example.

const webglRenderer = new WebGLRenderer({
enableAutoRendering: false,
});
canvas.render();

destroy(destroyScenegraph = true)

Destroy the canvas, executing the following destruction logic in turn.

  • If auto-rendering is enabled, stop the main rendering loop.
  • Remove the entire scene graph from the canvas, and destroy it if destroyScenegraph is set.
  • Destroying the rendering context.
// Destroy the canvas only, keep the scene graph
canvas.destroy();
// Destroy the scene graph in the canvas together
canvas.destroy(true);