Giter VIP home page Giter VIP logo

comfy-org / litegraph.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from comfyanonymous/litegraph.js

3.0 0.0 2.0 28.55 MB

A graph node engine and editor written in Javascript similar to PD or UDK Blueprints, comes with its own editor in HTML5 Canvas2D. The engine can run client side or server side using Node. It allows to export graphs as JSONs to be included in applications independently.

License: MIT License

Shell 0.03% JavaScript 97.09% Python 0.09% C# 1.96% CSS 0.55% HTML 0.29%

litegraph.js's Introduction

@ComfyOrg/litegraph

This is the litegraph version used in ComfyUI_frontend.

This repo is litegraph with the following modifications:

  • Accumulated comfyUI custom changes (2024-01 ~ 2024-05) (#1)
  • Type schema change for ComfyUI_frontend TS migration (#3)
  • Zoom fix (#7)
  • Emit search box triggering custom events (#10)
  • Truncate overflowing combo widget text (#17)
  • Sort node based on ID on graph serialization (#21)
  • Fix empty input not used when connecting links (#24)

Install

npm i @comfyorg/litegraph

litegraph.js

A library in Javascript to create graphs in the browser similar to Unreal Blueprints. Nodes can be programmed easily and it includes an editor to construct and tests the graphs.

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

Try it in the demo site.

Node Graph

Features

  • Renders on Canvas2D (zoom in/out and panning, easy to render complex interfaces, can be used inside a WebGLTexture)
  • Easy to use editor (searchbox, keyboard shortcuts, multiple selection, context menu, ...)
  • Optimized to support hundreds of nodes per graph (on editor but also on execution)
  • Customizable theme (colors, shapes, background)
  • Callbacks to personalize every action/drawing/event of nodes
  • Subgraphs (nodes that contain graphs themselves)
  • Live mode system (hides the graph but calls nodes to render whatever they want, useful to create UIs)
  • Graphs can be executed in NodeJS
  • Highly customizable nodes (color, shape, slots vertical or horizontal, widgets, custom rendering)
  • Easy to integrate in any JS application (one single file, no dependencies)
  • Typescript support

Nodes provided

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

  • Interface (Widgets)
  • Math (trigonometry, math operations)
  • Audio (AudioAPI and MIDI)
  • 3D Graphics (Postprocessing in WebGL)
  • Input (read Gamepad)

Installation

You can install it using npm

npm install litegraph.js

Or downloading the build/litegraph.js and css/litegraph.css version from this repository.

First project

<html>
<head>
	<link rel="stylesheet" type="text/css" href="litegraph.css">
	<script type="text/javascript" src="litegraph.js"></script>
</head>
<body style='width:100%; height:100%'>
<canvas id='mycanvas' width='1024' height='720' style='border: 1px solid'></canvas>
<script>
var graph = new LGraph();

var canvas = new LGraphCanvas("#mycanvas", graph);

var node_const = LiteGraph.createNode("basic/const");
node_const.pos = [200,200];
graph.add(node_const);
node_const.setValue(4.5);

var node_watch = LiteGraph.createNode("basic/watch");
node_watch.pos = [700,200];
graph.add(node_watch);

node_const.connect(0, node_watch, 0 );

graph.start()
</script>
</body>
</html>

How to code a new Node type

Here is an example of how to build a node that sums two inputs:

//node constructor class
function MyAddNode()
{
  this.addInput("A","number");
  this.addInput("B","number");
  this.addOutput("A+B","number");
  this.properties = { precision: 1 };
}

//name to show
MyAddNode.title = "Sum";

//function to call when the node is executed
MyAddNode.prototype.onExecute = function()
{
  var A = this.getInputData(0);
  if( A === undefined )
    A = 0;
  var B = this.getInputData(1);
  if( B === undefined )
    B = 0;
  this.setOutputData( 0, A + B );
}

//register in the system
LiteGraph.registerNodeType("basic/sum", MyAddNode );

or you can wrap an existing function:

function sum(a,b)
{
   return a+b;
}

LiteGraph.wrapFunctionAsNode("math/sum",sum, ["Number","Number"],"Number");

Server side

It also works server-side using NodeJS although some nodes do not work in server (audio, graphics, input, etc).

var LiteGraph = require("./litegraph.js").LiteGraph;

var graph = new LiteGraph.LGraph();

var node_time = LiteGraph.createNode("basic/time");
graph.add(node_time);

var node_console = LiteGraph.createNode("basic/console");
node_console.mode = LiteGraph.ALWAYS;
graph.add(node_console);

node_time.connect( 0, node_console, 1 );

graph.start()

Projects using it

screenshot

WebGLStudio

MOI Elephant

Mynodes

MyNodes

Utils


It includes several commands in the utils folder to generate doc, check errors and build minifyed version.

Demo


The demo includes some examples of graphs. In order to try them you can visit demo site or install it on your local computer, to do so you need git, node and npm. Given those dependencies are installed, run the following commands to try it out:

$ git clone https://github.com/jagenjo/litegraph.js.git
$ cd litegraph.js
$ npm install
$ node utils/server.js
Example app listening on port 80!

Open your browser and point it to http://localhost:8000/. You can select a demo from the dropdown at the top of the page.

Feedback


You can write any feedback to [email protected]

Contributors

  • atlasan
  • kriffe
  • rappestad
  • InventivetalentDev
  • NateScarlet
  • coderofsalvation
  • ilyabesk
  • gausszhou

litegraph.js's People

Contributors

jagenjo avatar inventivetalentdev avatar kriffe avatar atlasan avatar huchenlei avatar natescarlet avatar comfyanonymous avatar robthefivenine avatar altarfinch avatar baldurg avatar moritz89 avatar ilyabesk avatar m1kep avatar pythongosssss avatar ralphwetzel avatar ltdrdata avatar space-nuko avatar mercurio avatar tianlang0704 avatar mohsen-karami avatar j2l avatar dy-sh avatar vkerl avatar omar92 avatar gausszhou avatar kidoncio avatar mateuswetah avatar gotthardtz avatar marcmeszaros avatar guillaume-fgt avatar

Stargazers

fifteen42 avatar  avatar CH Sun avatar

litegraph.js's Issues

Add a zoom speed setting

Transferred from comfyanonymous/ComfyUI#3924

Feature Idea

My finger gets tired with this slow mouse wheel zoom speed, and yet I've also seen others who've had trouble with the zoom speed being too fast for them in the past, and so what this means is it really needs to be a configurable setting for each individual.

Existing Solutions

Luckily I figured out how to do it myself so this can serve as a guide for anyone who comes across it:

  • Open the file in a text editor: ComfyUI\web\lib\litegraph.core.js
  • Search for scale *= 1.1;
  • Replace the 1.1 with a larger number like 1.5 for a faster zooming in speed, or use a smaller number like 1.05 for a slower zooming in speed.
  • Two lines below that is the zooming out speed, which you'll also want to change: scale *= 1 / 1.1;, you can just replace this 1.1 with the same number as before.

But hopefully this can get added an official setting for better ease of use.

Other

No response

Rewrite the whole lib with TypeScript & ES6 Class instead of Function prototype for easier to maintain and expand

Description

This issue proposes a significant refactor of the library, transitioning from the current function prototype-based approach to a modern TypeScript and ES6 class-based architecture. The primary motivation behind this change is to improve the library's maintainability, scalability, and overall developer experience.

Current Problems

Inherent Limitations of Prototypal Inheritance: The current function prototype inheritance model can become complex and cumbersome, especially as the codebase grows and new features are added. It often leads to deeply nested object structures and a less intuitive understanding of the code.
Lack of Static Typing (JavaScript): Without static typing, we rely on runtime checks to catch errors, which can lead to unexpected issues in production. TypeScript's static type system would help prevent these problems at compile time.
Readability and Maintainability Concerns: The prototype-based code can be less readable and harder to reason about than the structured class approach, potentially hindering future development and maintenance efforts.

Proposed Solution

  • TypeScript Adoption: We will migrate the entire codebase to TypeScript. This will introduce static typing, enabling us to catch errors early, improve code quality, and enhance developer productivity through code autocompletion and better tooling.

  • ES6 Class Conversion: We will refactor all existing functions and prototype-based objects into ES6 classes. This change will provide a more organized and structured approach to code organization, making the codebase easier to understand, maintain, and extend.

  • Modular Code Structure: Currently, a lot of code is written based on ES5. It's worth writing with ES6 syntax to aid understanding and maintenance.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.