Include p5 libs and sketch via 'require' or npm install?

I've been playing around with a node / express / p5 project and it would be tidier (and more modern) to not have to include the p5 libs and sketch file in index.html. Is there a way to do this, perhaps using require? There's an npm for p5 but that doesn't include the sketch. I admit I am a noob in the area.

Answers

  • If you intend to run this on the client side (p5 won't run on node server side as it is dependent on accessing the window object) then you'll need some way to require the p5 dependency from npm in the client. I've been feeling the love for Browserify lately (don't let that awful graphic put you off!). You could certainly use that to bundle the p5 dependency up with your sketch code in a node-like way...

    TBH I haven't bothered going that far and I'm happy loading p5 from a CDN - that's no less 'modern' IMO... And it won't be long before we swing the other way and start splitting all our dependencies into tiny chunks for separate loading/caching. See this blog post for an explanation ;)

  • Browserify looks pretty slick. I'm working on something that will have a bunch of little script files (for classes etc) and now I should be able to bundle those all up for a 'release'. Thanks!

    On another point you mentioned - is it all of p5 that cannot run on the server side, or just the stuff that interacts with the window? It would be nice to able to use the vector stuff and collide2D on the server side - I've done vector math and collision detection from scratch before, but...

    If not, I wonder if a 'headless' version of p5 would be possible - or perhaps it's just too limited a use case to bother creating.

  • edited July 2016

    If you're looking for a "headless" vector lib, I've got 1 written in TypeScript. :-\"
    But it's just casually experimental. I've never tested it yet, lazy moi! b-(
    But I guess it's worth a try. It's based on the Pjs library btW: O:-)

    Download:

    https://GitHub.com/GoToLoop/pjs-proto/blob/master/Pjs.js

    Source:

    https://GitHub.com/GoToLoop/pjs-proto/tree/master/math

    Extremely Loose Reference:

    http://ProcessingJS.org/reference/PVector/

Sign In or Register to comment.