Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

What's p5.js?

p5.js is JavaScript (JS) library, which is used for creating graphic and interactive experiences for artists and developersdesigners. The code is written directly in JS, and it gets executed like any other JS file that is included on a website. It requires an empty HTML file that links to the p5.js library and your sketch file in the header. 

p5.js based on the core principles of Processing, but there which enables code to take the form of quick “sketches” rather than complex applications. There are some differences in the language syntax in Processing, because it’s based on Java, but it’s otherwise very similar.

Using p5.js

For the purpose of the course we are going to use The easiest way to get started is with Openprocessing.org. It's an online platform, where anyone can create and share visual-based, open-source projects. The big advantage of open processing is that the users can like and comment other comment on other sketches, follow other users and create themed collections. 

...

p5.js has its own web-based editor which compiles the code on the go and displays it directly in the browser.

The Processing IDE, which has to be put in can be used for p5.js mode in order to compile the JS code. It when put in Javascript mode.

The IDE consists of three main parts. 

  1. The text editor, where you can write (or paste in) the code.

  2. Message area and Console/Errors window. The Message area which displays the errors and is useful to see if you e.g., miss a semicolon. The Console/Errors window displays warnings, but can be also used to output text for debugging.

  3. The final part is the Display window. This is where you can see the results of our code after it is compiled, by pressing the Run button in the upper left corner. 

...

The third option is to use one of the text editors e.g. Sublime, Atom, Visual Studio Code and run p5.js directly on your computer. For that, you would need to download a complete the p5.js library and run the code on a local server. For more experienced developers, p5.js can also be used in any web application using npm and the "instance mode" of p5.js

...

In order to understand the examples and to write your own programs, you will be using references, i.e. artefacts that describe a programming languagethe p5.js reference page. Reference pages are essential for understanding how programming languages and frameworks like p5.js can be used.

Basic Sketch Structure

Every p5.js program consists of setup() and draw() function. Setup() runs when the program starts, and it's used to set the initial environment properties, such as the size of the canvas in the browser window. Draw() executes the code inside the block until the program is stopped or noLoop() is called. We will learn more about how functions work in the p5.js Writing our own Events and Functions.

Code Block
languagejs
function setup() {
  createCanvas(300,300);
  // setup stuff
}

function draw() {
  // draw stuff
}

...

Experiment with the examples by adding or combined combining new drawing commands (Arc, Point, Triangle, etc.). Look through the references page to find more.

...