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

Event

Previous
Scenegraph & Lifecycle
Next
OffscreenCanvas & Server-side Rendering

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

In the event system, most events bubble up to the canvas. For example, if we click Circle in the following simple scenario, we can see the propagation path of the events in order.

Circle -> Group(canvas.document.documentElement) -> Document(canvas.document) -> Canvas:
canvas.addEventListener('click', (e) => {
e.propagationPath(); // [Circle, Group, Document, Canvas]
});

Add/removeEventListener

Events can be bound on both the Canvas and the root node of the canvas.

canvas.addEventListener('click', () => {});
// or
canvas.document.addEventListener('click', () => {});

More event-related operations are described in event system.

Canvas-specific events

The canvas will trigger corresponding events before and after initialization, rendering, and currently the following canvas-related events can be listened to.

export enum CanvasEvent {
READY = 'ready',
BEFORE_RENDER = 'beforerender',
AFTER_RENDER = 'afterrender',
BEFORE_DESTROY = 'beforedestroy',
AFTER_DESTROY = 'afterdestroy',
RESIZE = 'resize',
}

For example, if we show the live frame rate in all the examples on the website, which is updated after each render, we can do it by listening to the afterrender event.

import { CanvasEvent } from '@antv/g';
canvas.addEventListener(CanvasEvent.AFTER_RENDER, () => {
stats.update();
});
// or
canvas.addEventListener('afterrender', () => {
stats.update();
});

ready event

In the browser, we can use window.onload to find out if the initialization of the page, including HTML parsing, style parsing, resource loading, etc., is complete.

// @see https://javascript.info/onload-ondomcontentloaded
window.onload = function () {
alert('Page loaded');
};

Also in G these initializations are asynchronous, and we provide a similar ready event. After the initialization is done you can do things like scene graph creation.

canvas.addEventListener(CanvasEvent.READY, () => {
canvas.appendChild(circle);
});

In addition to listening to the ready event, you can also choose to wait for this Promise.

await canvas.ready;
canvas.appendChild(circle);