Setting up Visual Studio Code for p5js
I have always been a great fan of Processing, the Java-based language for creative coding. While I do like programming in Python or c++ much more than Java, it is just nice to open the IDE of processing and start getting creative without setting up complicated frameworks. p5js is like Processing but based on Javascript, and so the sketches can be easily shared without other external dependencies.
There is a powerful online editor for p5js on the homepage of the project, but I am a fan of coding in VS Code set up exactly the way I like it. Here I describe how to set up VS Code for p5js.
Remark I know that there exist some all-in-one extensions for p5js but for now, I stick to my slightly more complex way.
1. Setting up the framework
Create a new directory for the sketch. We then need three files as a minimal boilerplate. The minimal index.html page just loads the library and the actual sketch.
|
|
I like to link to a local copy of the library (Download), but you can also refer to the CDN hosted version.
Finally, sketch.js is where the magic happens ;). A minimal template may look like this:
|
|
2. Setting up VS Code
While VS Code is perfectly fine for programming in p5js (after all it is Javascript and you can open the index.html simply in your browser), I do recommend getting two extensions.
- LiveServer opens the sketch in your favorite browser (just press
Go Livein the status bar). - The p5js Snippets extension (by the fantastic Dan Shiffman) may save you time when coding in p5js.