Navigation

Version

Mask ghosts

p5.videomotion example
Part of the p5.videomotion examples · Category: Background and mask

Uses motion.mask as an image: moving areas are added onto a slowly fading canvas, leaving ghost trails.

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).

// motion.mask is a p5.Image: white where there is motion, black elsewhere.
let cam, motion;

function setup() {
  createCanvas(640, 480);
  cam = createCapture(VIDEO, { flipped: true });
  cam.hide();
  motion = createMotion(cam, { background: 'frame', threshold: 0.08, blobs: false, flow: false });
  background(0);
}

function draw() {
  background(0, 18);                    // fade what was drawn before
  if (!motion.ready) return;

  blendMode(ADD);
  tint(60 + 60 * sin(frameCount * 0.02), 120, 255, 120);
  image(motion.mask, 0, 0, width, height);
  noTint();
  blendMode(BLEND);
}

Used in this example

createMotion(), mask, ready

← Background modes · All examples · Resolution and manual updates →