This document summarizes our research on LiteGraph.js, focusing on creating custom nodes and interactive controls (widgets). LiteGraph.js is a JavaScript library for creating node-based graphs similar to Unreal Blueprints or PureData, with a canvas-based editor and an execution engine.
LiteGraph.js provides a flexible system for creating custom nodes:
- Nodes are created as JavaScript constructor functions
- Node behavior is defined through prototype methods
- Nodes are registered to make them available in the editor
- Nodes can have inputs, outputs, properties, and interactive widgets
The system follows a pattern similar to:
function MyCustomNode() {
// Define inputs/outputs
this.addInput("Input", "number");
this.addOutput("Output", "number");
// Define properties
this.properties = { value: 10 };
// Add widgets
this.addWidget("number", "Value", this.properties.value, callback);
}
// Register node
LiteGraph.registerNodeType("category/my_node", MyCustomNode);LiteGraph.js includes several built-in widget types:
- number: Numeric input field
- slider: Slider control for numeric values
- combo: Dropdown selection
- text: Text input field
- toggle: Boolean on/off switch
- button: Clickable button
These widgets provide basic interactivity and can be linked to node properties.
While LiteGraph.js provides a widget system, it has some limitations:
- Limited to the predefined widget types
- No direct way to create completely custom widget types
- The
addCustomWidgetfunction exists but has prototype inheritance issues - Complex UI elements require bypassing the widget system
For truly custom controls, we found that the best approach is to:
- Use custom drawing via
onDrawForegroundoronDrawBackground - Handle mouse interactions with
onMouseDown,onMouseMove, etc. - Track interaction areas manually
- Manage state through node properties
This approach allows for creating any type of custom control, from buttons to complex interactive visualizations.
LiteGraph.js includes an event system for triggering actions between nodes:
- Use
LiteGraph.ACTIONtype for input slots that receive events - Use
LiteGraph.EVENTtype for output slots that trigger events - Implement
onActionto handle incoming events - Use
triggerSlotto send events to connected nodes
This system enables creating workflow-based applications where nodes can trigger each other without continuous data flow.
The library provides powerful custom drawing capabilities:
onDrawBackground: For drawing content only visible in edit modeonDrawForeground: For drawing content visible in both edit and live modes- Access to the standard Canvas2D API for rendering
- Local coordinate system with (0,0) at the top-left of the node content area
These capabilities allow for creating rich visualizations, custom controls, and data previews within nodes.
Nodes can handle mouse interactions through callback methods:
onMouseDown: Called when mouse button is pressed on the nodeonMouseMove: Called when mouse moves over the nodeonMouseUp: Called when mouse button is releasedonMouseEnter/onMouseLeave: Called when mouse enters/leaves node areaonDblClick: Called on double-click
These callbacks receive the event, local position, and graph canvas instance, enabling complex interactive elements.
Node state is managed through:
properties: Object containing serializable propertiesserialize_widgets: Boolean flag to include widget values in serializationonSerialize/onConfigure: Optional methods for custom serialization logic
By default, widget values aren't serialized unless the widget is linked to a property or serialize_widgets is set to true.
Based on our research, we recommend the following best practices for creating custom nodes with interactive controls:
-
Use built-in widgets when possible - They're optimized and integrate well with the system
-
Link widgets to properties - This simplifies state management and serialization
-
For custom controls:
- Define clear interaction areas
- Implement all relevant mouse handlers
- Provide visual feedback for interactions
- Constrain interaction to node boundaries
-
For complex visualizations:
- Use efficient data structures
- Optimize drawing routines
- Consider throttling or buffering frequent updates
- Use clipping to prevent drawing outside node bounds
-
Node design:
- Keep nodes focused on a single purpose
- Provide clear visual feedback about node state
- Use consistent visual language across custom nodes
- Set appropriate node size to accommodate controls
-
Limited Widget Customization: The built-in widget system doesn't provide a direct way to create fully custom widget types
-
Documentation Gaps: While the basic documentation is good, some advanced features aren't well documented
-
Custom Widget Inheritance: The
addCustomWidgetfunction has issues with prototype methods being lost -
Complex UI Elements: Creating complex UI elements requires managing interaction and drawing manually
-
Mobile Support: Touch interaction handling requires additional consideration
As part of our research, we created several example custom nodes demonstrating different widget and interaction techniques:
- CalculatorNode: Basic widget usage with combo and toggle widgets
- ColorMixerNode: Slider widgets with custom color preview
- CustomButtonsNode: Custom buttons with hover states
- GraphWidgetNode: Complex data visualization widget
- Knob2DNode: Custom XY control with drag interaction
- EventControlNode: Event triggering with visual feedback
These examples cover most common use cases for custom widgets and can serve as starting points for more specific implementations.
- comfyUI
- webglstudio.org
- MOI Elephant
- Mynodes
LiteGraph.js provides a solid foundation for creating node-based graph editors with custom nodes and interactive controls. While the built-in widget system has limitations for highly custom interfaces, the combination of custom drawing and mouse interaction callbacks enables creating virtually any type of control.
The library is well-suited for workflow editors, visual programming tools, and data processing applications. With the right approach to creating custom nodes and widgets, it's possible to build sophisticated and user-friendly graph-based applications.