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 开发者工具
    • 内置的渲染统计信息
    • 第三方开发调试工具

Built-in objects

Previous
Options
Next
Coordinate system

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

We all know the window object in the browser, the entry point of the DOM tree is window.document, and the entry point usually contains a root node <html> element, which can be obtained from window.document.documentElement. We add various DOM elements to this root node, such as <head>, <body>, etc.

Canvas canvases can be analogous to window objects. Similarly, each canvas is created with a built-in entry Document, which can be obtained via canvas.document. This entry contains the root node of Scene Graph, which can be obtained via canvas.document.documentElement, and then you can add graphics to this root node via appendChild to complete the rendering. and then you can add graphics to this root node with appendChild to complete the rendering.

document

Returns a built-in Document object that holds the root node of the scene graph. After getting this root node via document.documentElement, you can add child nodes using the scene graph capability:

// append a Circle to canvas
canvas.document.documentElement.appendChild(circle);
canvas.document.documentElement.children; // [circle]

In addition to the add/remove node capability, other scene graph and event capabilities are also available on the root node:

canvas.document.documentElement.getBounds();
canvas.document.addEventListener('click', () => {});

document.documentElement

Alias of getRoot(), so the following two ways of writing it are equivalent:

const root = canvas.getRoot(); // Group
const root = canvas.document.documentElement;

getContextService

Get [rendering context](/en/api/renderer#rendering environment context), which is implemented by the renderer (g-canvas/svg/webgl). There are many common methods on this rendering context, such as:

  • getDomElement() Get the DOM element of current renderer, for example g-canvas/webgl will return a <canvas> element while g-svg will return a <svg> element.
  • getDPR() Get devicePixelRatio of current rendering context.

getCamera

Get camera and subsequently perform operations on that camera, such as switching projection mode, completing camera actions and animations, etc.

const camera = canvas.getCamera();
// camera actions
camera.pan();
camera.rotate();
// switch to perspective projection
camera
.setPosition(300, 100, 500)
.setFocalPoint(300, 250, 0)
.setPerspective(0.1, 1000, 75, 600 / 500);