Navigation

Version

Starter

p5.videomotion example
Part of the p5.videomotion examples · Category: Basics

The smallest useful sketch: webcam, adaptive background, two zones and the debug overlay.

Live sketch. Allow camera access when the browser asks. If nothing appears, see Troubleshooting.

Source code

Save as sketch.js next to an index.html that loads p5.js and p5.videomotion.js (see Installation).

// Starter: webcam + adaptive background + 2 zones
let cam, motion;

function setup() {
  createCanvas(640, 480);
  cam = createCapture(VIDEO, { flipped: true });
  cam.hide();
  motion = createMotion(cam);

  motion.zone('left', 40, 140, 160, 200)
    .onEnter(z => console.log(z.id, 'ON'))
    .onExit(z => console.log(z.id, 'OFF'));

  motion.zone('right', 440, 140, 160, 200)
    .onEnter(z => console.log(z.id, 'ON'))
    .onExit(z => console.log(z.id, 'OFF'));
}

function draw() {
  image(cam, 0, 0, width, height);
  motion.debug();
}

Used in this example

createMotion(), debug, zone

· All examples · Without a camera →