Pausing Part of a Sketch

Pausing Part of a Sketch

The alternative to noLoop(). This tutorial shows how to pause and resume individual parts of a sketch by controlling animation time, rather than stopping the entire draw loop.

Use the ◀ ▶ buttons or arrow keys to step through the stages. Click inside the canvas to pause/resume, and press keys where noted. Drag any number in the code to adjust it live.

This sketch animates two objects: a circle that moves from left to right, and a square that spins in place.

Click the mouse button to pause them both. Click again to resume them.

This uses noLoop() to pause and loop() to resume. This is simple, but it doesn’t give you much control over what is paused and what is resumed.

function setup() {
  createCanvas(windowWidth, 250);
}

function draw() {
  background(100, 50);

  // veering circle
  let x = (millis() / 5) % width;
  let y = map(sin(millis() / 700), -1, 1, 25, height - 25);
  circle(x, y, 50);

  // spinning square
  rectMode(CENTER);
  rotateAbout(millis() / 800, 200, height / 2);
  rect(200, height / 2, 100, 100);
}

function mousePressed() {
  if (isLooping()) {
    noLoop();
  } else {
    loop();
  }
}

function rotateAbout(angle, x, y) {
  translate(x, y);
  rotate(angle);
  translate(-x, -y);
}
Step 1 of 9

Try it on OpenProcessing: Pausing Part of a Sketch