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:
1import createEngine, {
2 DefaultLinkModel,
3 DefaultNodeModel,
4 DiagramModel
5} from '@projectstorm/react-diagrams';
6
7import {
8 CanvasWidget
9} from '@projectstorm/react-canvas-core';
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
2const engine = createEngine();
Next, we create two nodes:
1// node 1
2const node1 = new DefaultNodeModel({
3 name: 'Node 1',
4 color: 'rgb(0,192,255)',
5});
6node1.setPosition(100, 100);
7let port1 = node1.addOutPort('Out');
8
9// node 2
10const node2 = new DefaultNodeModel({
11 name: 'Node 1',
12 color: 'rgb(0,192,255)',
13});
14node2.setPosition(100, 100);
15let port2 = node2.addOutPort('Out');
Now we link the two ports of both of the nodes:
1// link them and add a label to the link
2const link = port1.link<DefaultLinkModel>(port2);
3link.addLabel('Hello World!');
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.
1const model = new DiagramModel();
2model.addAll(node1, node2, link);
3engine.setModel(model);
And then we render with React!
1<CanvasWidget engine={engine} />