npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@gohai/p5.comfyui-helper

v1.0.0

Published

A library for p5.js which adds support for interacting with ComfyUI

Downloads

71

Readme

p5.comfyui-helper.js

A library for p5.js which adds support for interacting with ComfyUI, using its API. It provides the following features:

  • Submit ComfyUI workflows (saved in API format) from p5.js
  • Modify various aspects of the workflow from within JavaScript
  • Submit images or p5 drawing surfaces as inputs to workflows (e.g. for img2img, ...)
  • Easy to use API that supports multiple outputs as well
  • Works with promises or callbacks
  • Fast (uses WebSocket instead of file transfers)

Reference

Prerequisites

  • Working ComfyUI installation (tested with v0.2.4)
  • ComfyUI Nodes for External Tooling (available to install via the ComfyUI Manager, or manually)
  • If you're planning on accessing ComfyUI remotely: --listen 0.0.0.0 --enable-cors-header
  • If your website will run on HTTPS (e.g. in the p5.js Web Editor): you'll need to provision a certificate, and load it into ComfyUI with --tls-keyfile privkey.pem --tls-certfile fullchain.pem
  • Enable Dev Mode in ComfyUI's setting (via the cog icon on the website), for the "Save (API format)" button to show.

Getting started

Download the library file and include it in the head section of your HTML:

<script src="https://unpkg.com/@gohai/p5.comfyui-helper@^1/libraries/p5.comfyui-helper.js"></script>

or

<script src="p5.comfyui-helper.js"></script>

Connecting to the ComfyUI instance

Create a global variable, and set up the ComfyUiP5Helper like so. The only argument is the URL you're using to access ComfyUI (with or without a slash at the end).

let comfy;

function setup() {
  comfy = new ComfyUiP5Helper("https://your.comfyui.instance:8188");

Loading a workflow

Save the desired workflow in ComfyUI by clicking the "Save (API Format)" button in the tool bar. (If you don't see this button, make sure that Dev Mode is enabled in the settings accessible via the cog icon.)

This creates a JSON file that can be easily added to your p5.js project (e.g. by uploading it in the p5.js Web Editor), and loaded like so:

let workflow;

function preload() {
  workflow = loadJSON("workflow_api.json");
  console.log("workflow is", workflow);
}

The keys in this object correspond to the # number ComfyUI shows at the top right of each node.

example node

E.g. to change the seed of this KSampler node from within JavaScript, we'd do:

workflow[3].inputs.seed = random(999999);

Running a workflow

Submitting a workflow to ComfyUI's queue is as easy as calling its run method.

You can use this in two ways: either by passing a callback function as the second parameter.

comfy.run(workflow, gotImage);

This will call a function gotImage once the result are available:

function gotImage(results, error) {
  // ...
}

Alternatively, you can also use the await keyword to wait for the run method to eventually return the results:

let results = await comfy.run(workflow);

Receiving results

The results contains an array of objects, each with a src property and a node property. Use src with loadImage() to turn this into an image-type variable to be used for drawing.

let img;

function gotImage(results, error) {
  console.log(results);

  if (results.length > 0) {
    img = loadImage(results[0].src);
  }
}

The node property contains the id of the node that created the image.

Image inputs

Various types of workflows, such as image-to-image or inpainting, make use of existing images as part of the image generation.

The image() method is replacing any "Load Image" node (which loads an image from drive) by an image (or drawing context) variable in p5.js.

E.g.: the following image-to-image workflow has a "Load Image" as #10

let srcImg;

function preload() {
  srcImg = loadImage("example.png");
}

// ...
workflow[10] = comfy.image(srcImg);
let srcImg;

function setup() {
  createCanvas(512, 512);
  srcImg = createGraphics(width, height);
  srcImg.background(0);
  srcImg.fill("yellow");
  srcImg.circle(width/2, height/2, 100);

  // ...
  workflow[10] = comfy.image(srcImg);
}

The mask() method can be similarly used wherever the workflow contains a "Load Image (as mask)" node.