Variables and Interactivity

Variables and Interactivity

In this step-by-step tutorial, you’ll build an interactive face drawing using p5.js. You’ll learn how to use variables to organize your code, and mouseX/mouseY to add interactivity.

Use the ◀ ▶ buttons or arrow keys to step through the stages. Drag any number in the code to change it and see the result update live.

Here is a basic face.

Some numbers — 200, 80, etc. — are repeated several times.

function setup() {
  createCanvas(windowWidth, windowHeight);
  background("pink");
}

function draw() {
  fill("white");
  circle(85, 200, 80);
  circle(185, 200, 80);

  fill("blue");
  circle(85, 200, 80-30);
  circle(185, 200, 80-30);

  fill("black");
  circle(85, 200, 15);
  circle(185, 200, 30);

  fill("purple");
  rect(55, 310, 180, 20);
  circle(55, 305, 60);
  circle(55+170, 305, 60);
}
Step 1 of 11

Try it on OpenProcessing: Interactive Face Sketch