Return to main page
View source or report issues on GitHub

P5.js - A Javascript / TypeScript Drawing Library

For our next several assignments, we will use the p5js library. This is a set of functions that make it very easy to draw and animate shapes on a web site using javascript / TypeScript, which will help us learn programming techniques in a visual way. This will serve as a short introduction to the library.

The setup

I will be providing fully setup assignments for you. All you need to know, really, is that every p5 program contains two functions:

Drawing with p5

Take a look at the very simple code example below (click Run Pen to load it)

See the Pen ExYvPOO by David Griswold (@DavidGriswoldTeacher) on CodePen.

The comments should make it clear what is happening. When you move your mouse over the drawing area at the right, it draws a circle (a.k.a an ellipse with the same width and height).

As this very simple demo should make clear, p5js adds a lot of features to TypeScript that make it easy to do basic drawing and interaction.

If there is something you think p5 should be able to do (draw a line? A triangle? detect a keyboard press?) you can proably learn about in the p5 documentation - go there when playing with p5! The code examples there are in normal javascript, but modifying them for TypeScript is usually a very minor thing to do.