Using p5.js in a node.js server

I wish to use p5.js in conjunction with node.js to create a Twitter-bot which generates noise-based images and posts them at random times to Twitter. The difficulty is getting p5.js into the node.js universe. I have tried a few of the methods for making modules and such, but no luck there: I typically get a 'ReferenceError: window is not defined error'. So, briefly, has anyone been able to use p5.js in the server? If so, how? If impossible as present, any suggestions/work-arounds to call a script from node.js, the script is just creating a .jpg and putting it on the server, then node takes the .jpg and posts it.

Thanks!

Tagged:

Answers

  • edited May 2016

    'ReferenceError: window is not defined error'

    Do the test yourself. Open your terminal, type in node + ENTER, then type in window + ENTER.
    Conclusion: There's no variable called window in node.js. [-X

  • Answer ✓

    p5js is a client-side library and clearly hasn't been written with the intention of running in a node environment - which as GoToLoop points out - doesn't have a window object. p5js is wholly dependent on there being a window object (you can see that from the bootstrapping code and the ~30+ references in core)...

    It would take an insane amount of hacking to get it running under node :(|)

    If you're working in node then you must know about NPM. A quick search for 'image library' throws up jimp which has a image.setPixelColor() function; so ought to be able to deliver what you need...

  • Fair enough, thank you both. I will search for another library that does what I need.

  • I'm might be (probably am) misunderstanding the question, but might it be possible to leverage generator-p5js or p5-cli? From my testing they work equally well, although it's much easier to get p5-cli going.

  • @quaestor neither of those will run p5js on a node server in the way the OP is asking. generator-p5j is a yeoman scaffold that quickly generates a project folder structure (including Gulp). p5-cli runs a local server to allow testing of p5js pages...

    The OP wants p5 running on the node server itself; so that it can render images to the server. The only other workaround I can think of would be to run a virtual browser (something like phantomjs though strictly speaking that's mainly intended for testing) on the server - might jut be possible...

  • So, what I ended up doing was using plain old Processing/Java on a server and invoking Schiffman's ideas regarding running P5 headless.

    https://github.com/processing/processing/wiki/Running-without-a-Display

    It works pretty well for now, but it would be nice to role this into a .js solution which was kinda a p5.js server. Anyhow, check it out on twitter, tweeting texts derived from Guy Debord's Society of the Spectacle, with some noise based images here: @nanofortran

  • @jgeist -- thanks for sharing that your solution was Java headless.

    Nice project. You should post it to the forum Share Your Work channel!

    Re:headless p5.js -- if someone wanted to pursue this, the suggestion from @blindfish to run it by scripting phantomjs actually sound like it should work to me! (Although maybe I'm wrong -- you try it, jgeist?) It also sounds like it would be a big pain....

  • I will try to put together a little tutorial in the the next few days to show how it all came together under Linux 14.04. Will post to the sharing channel. As for phantom.js, I spent a day on it and abandoned it, I don't recall what the issue was (I need more experience with .js, too).

    Thanks.

Sign In or Register to comment.