logo

G

  • Tutorials
  • API
  • Examples
  • Plugins
  • Productsantv logo arrow
  • 6.1.26
  • Getting Started
  • Section I - Defining the Scenario
  • Section II - Using the renderer
  • Section III - Adding some interaction
  • Diving Deeper
    • Scene Graph
    • 创造一个“太阳系”
    • 使用相机
    • 使用插件
    • 实现一个简单的动画
    • GPGPU 初体验
    • 进入 3D 世界
    • 使用 Box2D 物理引擎
    • 使用 matter.js 物理引擎
    • 使用 Yoga 布局引擎
    • Takeover D3's rendering
    • Takeover Observable Plot's rendering
    • Choose a renderer
    • Rendering on demand
  • Advanced Topics
    • 性能优化
    • 自定义图形
    • 理解事件传播路径
    • Using react-g
    • Exporting the contents of the canvas
    • Using lite version

Section II - Using the renderer

Previous
Section I - Defining the Scenario
Next
Section III - Adding some interaction

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 this tutorial series, we will step-by-step implement a simple visualization scene that shows nodes and edges and gives them basic interaction capabilities such as dragging and picking.

In the previous section we defined a simple scene, in this section we will learn how to use renderer to complete the rendering.

  • Example of this section
  • DEMO in CodeSandbox

Choosing a renderer

First we need to introduce one or more renderers, and if we introduce more than one, we can also switch them at runtime. In this example we have selected only one Canvas2D renderer:

import { Renderer } from '@antv/g-canvas';
const renderer = new Renderer();

Creating a canvas

Then we need to create the canvas, using the renderer introduced above:.

const canvas = new Canvas({
container: 'container', // id of the DOM container
width: 600,
height: 500,
renderer,
});

Adding graphics to the canvas

With the canvas, we can add two nodes and an edge from the scene graph to the canvas, but of course we have to wait until the canvas is ready. We have two ways to know when the canvas is ready, either by listening to the ready event or waiting for the ready Promise to return.

canvas.addEventListener(CanvasEvent.READY, () => {
canvas.appendChild(node1);
canvas.appendChild(node2);
canvas.appendChild(edge);
});
// or
await canvas.ready;
canvas.appendChild(node1);
canvas.appendChild(node2);
canvas.appendChild(edge);

At this point you can see the rendering effect, but there is something strange, the edge appears on top of the node, even blocking the text:

abnormal effect

This problem is caused by the order in which we added the shapes to the canvas. We added the "edge" to the canvas last, and according to the painter's algorithm, it was drawn last, so it appears at the top.

The simplest solution is to modify the order, drawing the edges first and then the nodes.

canvas.appendChild(edge);
canvas.appendChild(node1);
canvas.appendChild(node2);

At this point the effect is normal.

normal effect

Alternatively, we can manually adjust the zIndex.

Setting the display order

Similar to zIndex in CSS, we can manually set the drawing order of the two nodes so that they are higher than the edge (default is 0):

node1.style.zIndex = 1;
node2.style.zIndex = 1;

The basic graphics are drawn, so let's add some interaction.