Time displacement / slit-scan webcam looping issue

I have this effect on the webcam that is creating a delayed portion that grows from bottom to top. I’m pretty beginner so I don’t know where to start. I’ve been trying to get it to just continually have the slit-scan effect. Please help!

import processing.video.*;

Capture cam;
int h = 10;
int y = 0;
PImage[] history; 
int historyIndex = 0;
int offset = 0;

boolean paused = false;  // Flag to control slitscan pause/resume

void setup() {
size(640, 480);
cam = new Capture(this, 640, 480);
history = new PImage[height / h];
for (int i = 0; i < history.length; i++) {
  history[i] = createImage(width, height, RGB);
}
cam.start();
background(0);
frameRate(24);
}

void captureEvent(Capture cam) {
cam.read();
if (!paused) {
  history[historyIndex].copy(cam, 0, 0, width, height, 0, 0, width, height);
  historyIndex = (historyIndex + 1) % history.length;
}
}

void draw() {
for (int i = 0; i < history.length; i++) {
  int y = i * h;
  int currentIndex = (i + offset + history.length) % history.length;
  PImage currentImage = history[currentIndex];
  copy(currentImage, 0, y, width, h, 0, y, width, h);
}

if (!paused) {
  offset++;
}
}

void mouseClicked() {
paused = !paused;  // Toggle pause/resume
}