Skip to main content
Participant
Jan 21, 2024 7:51 AM UTC
Question

How to make position only move inside the screen boundary after adding time expression

Hello,

I want to move a watermark around the screen like the old dvd players used to do. Rather than manually making a mask path, I intended on using expression to achieve this effect. I currently have modulated the watermark to move around by:

 

speed=50;

position + [time,time]*speed

 

This achieves the movement, but the position moves continually off screen and does not stay within. How can i create boundaries to stop position from moving off screen?

 

Best,

Rat

This topic is closed to new replies. Start a new post to keep the conversation going.

2 replies

Legend
Jan 21, 2024 9:44 AM UTC

Position Expression: 

 

seedRandom(index, true);

w = sourceRectAtTime().width * scale[0] / 100;
h = sourceRectAtTime().height * scale[1] / 100;

offset = 5; // offset or speed

// random start position
x = w / 2 + Math.floor(Math.random() * thisComp.width - w) - offset;
y = h / 2 + Math.floor(Math.random() * thisComp.height - h) - offset;
dx = 1;
dy = 1;

path = [];
for (var i = 0; i <= timeToFrames(time); i++) {
  x += dx * offset;
  y += dy * offset;
  x = clamp(x, w / 2, thisComp.width - w / 2);
  y = clamp(y, h / 2, thisComp.height - h / 2);
	
  if (x >= thisComp.width - w / 2 || x <= w / 2) {
    dx *= -1
  }
  if (y >= thisComp.height - h / 2 || y <= h / 2) {
    dy *= -1
  }
  path.push([x, y])
}
path[timeToFrames(time)]

 

 

Translate
Mylenium
Legend
Jan 21, 2024 8:06 AM UTC

You need to limit the ranges by using linear() expressions comparing position to comp width and such and of course you'd need further code to do the direction changes.

 

Mylenium

Translate