Skip to content

Commit 469e3d9

Browse files
author
tamat
committed
Merge branch 'master' of https://github.com/jagenjo/litegraph.js
2 parents 918b859 + 3c38926 commit 469e3d9

File tree

3 files changed

+55
-27
lines changed

3 files changed

+55
-27
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ A library in Javascript to create graphs in the browser similar to Unreal Bluepr
44

55
It can be integrated easily in any existing web applications and graphs can be run without the need of the editor.
66

7-
Try it in the [demo site](http://tamats.com/projects/litegraph/demo).
7+
Try it in the [demo site](https://tamats.com/projects/litegraph/demo).
88

99
![Node Graph](imgs/node_graph_example.png "WebGLStudio")
1010

@@ -18,7 +18,7 @@ Try it in the [demo site](http://tamats.com/projects/litegraph/demo).
1818
- Live mode system (hides the graph but calls nodes to render whatever they want, useful to create UIs)
1919
- Graphs can be executed in NodeJS
2020
- Highly customizable nodes (color, shape, slots vertical or horizontal, widgets, custom rendering)
21-
- Easy to integrate in any JS application
21+
- Easy to integrate in any JS application (one single file, no dependencies)
2222

2323
## Nodes provided
2424
Although it is easy to create new node types, LiteGraph comes with some default nodes that could be useful for many cases:

guides/README.md

+50-22
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,50 @@
11
# LiteGraph
22

3-
Here is a list of useful info when working with LiteGraph
3+
Here is a list of useful info when working with LiteGraph.
4+
The library is divided in four levels:
5+
* **LGraphNode**: the base class of a node (this library uses is own system of inheritance)
6+
* **LGraph**: the container of a whole graph made of nodes
7+
* **LGraphCanvas**: the class in charge of rendering/interaction with the nodes inside the browser.
8+
9+
And in ```the src/``` folder there is also another class included:
10+
* **LiteGraph.Editor**: A wrapper around LGraphCanvas that adds buttons around it.
411

512
## LGraphNode
613

714
LGraphNode is the base class used for all the nodes classes.
815
To extend the other classes all the methods contained in LGraphNode.prototype are copyed to the classes when registered.
916

10-
When you create a new node type you do not have to inherit from that class, when the node is registered all the methods are copied to your node prototype.
17+
When you create a new node type you do not have to inherit from that class, when the node is registered all the methods are copied to your node prototype. This is done inside the functions ```LiteGraph.registerNodeType(...)```.
1118

1219
Here is an example of how to create your own node:
1320

1421
```javascript
15-
//node constructor class
22+
//your node constructor class
1623
function MyAddNode()
1724
{
25+
//add some input slots
1826
this.addInput("A","number");
1927
this.addInput("B","number");
28+
//add some output slots
2029
this.addOutput("A+B","number");
30+
//add some properties
2131
this.properties = { precision: 1 };
2232
}
2333

24-
//name to show
34+
//name to show on the canvas
2535
MyAddNode.title = "Sum";
2636

2737
//function to call when the node is executed
2838
MyAddNode.prototype.onExecute = function()
2939
{
40+
//retrieve data from inputs
3041
var A = this.getInputData(0);
3142
if( A === undefined )
3243
A = 0;
3344
var B = this.getInputData(1);
3445
if( B === undefined )
3546
B = 0;
47+
//assing data to otputs
3648
this.setOutputData( 0, A + B );
3749
}
3850

@@ -44,30 +56,31 @@ LiteGraph.registerNodeType("basic/sum", MyAddNode );
4456

4557
## Node settings
4658

47-
There are several settings that could be defined per node:
48-
* **size**: ```[width,height]```
49-
* **properties**: object containing the properties that could be configured by the user
59+
There are several settings that could be defined or modified per node:
60+
* **size**: ```[width,height]``` the size of the area inside the node (excluding title). Every row is LiteGraph.NODE_SLOT_HEIGHT pixels height.
61+
* **properties**: object containing the properties that could be configured by the user, and serialized when saving the graph
5062
* **shape**: the shape of the object (could be LiteGraph.BOX,LiteGraph.ROUND,LiteGraph.CARD)
5163
* **flags**: several flags
5264
* **resizable**: if it can be resized dragging the corner
5365
* **horizontal**: if the slots should be placed horizontally on the top and bottom of the node
5466
* **clip_area**: clips the content when rendering the node
55-
56-
There are several callbacks that could be defined:
57-
* **onAdded**: when added to graph
58-
* **onRemoved**: when removed from graph
59-
* **onStart**: when the graph starts playing
60-
* **onStop**: when the graph stops playing
61-
* **onDrawBackground**: render something inside the node (not visible in Live mode)
62-
* **onDrawForeground**: render something inside the node
63-
* **onMouseDown,onMouseMove,onMouseUp,onMouseEnter,onMouseLeave**
67+
* **collapsed**: if it is shown collapsed (small)
68+
69+
There are several callbacks that could be defined by the user:
70+
* **onAdded**: called when added to graph
71+
* **onRemoved**: called when removed from graph
72+
* **onStart**: called when the graph starts playing
73+
* **onStop**: called when the graph stops playing
74+
* **onDrawBackground**: render custom node content on canvas (not visible in Live mode)
75+
* **onDrawForeground**: render custom node content on canvas (on top of slots)
76+
* **onMouseDown,onMouseMove,onMouseUp,onMouseEnter,onMouseLeave** to catch mouse events
6477
* **onDblClick**: double clicked in the editor
65-
* **onExecute**: execute the node
78+
* **onExecute**: called when it is time to execute the node
6679
* **onPropertyChanged**: when a property is changed in the panel (return true to skip default behaviour)
67-
* **onGetInputs**: returns an array of possible inputs
80+
* **onGetInputs**: returns an array of possible inputs in the form of [ ["name","type"], [...], [...] ]
6881
* **onGetOutputs**: returns an array of possible outputs
69-
* **onSerialize**: before serializing
70-
* **onSelected**: selected in the editor
82+
* **onSerialize**: before serializing, receives an object where to store data
83+
* **onSelected**: selected in the editor, receives an object where to read data
7184
* **onDeselected**: deselected from the editor
7285
* **onDropItem**: DOM item dropped over the node
7386
* **onDropFile**: file dropped over the node
@@ -119,10 +132,12 @@ MyNodeClass.shape = LiteGraph.ROUND_SHAPE;
119132

120133
You can draw something inside a node using the callbacks ```onDrawForeground``` and ```onDrawBackground```. The only difference is that onDrawForeground gets called in Live Mode and onDrawBackground not.
121134

122-
You do not have to worry about the coordinates system, [0,0] is the top-left corner of the node content area (not the title).
135+
Both functions receive the [Canvas2D rendering context](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D) and the LGraphCanvas instance where the node is being rendered.
136+
137+
You do not have to worry about the coordinates system, (0,0) is the top-left corner of the node content area (not the title).
123138

124139
```js
125-
node.onDrawForeground = function(canvas, ctx)
140+
node.onDrawForeground = function(ctx, graphcanvas)
126141
{
127142
if(this.flags.collapsed)
128143
return;
@@ -202,6 +217,19 @@ function MyNode()
202217
}
203218
```
204219

220+
# Execution Flow
221+
To execute a graph you must call ```graph.runStep()```.
222+
223+
This function will call the method ```node.onExecute()``` for every node in the graph.
224+
225+
The order of execution is determined by the system according to the morphology of the graph (nodes without inputs are considered level 0, then nodes connected to nodes of level 0 are level 1, and so on). This order is computed only when the graph morphology changes (new nodes are created, connections change).
226+
227+
It is up to the developer to decide how to handle inputs and outputs from inside the node.
228+
229+
The data send through outputs using ```this.setOutputData(0,data)``` is stored in the link, so if the node connected through that link does ```this.getInputData(0)``` it will receive the same data sent.
230+
231+
For rendering, the nodes are executed according to their order in the ```graph._nodes``` array, which changes when the user interact with the GraphCanvas (clicked nodes are moved to the back of the array so they are rendered the last).
232+
205233

206234
## Integration
207235

src/litegraph.d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,11 @@ export const LiteGraph: {
209209
/** if set to true some nodes like Formula would be allowed to evaluate code that comes from unsafe sources (like node configuration), which could lead to exploits */
210210
allow_scripts: boolean;
211211
/** node types by string */
212-
registered_node_types: Record<string, LGraphNode>;
212+
registered_node_types: Record<string, LGraphNodeConstructor>;
213213
/** used for dropping files in the canvas */
214-
node_types_by_file_extension: Record<string, LGraphNode>;
214+
node_types_by_file_extension: Record<string, LGraphNodeConstructor>;
215215
/** node types by class name */
216-
Nodes: Record<string, LGraphNode>;
216+
Nodes: Record<string, LGraphNodeConstructor>;
217217

218218
/** used to add extra features to the search box */
219219
searchbox_extras: Record<

0 commit comments

Comments
 (0)