Node Interaction in EJ2 TypeScript Diagram control
31 Jan 202524 minutes to read
The diagram provides support for selecting, dragging, resizing, and rotating nodes interactively. A node can be selected by simply clicking on it, dragged by dragging it on diagram canvas, resized using the resize handle, and rotated using the rotate handle. Additionally, interactions can be performed using some public methods, which are explained below:
Select
You can simply click on the node to select it and click on diagram canvas to unselect it like below.
To select node programatically
A node can be selected at runtime by using the select
method and the selection can be cleared in the diagram by using the clearSelection
or unSelect
method. The following code explains how to select and clear selection in the diagram.
import { Diagram, NodeModel, SymbolPalette,IDraggingEventArgs } from '@syncfusion/ej2-diagrams';
let diagram: Diagram;
let nodes: NodeModel[] = [
{
// Position of the node
offsetX: 250,
offsetY: 250,
// Size of the node
width: 100,
height: 100,
visible: true,
style: { fill: '#6AA8D7', strokeWidth: 1 },
// Text(label) added to the node
},
];
//Initializes diagram control
diagram = new Diagram({
width: '70%',
height: '645px',
nodes: nodes,
getNodeDefaults: function (obj: NodeModel) {
obj.style = { fill: 'skyblue' };
obj.width = 75;
obj.height = 50;
return obj;
},
created: created,
positionChange: function (args:IDraggingEventArgs) {
if(args.state === 'Completed'){
console.log('Position Change');
}
},
});
diagram.appendTo('#element');
function created(): void {
diagram.fitToPage({ mode: 'Width' });
}
(document.getElementById('select') as HTMLInputElement).onclick = () => {
diagram.select([diagram.nodes[0]]);
};
(document.getElementById('unSelect') as HTMLInputElement).onclick = () => {
diagram.clearSelection();
};
<!DOCTYPE html>
<html lang="en">
<head>
<title>EJ2 Diagram</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-diagrams/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-navigations/styles/fabric.css" rel="stylesheet" />
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<input type="button" value="select" id="select"/>
<input type="button" value="unSelect" id="unSelect"/>
<div id="element"></div>
</div>
</body>
</html>
Method | Parameter | Description |
---|---|---|
unSelect |
NodeModel/ConnectorModel | The object to remove from the selection. |
clearSelection |
- | Clears all the selection in the diagram. |
Drag
You can simply mousedown on a node and drag it anywhere on the diagram canvas like below.
To drag node programatically
A node can be dragged at runtime by using the drag
method. The following code explains how to drag the node by using the drag method.
import { Diagram, NodeModel, SymbolPalette,IDraggingEventArgs } from '@syncfusion/ej2-diagrams';
let diagram: Diagram;
let nodes: NodeModel[] = [
{
// Position of the node
offsetX: 250,
offsetY: 250,
// Size of the node
width: 100,
height: 100,
visible: true,
style: { fill: '#6AA8D7', strokeWidth: 1 },
// Text(label) added to the node
},
];
//Initializes diagram control
diagram = new Diagram({
width: '70%',
height: '645px',
nodes: nodes,
getNodeDefaults: function (obj: NodeModel) {
obj.style = { fill: 'skyblue' };
obj.width = 75;
obj.height = 50;
return obj;
},
created: created,
positionChange: function (args:IDraggingEventArgs) {
if(args.state === 'Completed'){
console.log('Position Change');
}
},
});
diagram.appendTo('#element');
function created(): void {
diagram.fitToPage({ mode: 'Width' });
}
(document.getElementById('drag') as HTMLInputElement).onclick = () => {
// First argument as node object. Second and third argument inidcates the position to be moved from the current position in x and y direction.
diagram.drag(diagram.nodes[0], 20, 20);
};
<!DOCTYPE html>
<html lang="en">
<head>
<title>EJ2 Diagram</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-diagrams/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-navigations/styles/fabric.css" rel="stylesheet" />
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<input type="button" value="drag" id="drag"/>
<div id="element"></div>
</div>
</body>
</html>
Resize
When we select a node a resize handle will appear on all the sides of the node. We can resize the node by clicking and dragging the resize handle.
To resize node programatically
A node can be resized at runtime by using the scale
method. The following code explains how to resize the node by using the scale method.
import { Diagram, NodeModel, SymbolPalette,IDraggingEventArgs } from '@syncfusion/ej2-diagrams';
let diagram: Diagram;
let nodes: NodeModel[] = [
{
// Position of the node
offsetX: 250,
offsetY: 250,
// Size of the node
width: 100,
height: 100,
visible: true,
style: { fill: '#6AA8D7', strokeWidth: 1 },
// Text(label) added to the node
},
];
//Initializes diagram control
diagram = new Diagram({
width: '70%',
height: '645px',
nodes: nodes,
getNodeDefaults: function (obj: NodeModel) {
obj.style = { fill: 'skyblue' };
obj.width = 75;
obj.height = 50;
return obj;
},
created: created,
positionChange: function (args:IDraggingEventArgs) {
if(args.state === 'Completed'){
console.log('Position Change');
}
},
});
diagram.appendTo('#element');
function created(): void {
diagram.fitToPage({ mode: 'Width' });
}
(document.getElementById('resize') as HTMLInputElement).onclick = () => {
diagram.scale(diagram.nodes[0], 0.5, 0.5, { x: 0.5, y: 0.5 })
};
<!DOCTYPE html>
<html lang="en">
<head>
<title>EJ2 Diagram</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-diagrams/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-navigations/styles/fabric.css" rel="stylesheet" />
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<input type="button" value="resize" id="resize"/>
<div id="element"></div>
</div>
</body>
</html>
Rotate
A node can be rotated interactively by clicking and dragging the rotate handle of the node.
To rotate node programatically
A node can be rotated at runtime by using the rotate
method. The following code explains how to rotate the node by using the rotate method.
import { Diagram, NodeModel, SymbolPalette,IDraggingEventArgs } from '@syncfusion/ej2-diagrams';
let diagram: Diagram;
let nodes: NodeModel[] = [
{
// Position of the node
offsetX: 250,
offsetY: 250,
// Size of the node
width: 100,
height: 100,
visible: true,
style: { fill: '#6AA8D7', strokeWidth: 1 },
// Text(label) added to the node
},
];
//Initializes diagram control
diagram = new Diagram({
width: '70%',
height: '645px',
nodes: nodes,
getNodeDefaults: function (obj: NodeModel) {
obj.style = { fill: 'skyblue' };
obj.width = 75;
obj.height = 50;
return obj;
},
created: created,
positionChange: function (args:IDraggingEventArgs) {
if(args.state === 'Completed'){
console.log('Position Change');
}
},
});
diagram.appendTo('#element');
function created(): void {
diagram.fitToPage({ mode: 'Width' });
}
(document.getElementById('rotate') as HTMLInputElement).onclick = () => {
diagram.rotate(diagram.nodes[0],diagram.nodes[0].rotateAngle + 15);
};
<!DOCTYPE html>
<html lang="en">
<head>
<title>EJ2 Diagram</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-diagrams/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-navigations/styles/fabric.css" rel="stylesheet" />
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<input type="button" value="rotate" id="rotate"/>
<div id="element"></div>
</div>
</body>
</html>
Flip
The diagram Provides support to flip the node. flip
is performed to give the mirrored image of the original element.
The flip types are as follows:
Flip direction | Description |
---|---|
HorizontalFlip |
Horizontal is used to flip the node to be mirrored across the horizontal axis. |
VerticalFlip |
Vertical is used to flip the node to be mirrored across the vertical axis. |
Both |
Both is used to flip the node to be mirrored across the horizontal and vertical axes. |
None | It is used to disable all the flip behavior. |
The following code illustrates how to provide the mirror image of the original element.
import { Diagram, NodeModel ,BasicShapeModel, FlipDirection} from '@syncfusion/ej2-diagrams';
// A node is created and stored in nodes array.
let node: NodeModel = {
// Position of the node
offsetX: 250,
offsetY: 250,
// Size of the node
width: 100,
height: 100,
// Flip the node in Horizontal Direction
flip: FlipDirection.Horizontal,
shape: {
type: 'Basic',
shape: 'RightTriangle',
},
style: {
fill: '#6BA5D7',
strokeDashArray: '5,5'
},
};
// initialize Diagram component
let diagram: Diagram = new Diagram({
width: '100%',
height: '600px',
// Add node
nodes: [node]
});
// render initialized Diagram
diagram.appendTo('#element');
<!DOCTYPE html>
<html lang="en">
<head>
<title>EJ2 Diagram</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-diagrams/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-navigations/styles/fabric.css" rel="stylesheet" />
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
</body>
</html>
Update flip at runtime
You can dynamically update the flip for a node at runtime using the ^
operator. This operator allows you to apply the same flip direction multiple times, toggling the node’s orientation effectively.
The following example demonstrates how to update the flip for a node dynamically:
import { Diagram, NodeModel ,BasicShapeModel, FlipDirection} from '@syncfusion/ej2-diagrams';
// A node is created and stored in nodes array.
let node: NodeModel = {
// Position of the node
offsetX: 250,
offsetY: 250,
// Size of the node
width: 100,
height: 100,
// Flip the node in Horizontal Direction
flip: FlipDirection.Horizontal,
shape: {
type: 'Basic',
shape: 'RightTriangle',
},
style: {
fill: '#6BA5D7',
strokeDashArray: '5,5'
},
};
// initialize Diagram component
let diagram: Diagram = new Diagram({
width: '100%',
height: '600px',
// Add node
nodes: [node]
});
// render initialized Diagram
diagram.appendTo('#element');
(document.getElementById('flipHorizontal') as HTMLElement).onclick = () => {
// Flips the node horizontally.
diagram.nodes[0].flip ^= FlipDirection.Horizontal;
diagram.dataBind();
}
(document.getElementById('flipVertical') as HTMLElement).onclick = () => {
// Flips the node vertically.
diagram.nodes[0].flip ^= FlipDirection.Vertical;
diagram.dataBind();
}
(document.getElementById('flipBoth') as HTMLElement).onclick = () => {
// Flips the node horizontally and vertically.
diagram.nodes[0].flip ^= FlipDirection.Both;
diagram.dataBind();
}
(document.getElementById('flipNone') as HTMLElement).onclick = () => {
// No flip applied to the node.
diagram.nodes[0].flip = FlipDirection.None;
diagram.dataBind();
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>EJ2 Diagram</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-diagrams/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-navigations/styles/fabric.css" rel="stylesheet" />
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<input type="button" value="flipHorizontal" id="flipHorizontal"/>
<input type="button" value="flipVertical" id="flipVertical"/>
<input type="button" value="flipBoth" id="flipBoth"/>
<input type="button" value="flipNone" id="flipNone"/>
<div id='element'></div>
</div>
</body>
</html>
Flip modes
The flipMode
is used to control the behavior of the flip object whether to flip the object along with the port and label.
FlipMode | Description |
---|---|
Label | It flips the label along with the object while keeping the text readable. |
Port | It flips the port along with the object. |
All | It flips the port, label, and label text along with the object. |
None | It flips only the object. |
LabelText | It flips the object and inverts the label without changing its position. |
PortAndLabel | It flips the port and label along with the object while keeping the text readable. |
PortAndLabelText | It flips the port and label text along with the object. |
LabelAndLabelText | It flips the label and label text along with the Object. |
Below are examples of a node undergoing various FlipModes in different flip directions.
Flip Direction | Flip Mode | Default Node | Flipped Node |
---|---|---|---|
Horizontal | All | ![]() |
![]() |
Horizontal | Label | ![]() |
![]() |
Horizontal | LabelText | ![]() |
![]() |
Horizontal | Port | ![]() |
![]() |
Horizontal | None | ![]() |
![]() |
Horizontal | PortAndLabel | ![]() |
![]() |
Horizontal | PortAndLabelText | ![]() |
![]() |
Horizontal | LabelAndLabelText | ![]() |
![]() |
Vertical | All | ![]() |
![]() |
Vertical | Label | ![]() |
![]() |
Vertical | LabelText | ![]() |
![]() |
Vertical | Port | ![]() |
![]() |
Vertical | None | ![]() |
![]() |
Vertical | PortAndLabel | ![]() |
![]() |
Vertical | PortAndLabelText | ![]() |
![]() |
Vertical | LabelAndLabelText | ![]() |
![]() |
Both | All | ![]() |
![]() |
Both | Label | ![]() |
![]() |
Both | LabelText | ![]() |
![]() |
Both | Port | ![]() |
![]() |
Both | None | ![]() |
![]() |
Both | PortAndLabel | ![]() |
![]() |
Both | PortAndLabelText | ![]() |
![]() |
Both | LabelAndLabelText | ![]() |
![]() |
import {
Diagram,
FlipDirection,
NodeModel,
PortVisibility,
} from '@syncfusion/ej2-diagrams';
// A node is created and stored in nodes array.
let nodes: NodeModel[] = [
{
id: 'node1',
// Position of the node
offsetX: 100,
offsetY: 100,
// Size of the node
width: 100,
height: 100,
ports: [
{
id: 'left',
offset: { x: 0, y: 0.5 },
visibility: PortVisibility.Visible,
},
],
annotations: [{ content: 'FlipMode as Label', offset: { x: 0, y: 0.8 } }],
// Flip the node in Horizontal Direction
flip: FlipDirection.Horizontal,
//FlipMode as Label
flipMode: 'Label',
shape: {
type: 'Basic',
shape: 'RightTriangle',
},
style: {
fill: '#6BA5D7',
},
},
{
id: 'node2',
// Position of the node
offsetX: 400,
offsetY: 100,
// Size of the node
width: 100,
height: 100,
annotations: [{ content: 'FlipMode as Port', offset: { x: 0, y: 0.8 } }],
// Flip the node in Horizontal Direction
flip: FlipDirection.Horizontal,
ports: [
{
id: 'left',
offset: { x: 0, y: 0.5 },
visibility: PortVisibility.Visible,
},
],
//FlipMode as Port
flipMode: 'Port',
shape: {
type: 'Basic',
shape: 'RightTriangle',
},
style: {
fill: '#6BA5D7',
},
},
{
id: 'node3',
// Position of the node
offsetX: 100,
offsetY: 300,
// Size of the node
width: 100,
height: 100,
annotations: [{ content: 'FlipMode as All', offset: { x: 0, y: 0.8 } }],
// Flip the node in Horizontal Direction
flip: FlipDirection.Horizontal,
ports: [
{
id: 'left',
offset: { x: 0, y: 0.5 },
visibility: PortVisibility.Visible,
},
],
//FlipMode as All
flipMode: 'All',
shape: {
type: 'Basic',
shape: 'RightTriangle',
},
style: {
fill: '#6BA5D7',
},
},
{
id: 'node4',
// Position of the node
offsetX: 400,
offsetY: 300,
// Size of the node
width: 100,
height: 100,
annotations: [{ content: 'FlipMode as None', offset: { x: 0, y: 0.8 } }],
// Flip the node in Horizontal Direction
flip: FlipDirection.Horizontal,
ports: [
{
id: 'left',
offset: { x: 0, y: 0.5 },
visibility: PortVisibility.Visible,
},
],
//FlipMode as None
flipMode: 'None',
shape: {
type: 'Basic',
shape: 'RightTriangle',
},
style: {
fill: '#6BA5D7',
},
},
];
// initialize Diagram component
let diagram: Diagram = new Diagram({
width: '100%',
height: '600px',
// Add node
nodes: nodes,
});
// render initialized Diagram
diagram.appendTo('#element');
<!DOCTYPE html>
<html lang="en">
<head>
<title>EJ2 Diagram</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-diagrams/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-navigations/styles/fabric.css" rel="stylesheet" />
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
</body>
</html>