p5.js is a JavaScript library for creative coding. A collection of pre-written code, it provides us with tools that simplify the process of creating interactive visuals with code in the web browser.
A p5.js project is like any other web project—it utilizes HTML, CSS, and JavaScript. A typical p5.js project includes the p5.js library, index.html, style.css, and sketch.js.
The p5.js library must be included using a <script>
tag in the <head>
section of an HTML document. Only then, the p5.js library can be used in a JavaScript file.
<!DOCTYPE html><html lang="en"><head><!-- Include p5.js library --><script src="p5.js"></script><link rel="stylesheet" type="text/css" href="style.css"><meta charset="utf-8" /></head><body><script src="sketch.js"></script></body></html>
The <canvas>
element is an HTML element that renders graphics created with JavaScript’s Canvas API.
Utilizing the Canvas API behind the scenes, the p5.js library provides many built-in drawing functions that simplify drawing to the HTML <canvas>
on the fly using JavaScript.
createCanvas()
FunctionThe createCanvas()
function creates an HTML canvas on the web page, taking the desired canvas width and height as arguments. Typically, it is one of the first functions called in the setup()
function. The createCanvas()
function can only be called once within a p5.js sketch.
function setup(){// Creates a canvas with 800px width and 600px heightcreateCanvas(800, 600);}
The p5.js library will create a default canvas with a width of 100 pixels and a height of 100 pixels when no arguments are provided for the createCanvas()
function.
noCanvas()
FunctionIf, for some reason, your p5.js sketch does not require a canvas, explicitly call the noCanvas()
function to stop the p5.js library from creating a canvas at the start of the program.
function setup() {// No canvas element will be created for this p5.js sketchnoCanvas();}
background()
FunctionThe background()
function sets the background color of the p5.js canvas. The background of a p5.js canvas is transparent by default.
function setup(){// Sets background to a gray colorbackground(127);}
background()
with One ArgumentWhen the background()
function is called with a numeric argument between 0 and 255, the background color will be set to a grayscale value, with 0 being pure black and 255 being pure white.
function setup(){// Sets the background color to whitebackground(255);}
setup()
FunctionAt the beginning of a p5.js program, the p5.js library automatically executes the setup()
function. The setup()
function should not be explicitly called in the sketch.
function setup() {// Runs once at the beginning of the p5.js sketch}
setup()
FunctionThe setup()
function typically contains code that defines the initial state of the sketch, such as the canvas size, background color, and initial values of global variables.
let beginSize;// Initializing the canvas size, background color and beginlSize valuefunction setup() {createCanvas(800, 600);background(220);beginSize = 5;}
draw()
FunctionThe draw()
function is automatically called after the setup()
function, which runs once at the program’s start. The draw()
loop infinitely runs the code block inside the function from top to bottom.
function setup(){// Runs once at the start of the program}function draw(){// Loops infinitely after setup() is run}
The p5.js canvas uses a coordinate system to describe space. The origin, (0, 0), of the canvas is the top-left corner of the canvas.
The canvas coordinate system is described using ordered pairs, (x, y), where the x coordinate is the distance from the left edge of the canvas and the y coordinate is the distance from the top edge of the canvas.
A color value can be represented in various ways with p5.js. It can be given as:
point()
FunctionThe point()
function draws a single pixel at specified coordinates. It takes two arguments where the first argument is the x coordinate, and the second argument is the y coordinate.
The color of the point can be changed with the stroke()
function. The size of the point can be changed with the strokeWeight()
function.
line()
FunctionThe line()
function draws a line between two points and requires four arguments: the x and y positions for each endpoint.
The width of the line can be set with the strokeWeight()
function and the color of the line can be set with the stroke()
function.
rect()
FunctionThe rect()
function draws a rectangle to the canvas. It takes four arguments: the first two arguments are the x and y positions of the top left corner of the rectangle. The third argument is the width, and the fourth argument is the height.
square()
FunctionThe square()
function draws a square to the canvas. It has three required arguments: the first two arguments are the x and y positions of the top left corner of the square. The third argument is the width (and height) of the square.
ellipse()
FunctionThe ellipse()
function draws an ellipse to the canvas. It requires four arguments where the first and second arguments are the x and y positions of the center of the ellipse. The third and fourth arguments are the width and height of the ellipse.
circle()
FunctionThe circle()
function draws a circle to the canvas. It has three required arguments where the first and second arguments are the x and y positions of the center of the circle. The third argument is the width (and height) of the circle.
triangle()
FunctionThe triangle()
function draws a triangle to the canvas. It has six required arguments: the x and y positions for each of the triangle’s three vertices.
quad()
FunctionThe quad()
function draws a quadrilateral to the canvas. It has eight required arguments: the x and y positions for each of the four vertices.
width
and height
width
is a built-in variable that returns the width of the canvas, and the height
variable returns the height of the canvas.
function setup() {createCanvas(400, 800);console.log(width); // Logs 400 to consoleconsole.log(height); // Logs 800 to console}
background()
FunctionThe background()
function sets the color used for the background of the p5.js canvas. The default background is transparent. This function is typically used within the draw()
function to clear the canvas at the beginning of each frame, but it can be used inside the setup()
if the background needs to be only set once.
function draw(){// Sets a red background colorbackground(255, 0, 0);}
fill()
FunctionThe fill()
function sets the color used to fill a shape with the specified color. It must be called prior to drawing the shape. The default fill color is white.
function draw(){// Sets the fill color of the circle to bluefill(0, 0, 255);circle(100, 100, 25);}
noFill()
FunctionThe noFill()
function sets the fill color of a shape as transparent. It must be called before drawing the shape.
function draw(){// Sets the square to have transparent fillnoFill();square(50, 50, 25);}
stroke()
FunctionThe stroke()
function sets the stroke color used for a shape to the specified color. It must be called before drawing the shape. The default stroke color is black.
function draw(){// Sets stroke color of the square to greenstroke(0, 255, 0);square(50, 50, 25);}
strokeWeight()
FunctionThe strokeWeight()
function sets the width of a shape’s stroke to specified pixels. It must be called before drawing the shape. The default stroke weight is 1 pixel.
function draw(){// Draws a line of 5px thicknessstrokeWeight(5);line(50, 25, 50, 75);}
noStroke()
FunctionThe noStroke()
function disables the stroke of a shape. It must be called before drawing the shape.
function draw(){// Draws a circle with blue fill color and no stroke/outlinenoStroke();fill(0, 0, 255);circle(50, 50, 25);}
The order in which shape functions are called is important, as the shape function called last will be rendered on top of previously drawn shapes.
function draw(){// The square will appear on top of the circlecircle(100, 100, 100);square(50, 50, 100);}
for
LoopsA for
loop can be used to draw multiple shapes at once which is useful to create patterns of shapes. The iterator variable of the for
loop can be used for the x and y positions to draw shapes next to each other.
function draw(){// Draw a 5x5 grid of circles that are 25px apart horizontally and verticallyfor(let posX = 0; posX < 5; posX++) {for(let posY = 0; posY < 5; posY++) {circle(posX * 25, posY * 25, 10);}}}