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 III - Adding some interaction

Previous
Section II - Using the renderer
Next
Scene Graph

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 this section, we will learn how to make graphics respond to events, example of this section. The following APIs will be involved:

  • Using addEventListener
  • Using style
  • Using translateLocal

DEMO in CodeSandbox

Activate highlighting

We want node 1 to respond to an activation event: turn the node red when the mouse is moved in, change the mouse style, and restore it when it is moved out.

interaction

As with the DOM API, we add event listeners to the graphics via addEventListener to listen for mouseenter and mouseleave events.

node1.addEventListener('mouseenter', () => {
node1.style.fill = 'red';
});
node1.addEventListener('mouseleave', () => {
node1.style.fill = '#1890FF';
});

Then we can add the cursor property to the node to set the [mouse hover style](/en/api/basic/display-object#mouse style), here we use the "finger" shape pointer.

const node1 = new Circle({
style: {
//...
cursor: 'pointer',
},
});

Our event system is fully compatible with the DOM Event API, which means that it is possible to bind/unbind event listeners, trigger custom events, delegate events, and more using the familiar API on the front-end. Besides the fact that these method names are better remembered, we will see another big advantage of it in the next section.

Dragging

Dragging is a common interaction and we want to implement dragging for node 1 while changing the endpoint position of the edge.

dragging

Drag and Drop with interact.js

We can certainly drag and drop by combining listening to the base events (pointerup, pointermove, pointerdown). But here we go with a simpler approach. Since our event system is fully compatible with the DOM Event API, we can directly use a web-side off-the-shelf drag-and-drop library such as interact.js to do most of the of the "dirty work". Instead, we only need to do two things:

  1. Pass interact.js a fake context canvas.document and node 1 to make it think it's operating on the real DOM
  2. Changing the position of nodes and edge endpoints in the onmove callback function of interact.js
import interact from 'interactjs';
// Use interact.js
interact(node1, {
context: canvas.document, // Pass our fake DOM-like context
}).draggable({
onmove: function (event) {
// Get the offset from interact.js
const { dx, dy } = event;
// Change the position of node 1
node1.translateLocal(dx, dy);
// Get the position of node 1
const [nx, ny] = node1.getLocalPosition();
// Changing the endpoint position of an edge
edge.style.x1 = nx;
edge.style.y1 = ny;
},
});

You may have noticed that the mouse style automatically changes to a move shape when dragging and dropping, thanks to interact.js. This is possible because interact.js does not assume that it is necessarily running in the real DOM environment. In other words, we can "trick" G's graphics by disguising them as the DOM. By the same token, we can also use gesture libraries like hammer.js.

Change node position

Back in the onmove callback function, we need to change the position of the node, and the offset interact.js already tells us.

node1.translateLocal(dx, dy);

There are many other [transform operations](/en/api/basic/display-object#transform operations) like translateLocal, besides translation, you can also rotate and scale.

Changing the endpoint of an edge is also simple, and can be done by modifying its style attribute x1/y1, see Line for further information.

edge.style.x1 = nx;
edge.style.y1 = ny;

So this simple scene is complete, follow our subsequent tutorials to continue to understand the scene graph and camera.