Using the library

Using Typescript

If you are using typescript, then you are in luck! The library is built in typescript, and includes advanced types for everything you need right out of the box.
Lets start by including the things we are going to need:
1
import createEngine, {
2
DefaultLinkModel,
3
DefaultNodeModel,
4
DiagramModel
5
} from '@projectstorm/react-diagrams';
6
​
7
import {
8
CanvasWidget
9
} from '@projectstorm/react-canvas-core';
Copied!
Now we call createEngine which will bootstrap a DiagramEngine for us that contains all the defaults setup.
1
// create an instance of the engine with all the defaults
2
const engine = createEngine();
Copied!
Next, we create two nodes:
1
// node 1
2
const node1 = new DefaultNodeModel({
3
name: 'Node 1',
4
color: 'rgb(0,192,255)',
5
});
6
node1.setPosition(100, 100);
7
let port1 = node1.addOutPort('Out');
8
​
9
// node 2
10
const node2 = new DefaultNodeModel({
11
name: 'Node 1',
12
color: 'rgb(0,192,255)',
13
});
14
node2.setPosition(100, 100);
15
let port2 = node2.addOutPort('Out');
Copied!
Now we link the two ports of both of the nodes:
1
// link them and add a label to the link
2
const link = port1.link<DefaultLinkModel>(port2);
3
link.addLabel('Hello World!');
Copied!
Great! Now we have setup a simple diagram. All thats left to do, is create a DiagramModel to contain everything, add all the elements to it, and then add it to the engine.
1
const model = new DiagramModel();
2
model.addAll(node1, node2, link);
3
engine.setModel(model);
Copied!
And then we render with React!
1
<CanvasWidget engine={engine} />
Copied!
Last modified 2yr ago
Copy link