Starting
with p5.js

Processing with AI

Are you ready to discover p5.js? It will allow you to code online, directly in your web browser and quickly preview the result of your program. This module is really important because you will need p5.js all along this course.

The .js in p5.js stand for JavaScript… so, let's code in JavaScript 🙃

You should already know how to code in JavaScript since you have done Designing with Web, another course from the makers'lab, but a quick reminder is never too much, you will find a JS basics reminder in Part 1: Basics.

Every day, we are interacting with dozens of objects that use at least a little bit of code. It is everywhere, from your coffee machine to space rockets. But what does coding mean? In this module, you will learn the basics of p5.js to start creating your projects!


Jetpack

Starter pack

JavaScript is a programming language widely used on the web for scripting behavior and interaction within a web page. Don't be afraid, it's a fairly easy programming language (compared to many others).

p5.js is a JavaScript library that offers beginners the opportunity to learn to code through a graphic approach and a simple programming language so that people, like curious students and others, can experiment and shape their ideas on their own. You will find many resources to guide you on the p5.js reference page.

editor.p5js.org, a web editor for p5.js that allows you to directly code and test your projects in a browser.

Don't mix up p5.js and editor.p5js.org. If you need a reference on how to code a circle, go to p5.js but if you want to code this circle, go to editor.p5js.org. Is everything clear? Let's continue!


How does it look

Here is a video by the Coding Train to discover how the p5.js editor works. Daniel Shiffman from the Coding Train has made many very clear videos that it would be a shame not to use in this course.

In this video you will find:

  1. 1:39 How to log in/sign up
    For this course, you need to log in with your Github account.
  2. 2:20 Where to find your previous sketches
  3. 3:04 How to share your work when it's done
  4. 3:42 How to rename your project
  5. 4:49 How to modify the settings of the interface

One more thing: during the course, you will often have to explain your use cases. To do so, you will have to add your explanations in the .html part of your p5.js projects.
Here is a little step by step to show you how to do it:

At any time you can click on the arrow just below the play button to expand the folder of your project to modify the .html and the .css part.

Interface of editor.p5js.org

You can access all the files of your project, .html, .js, or .css files but also any potential images or media you may have added.

.html

index.html

.js

script.js

.css

style.css


Two main functions

The setup() and the draw() are the two main functions you will use during this course. The setup() function is called once when the program starts. The draw() function continuously executes the lines of code contained inside its block right after the setup() is over.

Interface of editor.p5js.org

When you create a sketch, it starts always like this ⬆️

A purple square on a black background

Define a black background once (in the setup() function) and draw a purple rectangle on it at each frame (in the draw() function).

Help yourself

Interface of p5js.org

p5.js reference page you can already add it to your favorites 😉

You will regularly need to consult the p5.js references, to do so, here are some explanations:

If you are lost even after reading the references, you can ask for help on Slack!.

When you have finished or when needed you can share your work. To do so you just need to be logged in, save your work and then click on File > Share.

You will be able to choose between four links:

  • /embed/: to use your p5.js sketch embedded in a website.
  • /present/: this link only displays the preview part of your sketch, not your code neither the p5.js environment.
  • /full/: displays the preview part of your sketch and the p5.js environment.
  • /edit/: displays your code beside the preview part.

PRACTICE

This project is optional and not graded, but you can do it to check your knowledge 🤓

Use p5.js and the p5.js editor to make a drawing machine, that includes at least:

Have a look at the p5.js reference to learn how to use different shapes, colors, and detect user inputs. You can use randomness, and add any interaction you want. Don’t hesitate to write instructions on how to use your drawing machine, either in the drawing or in the code (with comments).


Quiz

Quiz

Watch the video above if you haven't already, and take the quiz below to make sure you now know exactly how p5.js and Javascript work! 🎉 Some answers can be found elsewhere, like through a quick internet search!

This quiz is for your self-assessment only and is not graded!

The due date has expired. You can no longer answer this quiz.


Going further

Interface of editor.p5js.org

Resources

Projects